One-Way Reform

CodeForces - 723E

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

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 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的更多相关文章

  1. Codeforces723E One-Way Reform【欧拉回路】

    题意:给你n点m边的图,然后让你确定每条边的方向,使得入度=出度的点最多 . 度数为偶数的点均能满足入度 = 出度. 证明:度数为奇数的点有偶数个,奇度点两两配对连无向边,则新图存在欧拉回路,则可使新 ...

随机推荐

  1. django类视图的使用

    1 类视图引入 以函数的方式定义的视图称为函数视图,函数视图便于理解. 但是遇到一个视图对应的路径提供了多种不同HTTP请求方式的支持时,便需要在一个函数中编写不同的业务逻辑,代码可读性与复用性都不佳 ...

  2. 第十三章 ZYNQ-MIZ701 TIMER定时器中断

      上篇文章实现了了PS接受来自PL的中断,本片文章将在ZYNQ的纯PS里实现私有定时器中断.每隔一秒中断一次,在中断函数里计数加1,通过串口打印输出. 本文所使用的开发板是Miz701 PC 开发环 ...

  3. 后端排序,debug模式中map的顺序出错

    js中map遍历的顺序是按照插入的顺序来执行的.如果map的来源是字符串转换的,那么就会按照字符串中key值的顺序进行遍历.千万不要被debug中显示的顺序误导,这里应该是为了方便查看对key进行了字 ...

  4. Python 正则表达模块详解

    Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...

  5. 尝试 javascript 一对多 双向绑定器

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  6. Entity的约束

    在DBContext的OnModelCreating()方法中调用上面的那个类 1.Infrastruture的Database文件夹建立Entityconfiguretions的文件夹 2.MyCo ...

  7. 【原型模式】--重写原型对象prototype的影响

    //[原型模式]--重写原型对象prototype的影响 2014-12-12//定义构造函数function Person() { }//直接指定构造函数的原型为一个对象(为了简化逐个给原型添加成员 ...

  8. spark2.0新特性之DataSet

    1.Spark SQL,DataFrame,DataSet的错误类型检测时机 spark SQL:其类型检测与语法检测是在运行时检测的 DataFrame:在spark2.0以前的版本中,DataFr ...

  9. cmd设置电脑自动关机

    cmd设置电脑自动关机 设置:(3600代表一小时,单位s) shutdown -s -t 3600 取消 shutdown -a

  10. LEANGOO成员

    转自:https://www.leangoo.com/leangoo_guide/leangoo_guide_member.html 1. 看板成员及权限 一个看板上的最大成员限制为200个. 看板的 ...