Codeforces Round #375 (Div. 2)E. One-Way Reform
题目链接:传送门
题目大意:一副无向图,要求你给边定向(变为有向图),使出度等于入度的点最多,输出有多少
个点,并且输出定向后的边(前为起点,后为终点)
题目思路:欧拉路
我们这样考虑,先考虑无向图的点的度数,如果为奇数则一定无法变为题目要求的点,ans-1
对于度为偶数的点则一定可以通过调整满足。
处理方法:新建一个虚拟节点0,使所有度为奇数的点向其连一条边,那么最终图中的点的度数都为偶数。
这样就满足欧拉路的条件了。我们只需要跑欧拉路并且将走过的路径保留下来即可。
注意将与虚拟节点连的边删去。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 50005
#define maxn 30010
typedef pair<int,int> PII;
typedef long long LL;
LL read(){
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,k,ans,in[];
int vis[][];
set<int>S[];
vector<PII >V;
void dfs(int u){
while(S[u].size()){
int x=*S[u].begin();S[u].erase(S[u].begin());
if(vis[u][x])continue;
vis[u][x]=vis[x][u]=;
V.push_back(make_pair(u,x));
dfs(x);
}
}
int main(){
int i,j,group,x,y,v,Case=;
group=read();
while(group--){
n=read(),m=read();
mst(in,);V.clear();
mst(vis,);
for(i=;i<=n;++i) S[i].clear();
for(i=;i<=m;++i){
x=read(),y=read();
S[x].insert(y),S[y].insert(x);
++in[x],++in[y];
}
int ans=n;
for(i=;i<=n;++i)if(in[i]&){
--ans;
S[].insert(i),S[i].insert();
}
for(i=;i<=n;++i)
if(S[i].size())
dfs(i);
printf("%d\n",ans);
for(PII u:V)if(u.fi!=&&u.se!=){
printf("%d %d\n",u.fi,u.se);
}
}
return ;
}
Codeforces Round #375 (Div. 2)E. One-Way Reform的更多相关文章
- Codeforces Round #375 (Div. 2) - D
题目链接:http://codeforces.com/contest/723/problem/D 题意:给定n*m小大的字符矩阵.'*'表示陆地,'.'表示水域.然后湖的定义是:如果水域完全被陆地包围 ...
- Codeforces Round #375 (Div. 2) - C
题目链接:http://codeforces.com/contest/723/problem/C 题意:给定长度为n的一个序列.还有一个m.现在可以改变序列的一些数.使得序列里面数字[1,m]出现次数 ...
- Codeforces Round #375 (Div. 2) - B
题目链接:http://codeforces.com/contest/723/problem/B 题意:给定一个字符串.只包含_,大小写字母,左右括号(保证不会出现括号里面套括号的情况),_分隔开单词 ...
- Codeforces Round #375 (Div. 2) - A
题目链接:http://codeforces.com/contest/723/problem/A 题意:在一维坐标下有3个人(坐标点).他们想选一个点使得他们3个到这个点的距离之和最小. 思路:水题. ...
- Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树
F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...
- Codeforces Round #375 (Div. 2) E. One-Way Reform 欧拉路径
E. One-Way Reform 题目连接: http://codeforces.com/contest/723/problem/E Description There are n cities a ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 贪心
D. Lakes in Berland 题目连接: http://codeforces.com/contest/723/problem/D Description The map of Berland ...
- Codeforces Round #375 (Div. 2) B. Text Document Analysis 模拟
B. Text Document Analysis 题目连接: http://codeforces.com/contest/723/problem/B Description Modern text ...
- Codeforces Round #375 (Div. 2) A. The New Year: Meeting Friends 水题
A. The New Year: Meeting Friends 题目连接: http://codeforces.com/contest/723/problem/A Description There ...
- Codeforces Round #375 (Div. 2) Polycarp at the Radio 优先队列模拟题 + 贪心
http://codeforces.com/contest/723/problem/C 题目是给出一个序列 a[i]表示第i个歌曲是第a[i]个人演唱,现在选出前m个人,记b[j]表示第j个人演唱歌曲 ...
随机推荐
- Remote Desktop Connection没法全屏解决方案
Remote Desktop Connection无法全屏解决方案Sometimes, Remote Desktop Connection总是一个窗口,不自动全屏,任务栏不能自动隐藏起来,要拖动滚动条 ...
- brew install mac安装失败的问题
问题:brew 安装失败思路:将github仓库放到本地,不用ruby下载解决办法:1.下载https://raw.githubusercontent.com/Homebrew/install/mas ...
- rails rake和示例
一篇看到的讲解得不错的文章 http://blog.csdn.net/clskkk2222/article/details/6735365 这里还有一些例子: Rake Documentation R ...
- 使用Backbone构建精美应用的7条建议
我们在Bizzabo使用Backbone.js已经有两年的时间了.我们从一个小的网页应用为开始,这些小应用是由Backbone驱动的,而且他们看上去非常友好. Backbone天生就不固执己见.你从文 ...
- 上传图片(示列分析) $_FILES
新建一个think_photo数据库,库里用sql CREATE TABLE IF NOT EXISTS `think_photo` ( `id` ) NOT NULL AUTO_INCREMENT, ...
- WPF查找子控件和父控件方法
一.查找某种类型的子控件,并返回一个List集合 public List<T> GetChildObjects<T>(DependencyObject obj, Type ty ...
- e665. 在图像中过滤三元色
This example demonstrates how to create a filter that can modify any of the RGB pixel values in an i ...
- ffplay的音视频同步分析
以前工作中参与了一些音视频程序的开发,不过使用的都是芯片公司的SDK,没有研究到更深入一层,比如说音视频同步是怎么回事.只好自己抽点时间出来分析开源代码了,做音视频编解码的人都知道ffmpeg,他在各 ...
- 转载:erlang程序优化点的总结
erlang程序优化点的总结(持续更新) 转自:http://wqtn22.iteye.com/blog/1820587 转载请注明出处 注意,这里只是给出一个总结,具体性能需要根据实际环境和需要来确 ...
- smrt analysis 软件安装
pacbio 公司把三代测序常用软件整合起来,做成了一个在线的分析软件,类似于galaxy; 安装过程如下,参考官网给的指导手册 http://www.pacb.com/wp-content/uplo ...