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个人演唱歌曲 ...
随机推荐
- Entity Framework应用:使用Code First模式管理视图
一.什么是视图 视图在RDBMS(关系型数据库管理系统)中扮演了一个重要的角色,它是将多个表的数据联结成一种看起来像是一张表的结构,但是没有提供持久化.因此,可以将视图看成是一个原生表数据顶层的一个抽 ...
- win7控制面板一打开就停止的解决方法
现象:win7系统,打开控制面板后,弹出提示窗口:资源管理器停止工作,需要重启.点重启后,系统自动重建桌面进程.控制面板根本无法使用. 下面是网上找到的方法,如果都不行再参照后面我的解决方法. 1. ...
- 数据库 proc编程八
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...
- android 内存管理机制、异常、垃圾回收
当 Android 应用程序退出时,并不清理其所占用的内存,Linux 内核进程也相应的继续存在,所谓“退出但不关闭”.从而使得用户调用程序时能够在第一时间得到响应. 当系统内存不足时,系统将激活内存 ...
- e664. 在图像中获取子图像
// From an Image image = createImage(new FilteredImageSource(image.getSource(), new CropImageFilter( ...
- cVim——Chrome上更强大的vim插件
ref: http://www.cnblogs.com/voidsky/p/5490787.html 介绍 也许很多人在chrome上都用过类似Vimium, ViChrome的插件,这些插件的目的都 ...
- 轻量级iOS安全框架:SSKeyChain
原文地址: http://blog.csdn.net/kmyhy/article/details/7261065 SSKeyChains对苹果安全框架API进行了简单封装,支持对存储在钥匙串中密码.账 ...
- Oracle查询优化--单表查询
--查询所有 select * from emp; select * from emp where comm is null; --错误表达 --select * from emp where com ...
- xml & < 需要转义
写了个request2XML的方法,每当数据中有'<'.'&'符号时,封装的XML就无法解析.发现了XML里的CDATA属性,问题迎刃而解!在XML文档中的所有文本都会被解析器解析. 只 ...
- delphi中设置listview行高的方法
第一步.在form中放置一个ImageList: 第二步.将ListView的SmallImages设置为第一步中放置的ImageList: 第三部.将imageList的height设置成自己需要的 ...