Description
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.
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 t should be equal to one.
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.
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
正解:dfs+图论相关性质
解题报告:
比赛的时候想到了度数为奇数的不可能成为答案,但是没想到答案个数就是度数为偶数的点的个数...
因为度数为奇数的点不可能成为答案,那么我们可以发现利用奇数,我们去尽可能地满足偶数。因为如果我们想画出一条链,那么我们必须让路径的头尾都是度数为奇数的点,否则无法满足。
我们这样把度数为奇数的点去掉之后,只剩下度数为偶数的点,根据图论相关性质,我们会发现此时一定存在一种方案使得每个点出度入度相等。直接连边就可以了。
1 //It is made by jump~ 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <ctime> 9 #include <vector> 10 #include <queue> 11 #include <map> 12 #include <set> 13 using namespace std; 14 typedef long long LL; 15 const int inf = (1<<30); 16 const int MAXN = 211; 17 const int MAXM = 40011; 18 int n,m,d[MAXN],ans; 19 int w[MAXN][MAXN]; 20 int ea[MAXM],eb[MAXM],cnt; 21 22 inline int getint() 23 { 24 int w=0,q=0; char c=getchar(); 25 while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 26 while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w; 27 } 28 29 inline void dfs(int x){ 30 while(d[x]) { 31 for(int i=1;i<=n;i++) { 32 if(!w[x][i]) continue; 33 w[x][i]=w[i][x]=0; ea[++cnt]=x; eb[cnt]=i; 34 d[x]--; d[i]--; x=i; break; 35 } 36 } 37 } 38 39 inline void work(){ 40 int T=getint(); int x,y; 41 while(T--) { 42 n=getint(); m=getint(); memset(w,0,sizeof(w)); memset(d,0,sizeof(d)); 43 for(int i=1;i<=m;i++) { 44 x=getint(); y=getint(); 45 w[x][y]=w[y][x]=1; 46 d[x]++; d[y]++; 47 } 48 ans=n; cnt=0; 49 for(int i=1;i<=n;i++) if(d[i]&1) ans--; 50 for(int i=1;i<=n;i++) if(d[i]&1) while(d[i]) dfs(i); 51 for(int i=1;i<=n;i++) while(d[i]) dfs(i); 52 printf("%d ",ans); 53 for(int i=1;i<=cnt;i++) printf("%d %d ",ea[i],eb[i]); 54 } 55 } 56 57 int main() 58 { 59 work(); 60 return 0; 61 }