【Codeforces Round 375 (Div 2) E】【欧拉回路Fleury算法 或网络流】One-Way Reform 每条边定向使得最多的点满足入度=出度

来源:互联网 发布:js 打开新url 编辑:程序博客网 时间:2024/06/13 23:33
E. One-Way Reform
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 t should 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
25 52 14 52 31 33 57 23 74 2
output
31 33 55 43 22 132 43 7

#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 205, M = N * N * 20, Z = 1e9 + 7, inf = 0x3f3f3f3f;template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }int casenum, casei;int n, m;int ind[N], oud[N];int ST, ED;int first[N]; int id;int w[M], cap[M], nxt[M];void ins(int x, int y, int cap_){w[++id] = y;cap[id] = cap_;nxt[id] = first[x];first[x] = id;w[++id] = x;cap[id] = 0;nxt[id] = first[y];first[y] = id;}int d[N];bool bfs(){MS(d, -1);queue<int>q; q.push(ST); d[ST] = 0;while (!q.empty()){int x = q.front(); q.pop();for (int z = first[x]; z; z = nxt[z])if (cap[z]){int y = w[z];if (d[y] == -1){d[y] = d[x] + 1;q.push(y);if (y == ED)return 1;}}}return 0;}int dfs(int x, int all){if (x == ED)return all;int use = 0;for (int z = first[x]; z; z = nxt[z])if (cap[z]){int y = w[z];if (d[y] == d[x] + 1){int tmp = dfs(y, min(cap[z], all - use));cap[z] -= tmp;cap[z ^ 1] += tmp;use += tmp;if (use == all)break;}}if (use == 0)d[x] = -1;return use;}int dinic(){int ret = 0;while (bfs())ret += dfs(ST, inf);return ret;}int b[N], g;void solve(){int sum = 0;g = 0;for (int i = 1; i <= n; ++i){if (abs(ind[i] - oud[i]) % 2 == 1)b[++g] = i;}for (int i = 1; i <= g; i += 2){ins(b[i], b[i + 1], 1);++oud[b[i]];++ind[b[i + 1]];}for (int i = 1; i <= n; ++i){int w = abs(ind[i] - oud[i]) / 2;if (ind[i] > oud[i]){ins(i, ED, w);sum += w;}if (oud[i] > ind[i]){ins(ST, i, w);}}dinic();int ans = n - g;printf("%d\n", ans);for (int i = 2; i <= 2 * m; i += 2){if (cap[i] == 0)printf("%d %d\n", w[i], w[i ^ 1]);else printf("%d %d\n", w[i ^ 1], w[i]);}}int main(){scanf("%d", &casenum);for (casei = 1; casei <= casenum; ++casei){scanf("%d%d", &n, &m);ST = 0;ED = n + 1;MS(first, 0); id = 1;for (int i = 1; i <= n; ++i)oud[i] = ind[i] = 0;for (int i = 1; i <= m; ++i){int x, y; scanf("%d%d", &x, &y);++oud[x];++ind[y];ins(x, y, 1);}solve();}return 0;}/*【trick&&吐槽】【题意】n个点m条边的无向连通图没有自环没有重边我们要把所有点都定向希望使得尽可能多的点拥有相同的入度与出度让你输出满足这个条件的最大点数和每条边最后的定向【类型】欧拉回路 或 网络流【分析】首先,有一个猜想——就是满足那个条件的最大点数为拥有偶数度数的点数。基于这个猜想,我们可以把度数为奇数的点配对连边这时的图一定存在一个欧拉回路,也就使得每个点的入度=出度,使得我们的猜想成立我们可以用欧拉回路算法解决这道题,复杂度O(n+m)也可以套用网络流解决复杂度O(n*m)网络流的建图方式是——一开始把所有点任意定向,(奇点之间配对连边)后来把设w=abs(ind[i]-oud[i])/2如果ind[i]>oud[i],就从i向ED连容量为w的边如果oud[i]>ind[i],就从ST向i连容量为w的边这时跑下最大流,如果满流,就存在欧拉回路因为我们的连边方式,肯定满流,肯定从在欧拉回路我们只要通过流量判定边的具体方向就好了【时间复杂度&&优化】O(n+m) or O(nm)*/


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 内衣肩带老是往下滑怎么办 乳房发育一边大一边小怎么办 母猫乳房有硬块怎么办 19岁乳晕很大乳头很小怎么办 做完俯卧撑胳膊特别痛怎么办 电压低风扇转不动怎么办 小孩不配合康复锻炼怎么办 跑步机安全开关脱落怎么办 办健身卡贵了怎么办 跑步机安全锁丢了怎么办 老公的腿老是疼怎么办 跑步机钥匙丢了怎么办 跑步机磁铁丢了怎么办 瑜伽拉筋拉伤了怎么办康复 大腿拉筋拉伤了怎么办 肚子饿的咕咕叫怎么办 手表折叠扣松了怎么办? 手表链扣松了怎么办? 原车挡泥板碎了怎么办 车检通不过的车怎么办 车子被轻微刮了怎么办 车龙门架弯了怎么办 材料合格工地非说不合格怎么办 汽车翼子板撞过弄好缝隙大怎么办 手表秒针摔掉了怎么办 元征升降机泄压怎么办 自拍杆的杆子往下下滑怎么办 小狗被车压了一下拉血怎么办 有鬼给你磕头让你不好怎么办 腿又粗又短怎么办 腿又短又粗怎么办 因跑步小腿变粗怎么办 健身后小腿变粗怎么办 打非洲鼓打的手疼怎么办 无塔供水压力小怎么办 家用增压水塔压力不稳定怎么办 蝴蝶耳堵比较紧怎么办 跑步机踏板坏了怎么办 遥控器电池没电了怎么办 空调遥控器电池没电了怎么办 想要自慰家里没有情趣用品怎么办