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 ...
随机推荐
- 001淘淘商城项目:项目的Maven工程搭建
开始一个新的项目,特此记录,资料全部来源于传智播客,感谢. 我们要做一个类似电商的项目.用maven做管理. maven里面主要分为三种工程: 1:pom工程:用在父级工程,聚合工程中 2:war工程 ...
- GIT常用命令备忘
Git配置 git config --global user.name "storm" git config --global user.email "stormzhan ...
- Xcode里-ObjC, -all_load, -force_load
最近在做一个项目的时候,需要使用到一个第三方库,这个库的使用向导里面特别说明,在添加完该库后,需要在Xcode的Build Settings下Other Linker Flags里面加入-ObjC标志 ...
- MyBatis.Net 学习手记
MyBatis.NET的前身为IBatis,是JAVA版MyBatis在.NET平台上的翻版,相对NHibernate.EntityFramework等重量级ORM框架而言,MyBatis.NET必须 ...
- JAVA格物致知基础篇:用JAX-RS和Jersey打造RESTful Service
随着服务器的处理能力越来越强,业务需求量的不断累积,越来越多的公司开始从单一服务器,单一业务承载变成了多服务器,多业务承载的快速扩展的过程中.传统的方法很难满足和应付这种业务量的增长和部署方式的改变. ...
- EMV内核使用中的常见问题
EMV内核在使用上会由于调用不当引起的许多问题,本文旨在基于内核LOG(也就是与IC卡交互的指令LOG)的基础上,对一些常见问题作初步的分析与解答,方便不熟悉EMV规范的同学参考. 本文的前提是你已经 ...
- beaglebone_black_学习笔记——(9)UART使用
笔者通过查阅相关资料,了解了BeagleBoneBlack开发板的UART接口特性,掌握的UART接口的基本使用方法,最后通过一个C语言的例程实现串口的自发自收.有了这个串口开发板就可和其他设备进行串 ...
- Safari 下用 "location.href = filePath" 实现下载功能的诡异 bug
Safari 下的一些诡异 bug 我们已经领教一二,比如前文中说的 无痕浏览模式下使用 localStorage 的 API 就会报错.今天我们要讲的是利用 location.href = file ...
- stack overflow错误分析
stack overflow(堆栈溢出)就是不顾堆栈中分配的局部数据块大小,向该数据块写入了过多的数据,导致数据越界,结果覆盖了老的堆栈数据. 或者解释为 在长字符串中嵌入一段代码,并将过程的返回地址 ...
- VS2010+MVC4+Spring.NET2+NHibernate4-传统三层架构-前篇
VS2010+MVC4+Spring.NET2+NHibernate4 - 传统三层架构 - 前篇 一直追求使用开源项目,就因一个字:懒! 一直想整理一下的,却一直懒到现在!从当初用的MVC3到现在的 ...