CodeForces 723E One-Way Reform
构造。
有一种十分巧妙的方法可以使图中所有度数为偶数的节点,经过每条边定向后,出度和入度都相等。
首先统计每个节点的度数,将度数为奇数的节点与编号为$n+1$的节点连边,这样一来,这张新图变成了每个节点的度数都是偶数的图,必然存在欧拉回路。任意一条欧拉回路都使得原图中度数为偶数的节点的出度与入度相等。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} int T,n,m,ans,cnt;
vector<int>v[];
int f[];
int ss[],ee[],t[][],p[][]; void dfs(int x)
{
for(int i=v[x].size()-;i>=;i--)
{
if(t[x][v[x][i]]==) continue; t[x][v[x][i]]=t[v[x][i]][x]=;
ss[cnt]=x; ee[cnt]=v[x][i]; cnt++; dfs(v[x][i]);
}
} int main()
{
cin>>T;
for(int cas=;cas<=T;cas++)
{
cin>>n>>m; for(int i=;i<=n+;i++) v[i].clear(); memset(f,,sizeof f);
memset(p,,sizeof p); for(int i=;i<=m;i++)
{
int a,b; cin>>a>>b; v[a].push_back(b);
v[b].push_back(a);
f[a]++; f[b]++;
p[a][b]=p[b][a]=;
} ans=; vector<int>x;
for(int i=;i<=n;i++)
{
if(f[i]%==) ans++;
else x.push_back(i);
} for(int i=;i<x.size();i++)
{
v[n+].push_back(x[i]);
v[x[i]].push_back(n+);
} memset(t,,sizeof t); cnt=;
for(int i=;i<=n+;i++) dfs(i); printf("%d\n",ans); for(int i=;i<m+(n-ans);i++)
{
if(p[ss[i]][ee[i]]==)
printf("%d %d\n",ss[i],ee[i]);
} }
return ;
}
CodeForces 723E One-Way Reform的更多相关文章
- codeforces 723E:One-Way Reform
Description There are n cities and m two-way roads in Berland, each road connects two cities. It is ...
- 【codeforces 723E】One-Way Reform
[题目链接]:http://codeforces.com/contest/723/problem/E [题意] 给你一个无向图; 让你把这m条边改成有向图; 然后使得出度数目等于入度数目的点的数目最多 ...
- codeforces 723E (欧拉回路)
Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...
- codeforces 659E E. New Reform(图论)
题目链接: E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【codeforces 732F】Tourist Reform
[题目链接]:http://codeforces.com/contest/732/problem/F [题意] 给你一张无向图; n个点,m条边; 让你把这张图改成有向边 然后定义r[i]为每个点能够 ...
- Codeforces 723e [图论][欧拉回路]
/* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给你一个有n个点,m条边的无向图,给每条边规定一个方向,使得这个图变成有向图,并且使得尽可能多的点入度与出度相同. 输出有多少个这样的点并且输出 ...
- codeforces723E
One-Way Reform CodeForces - 723E There are n cities and m two-way roads in Berland, each road connec ...
- 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 #346 (Div. 2) E. New Reform dfs
E. New Reform 题目连接: http://www.codeforces.com/contest/659/problem/E Description Berland has n cities ...
随机推荐
- CSS知识之 background-position 用法详细介绍
一.语法 background-position : length || length background-position : position || position 二.取值 length ...
- ES6中字符串的扩展
一.查找字符串 在ES5中,可以使用 indexOf 方法和 lastIndexOf 方法查找字符串: let str = 'hello world'; alert(str.indexOf('o')) ...
- Django ORM常用的函数以及修饰词
函数名称或修饰词 说明 filter() 返回符合指定条件的QuerySet exclude() 返回不符合指定条件的QuerySet ordey_by() 串接到QuerySet之后,针对某一指定的 ...
- idea 修改静态资源不需要重启的办法
快捷键Ctrl + Alt + S打开设置面板,勾选Build project automatically选项: 快捷键Ctrl + Shift + A查找registry命令: 在查找到的regis ...
- Item 29 优先考虑类型安全的异构容器
集合API展示了泛型的一般用法.但是它们(Set,HashMap,Map)限制了每个容器只能有固定数目的类型参数. 比如Set集合,HashMap集合: import java.util.Ha ...
- bzoj 2786 DP
我们可以将=左右的两个数看成一个块,块内无顺序要求,把<分隔的看成两个块,那么我们设w[i][j]代表将i个元素分成j个块的方案数,那么显然w[i][j]=w[i-1][j]*j+w[i-1][ ...
- bzoj 3035 二分答案+二分图最大匹配
大原来做的一道题,偷懒直接粘的原来的程序 http://www.cnblogs.com/BLADEVIL/p/3433520.html /******************************* ...
- bzoj 2440 dfs序
首先我们可以做一遍dfs,用一个队列记录每个点进出的顺序,当每个点访问的时候que[tot++]=x,记为in[x],当结束dfs的时候que[tot++]=x,记为out[x],这样处理出来的队列, ...
- hdu 1232 畅通工程(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 畅通工程 Time Limit: 4000/2000 MS (Java/Others) M ...
- Django之项目搭建和配置总结(一)
安装和创建虚拟环境 参考:linux系统下Python虚拟环境的安装和使用 安装Django包 先进入虚拟环境,在联网下执行: pip install django==1.8.7 1.8.7表示dja ...