构造。

有一种十分巧妙的方法可以使图中所有度数为偶数的节点,经过每条边定向后,出度和入度都相等。

首先统计每个节点的度数,将度数为奇数的节点与编号为$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的更多相关文章

  1. codeforces 723E:One-Way Reform

    Description There are n cities and m two-way roads in Berland, each road connects two cities. It is ...

  2. 【codeforces 723E】One-Way Reform

    [题目链接]:http://codeforces.com/contest/723/problem/E [题意] 给你一个无向图; 让你把这m条边改成有向图; 然后使得出度数目等于入度数目的点的数目最多 ...

  3. codeforces 723E (欧拉回路)

    Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...

  4. codeforces 659E E. New Reform(图论)

    题目链接: E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. 【codeforces 732F】Tourist Reform

    [题目链接]:http://codeforces.com/contest/732/problem/F [题意] 给你一张无向图; n个点,m条边; 让你把这张图改成有向边 然后定义r[i]为每个点能够 ...

  6. Codeforces 723e [图论][欧拉回路]

    /* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给你一个有n个点,m条边的无向图,给每条边规定一个方向,使得这个图变成有向图,并且使得尽可能多的点入度与出度相同. 输出有多少个这样的点并且输出 ...

  7. codeforces723E

    One-Way Reform CodeForces - 723E There are n cities and m two-way roads in Berland, each road connec ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  2. kvm增加硬盘挂载

    1.查询需要添加虚拟主机 [root@sz-kvm-110 images]# virsh list --all  Id    名称                         状态 ------- ...

  3. 在linux下创建软链接(即目录映射)

    在linux中创建软链接,使用命令:ln -s. 语法:ln -s 源文件 目标文件.

  4. 奇偶排序Odd-even sort

    又一个比较性质的排序,基本思路是奇数列排一趟序,偶数列排一趟序,再奇数排,再偶数排,直到全部有序 举例吧, 待排数组[6 2 4 1 5 9] 第一次比较奇数列,奇数列与它的邻居偶数列比较,如6和2比 ...

  5. OScached页面缓存的入门使用

    OSCache的使用: 一,环境的搭建: 1,把oscache.jar file放在 /WEB-INF/lib 目录下(Put the oscache.jar file in the /WEB-INF ...

  6. 【BZOJ4540】【HNOI2016】序列 [莫队][RMQ]

    序列 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 给定长度为n的序列:a1,a2,…,a ...

  7. bzoj 1036: [ZJOI2008]树的统计Count——树链剖分

    Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...

  8. 【BZOJ】3626 [LNOI2014]LCA

    [算法]树链剖分+线段树(区间加值,区间求和) [题解]http://hzwer.com/3891.html 中间不要取模不然相减会出错. 血的教训:线段树修改时标记下传+上传,查询时下传.如果修改时 ...

  9. 2009 Round2 A Crazy Rows (模拟)

    Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the ...

  10. Perl6 Bailador框架(8):自定义400/500

    第一种方法, 直接写在源码中: use Bailador; get '/' => sub { '<h1>hello, Bailador</h1>'; } get '/te ...