codeforces 723E:One-Way Reform
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+图论相关性质
解题报告:
比赛的时候想到了度数为奇数的不可能成为答案,但是没想到答案个数就是度数为偶数的点的个数...
因为度数为奇数的点不可能成为答案,那么我们可以发现利用奇数,我们去尽可能地满足偶数。因为如果我们想画出一条链,那么我们必须让路径的头尾都是度数为奇数的点,否则无法满足。
我们这样把度数为奇数的点去掉之后,只剩下度数为偶数的点,根据图论相关性质,我们会发现此时一定存在一种方案使得每个点出度入度相等。直接连边就可以了。
- //It is made by jump~
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- #include <ctime>
- #include <vector>
- #include <queue>
- #include <map>
- #include <set>
- using namespace std;
- typedef long long LL;
- const int inf = (<<);
- const int MAXN = ;
- const int MAXM = ;
- int n,m,d[MAXN],ans;
- int w[MAXN][MAXN];
- int ea[MAXM],eb[MAXM],cnt;
- inline int getint()
- {
- int w=,q=; char c=getchar();
- while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
- while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
- }
- inline void dfs(int x){
- while(d[x]) {
- for(int i=;i<=n;i++) {
- if(!w[x][i]) continue;
- w[x][i]=w[i][x]=; ea[++cnt]=x; eb[cnt]=i;
- d[x]--; d[i]--; x=i; break;
- }
- }
- }
- inline void work(){
- int T=getint(); int x,y;
- while(T--) {
- n=getint(); m=getint(); memset(w,,sizeof(w)); memset(d,,sizeof(d));
- for(int i=;i<=m;i++) {
- x=getint(); y=getint();
- w[x][y]=w[y][x]=;
- d[x]++; d[y]++;
- }
- ans=n; cnt=;
- for(int i=;i<=n;i++) if(d[i]&) ans--;
- for(int i=;i<=n;i++) if(d[i]&) while(d[i]) dfs(i);
- for(int i=;i<=n;i++) while(d[i]) dfs(i);
- printf("%d\n",ans);
- for(int i=;i<=cnt;i++) printf("%d %d\n",ea[i],eb[i]);
- }
- }
- int main()
- {
- work();
- return ;
- }
codeforces 723E:One-Way Reform的更多相关文章
- 【codeforces 723E】One-Way Reform
[题目链接]:http://codeforces.com/contest/723/problem/E [题意] 给你一个无向图; 让你把这m条边改成有向图; 然后使得出度数目等于入度数目的点的数目最多 ...
- CodeForces 723E One-Way Reform
构造. 有一种十分巧妙的方法可以使图中所有度数为偶数的节点,经过每条边定向后,出度和入度都相等. 首先统计每个节点的度数,将度数为奇数的节点与编号为$n+1$的节点连边,这样一来,这张新图变成了每个节 ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
- Codeforces 747D:Winter Is Coming(贪心)
http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...
- Codeforces 747C:Servers(模拟)
http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开 ...
- codeforces 723F : st-Spanning Tree
Description There are n cities and m two-way roads in Berland, each road connects two cities. It is ...
- codeforces 613D:Kingdom and its Cities
Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...
- Codeforces 749D:Leaving Auction(set+二分)
http://codeforces.com/contest/749/problem/D 题意:有几个人在拍卖场竞价,一共有n次喊价,有q个询问,每一个询问有一个num,接下来num个人从这次拍卖中除去 ...
- Codeforces 749B:Parallelogram is Back(计算几何)
http://codeforces.com/problemset/problem/749/B 题意:已知平行四边形三个顶点,求另外一个顶点可能的位置. 思路:用向量来做. #include <c ...
随机推荐
- DEDECMS之八 漏洞错误和疑难杂症
1.dedecms文章加粗b属性后出现strong或者b标签修改 dedecms的文章,如果设置了加粗的属性后,文章标题那会自动添加一个strong或者是b标签,如何去掉呢,方法如下: a.更改自动添 ...
- centos 命令集合
链接: http://www.cnblogs.com/zitsing/archive/2012/05/02/2479009.html http://www.centoscn.com/CentOS/he ...
- svn: Checksum mismatch for 'C:\Documents and Settings\Admin\workspace\pics5\src\baolintest\.svn\text-base\test1.java.svn-base'; expected: '034cc715af5
出现这个问题解决如下: 比如问题文件为\workspace\pics5\src\baolintest\test.java文件,则 1.把在工程根\workspace\pics5\src\baolint ...
- leetcode - 位运算题目汇总(下)
接上文leetcode - 位运算题目汇总(上),继续来切leetcode中Bit Manipulation下的题目. Bitwise AND of Numbers Range 给出一个范围,[m, ...
- Javascript将构造函数扩展为简单工厂
一般而言,在Javascript中创建对象时需要使用关键字new(按构造函数去调用),但是某些时候,开发者希望无论new关键字有没有被显式使用,构造函数都可以被正常调用,即构造函数同时还具备简单工厂的 ...
- Orchard搜索与索引
Orchard提供了索引与搜索的功能.开启Indexing属性可实现索引功能,伴随着一个特定的索引执行(默认包含基础搜索引擎).除了Indexing和Search提供查询索引的功能外(通过关键字或使用 ...
- sql server 事务处理
事物处理 事务是SQL Server中的单个逻辑单元,一个事务内的所有SQL语句作为一个整体执行,要么全部执行,要么都不执行. 事务有4个属性,称为ACID(原子性.一致性.隔离性和持久性) ...
- pay-as-you-go
What is pay as you go? A pay as you go deal means you aren’t tied into a contract and can top up you ...
- 如何在UIimageview里显示一张图片里的某一部分
首先,获取想要显示的部分的大小及位置 CGRect rect: 然后,将此部分从图片中剪切出来 CGImageRef imageRef=CGImageCreateWithImageInRect([im ...
- 42-stat 显示文件的信息
显示文件的信息 stat [options] [file-list] 参数 file-list指定stat所显示的一个或多个文件的路径名 选项 -f 显示文件系 ...