codeforces723E
One-Way Reform
There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads.
The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it.
Input
The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input.
Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland.
The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities.
It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200.
Pay attention that for hacks, you can only use tests consisting of one testset, so tshould be equal to one.
Output
For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it.
In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once.
Example
2
5 5
2 1
4 5
2 3
1 3
3 5
7 2
3 7
4 2
3
1 3
3 5
5 4
3 2
2 1
3
2 4
3 7 sol:
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int T,n,m,Deg[N],In[N],Out[N];
set<int>E[N],Ans[N];
bool Bo[N][N],Vis[N];
inline void Link(int x,int y)
{
E[x].insert(y); Bo[x][y]=; Deg[x]++;
}
inline void dfs(int x)
{
// cout<<"x="<<x<<endl;
Vis[x]=;
set<int>::iterator it;
for(it=E[x].begin();it!=E[x].end();it++)
{
int to=*it;
if(!Bo[x][to]) continue;
Bo[x][to]=Bo[to][x]=;
Ans[x].insert(to);
dfs(to);
}
}
int main()
{
// freopen("data.in","r",stdin);
int i;
R(T);
while(T--)
{
R(n); R(m);
for(i=;i<=n;E[i].clear(),Ans[i].clear(),Deg[i]=In[i]=Out[i]=Vis[i]=,i++);
for(i=;i<=m;i++)
{
int x,y; R(x); R(y); Link(x,y); Link(y,x);
}
for(i=;i<=n;i++) if(Deg[i]&) Link(n+,i),Link(i,n+);
for(i=;i<=n;i++) if(!Vis[i]) dfs(i);
set<int>::iterator it;
for(i=;i<=n;i++)
{
for(it=Ans[i].begin();it!=Ans[i].end();it++)
{
if(*it!=n+) In[*it]++,Out[i]++;
}
}
int Sum=;
for(i=;i<=n;i++) if(In[i]==Out[i]) Sum++;
Wl(Sum);
for(i=;i<=n;i++)
{
for(it=Ans[i].begin();it!=Ans[i].end();it++) if(*it!=n+)
{
W(i); Wl(*it);
}
}
}
return ;
}
/*
Input
2
5 5
2 1
4 5
2 3
1 3
3 5
7 2
3 7
4 2
Output
3
1 3
3 5
5 4
3 2
2 1
3
2 4
3 7
*/
codeforces723E的更多相关文章
- Codeforces723E One-Way Reform【欧拉回路】
题意:给你n点m边的图,然后让你确定每条边的方向,使得入度=出度的点最多 . 度数为偶数的点均能满足入度 = 出度. 证明:度数为奇数的点有偶数个,奇度点两两配对连无向边,则新图存在欧拉回路,则可使新 ...
随机推荐
- 怎样查看或修改元素节点的id属性
使用 el.id; el表示获取到的元素节点, 如下所示: // HTML 代码 // <div id="app" class="c1">hello ...
- AngularJS-02 数据绑定和表达式
AngularJS----数据绑定和表达式 1.表达式是AngularJS模板引擎的重要内容,也是视图View的必要组成部分,用来将模型动态转换为可视DOM元素或者其内容. 表达式的形式: 1)常量: ...
- CPU vector operations
CPU vector operations 原文:https://blog.csdn.net/wangeen/article/details/8602028 vector operations 是现代 ...
- LeetCode 腾讯精选50题--子集
根据题意,找到几何中的所有子集,说实话子集是没有什么头绪的,因为如果采用遍历的方法,稍有遗漏不说,代码的嵌套循环层数随着数组大小的增加而增加,想了很久没有头绪后就去看了看评论,然后就被点破了解题的关键 ...
- queryURLParams
let url = 'http://www.douqu.com/index.html?name1=val1&name2=val2'; //1.提取问号后的字符 let asktext = ur ...
- ES6复制数组
ES6复制数组和合并数组 一.复制数组与合并数组 复制数组:它是复合数据类型,直接复制只是复制了指向底层数据结构的指针,而不是复制一个全新的数组 <!DOCTYPE html> <h ...
- 【python】Logging模块
1.日志记录级别 logging.debug<logging.info<logging.warning<logging.error<logging.critical 关键是最高 ...
- 手写走通HTTP server 第三版本
后台 后端 服务端 功能:逻辑处理 算法处理 磁盘交互(数据库 静态文件处理) 要求:健壮性,安全性 并发性能和处理速度 架构合理便于维护扩展 网站后端 httpserver + WebFr ...
- c中fgets与strlen
fgets函数从文件读取'\n'并存储,在'\n'后再增加一个'\0'构成字符串. 但fgets函数需要指定读入的字符数,如果指定了n,则最多只能读取n-1个.fgets在读取了n-1个字符.读到了' ...
- What is libacl.so.1 ?
Google says, "This package contains the libacl.so dynamic library which contains the POSIX 1003 ...
