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 ...
 
随机推荐
- matlab FDR校正
			
http://home.52brain.com/forum.php?mod=viewthread&tid=27066&page=1#pid170857 http://www.mathw ...
 - try catch finally的执行顺序(有return的情况下)
			
结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...
 - 2016 08 27 印刷菜单增加sql语句
			
insert into `module` (`ID`, `CONSONANTCODE`, `CREATEDATE`, `DESCRIPTION`, `HANDLER`, `HASCHILD`, `IC ...
 - SQL使用开窗函数与CTE查询每月销售额的前几名
			
WITH tagTab AS( SELECT YearMonth, pm=RANK() OVER(PARTITION BY YearMonth ORDER BY amount DESC) FROM S ...
 - mac 下卸载mysql的方法
			
今天在mac上瞎折腾时,把mysql玩坏了,想卸载重装,却发现找不到卸载程序,百度了下,将操作步骤备份于此: cd ~/ sudo rm /usr/local/mysqlsudo rm -rf /us ...
 - Python 操作 MongoDB
			
原文 这篇文章主要介绍了使用Python脚本操作MongoDB的教程,MongoDB作为非关系型数据库得到了很大的宣传力度,而市面上的教程一般都是讲解JavaScript的脚本操作,本文则是基于Pyt ...
 - 用jQuery File Upload做的上传控件demo,支持同页面多个上传按钮
			
需求 有这么一个需求,一个form有多个文件要上传,但又不是传统的图片批量上传那种,是类似下图这种需求,一开始是用的swfupload做的上传,但是问题是如果有多个按钮的话,就要写很多重复的代码,于为 ...
 - java并发:线程同步机制之计数器&Exechanger
			
第一节 CountDownLatch (1)初识CountDownLatch (2)详述CountDownLatch CountDownLatch是通过一个计数器来实现的,计数器的初始值为线程的数量. ...
 - 基于FPGA的电压表与串口通信(下)
			
实验操作 上电 接入5V电源,用配套的线,USB那端接电脑即可: 电源开关 按下电源开关 接串口线 接下载线 现在电脑装串口线驱动 R340qd.zip 双击进行安装 设置串口调试助手 Com1要根据 ...
 - ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”
			
ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”,这在给定的上下文中无效 这是一个与Controller.F ...