题意:给你n点m边的图,然后让你确定每条边的方向,使得入度=出度的点最多 。

度数为偶数的点均能满足入度 = 出度。

证明:度数为奇数的点有偶数个,奇度点两两配对连无向边,则新图存在欧拉回路,则可使新图所有点入度 = 出度。

 #include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
typedef long long ll;
const int N = ;
int d[N], head[N], tot;
int to[N*N], nex[N*N], vis[N*N];
vector< pair<int, int> > ans;
void init(){
tot = ;
memset(d, , sizeof(d));
memset(head, -, sizeof(head));
ans.clear();
}
void add(int u, int v, int w){// w = 0, exit; w = -1, add
d[v]++;
to[tot] = v;
nex[tot] = head[u];
vis[tot] = w;
head[u] = tot++;
}
void dfs(int x){
for(int& i = head[x]; ~i; ){
if(vis[i] == ) {
i = nex[i];
continue ;
}
if(vis[i] == ) ans.push_back( make_pair(x, to[i]) );
vis[i] = vis[i^] = ;
dfs(to[i]);
}
} int main(){
int t; scanf("%d", &t);
while(t--){
init();
int n, m, u, v;
scanf("%d%d", &n, &m);
for(int i = ; i < m; i++){
scanf("%d%d", &u, &v);
add(u, v, );
add(v, u, );
}
vector<int> tmp;
int ret = n;
for(int i = ; i <= n; i++)
if(d[i]&) tmp.push_back(i), ret--;
for(int i = ; i < tmp.size(); i += ){
add(tmp[i], tmp[i+], -);
add(tmp[i+], tmp[i], -);
}
for(int i = ; i <= n; i++)
dfs(i);
printf("%d\n", ret);
for(int i = ; i < ans.size(); i++)
printf("%d %d\n", ans[i].X, ans[i].Y);
}
return ;
}

Codeforces723E One-Way Reform【欧拉回路】的更多相关文章

  1. codeforces 723E (欧拉回路)

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

  2. URAL 1137 Bus Routes(欧拉回路路径)

    1137. Bus Routes Time limit: 1.0 secondMemory limit: 64 MB Several bus routes were in the city of Fi ...

  3. One-Way Reform

    One-Way Reform time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  4. ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)

    //网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...

  5. [poj2337]求字典序最小欧拉回路

    注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...

  6. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  7. UVA 10054 the necklace 欧拉回路

    有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...

  8. POJ 1637 混合图的欧拉回路判定

    题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...

  9. UVa 12118 检查员的难题(dfs+欧拉回路)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. 源码安装Apache,报错:Cannot use an external APR with the bundled APR-util和httpd: Could not reliably determine the server's fully qualified domain name, using

    一.解决APR和APR-util错误: 1.1.安装APR: [root@ganglia httpd-2.2.23]# cd srclib/apr [root@ganglia apr]# ./conf ...

  2. Nagios监控远端的mysql

    工作原理: 利用特定的用户定期访问指定的mysql数据库.当不能访问或连不通时则报警. 1.在生产库上安装nagios插件    安装略    备注:编译完显示一定要有mysql支持,不然没有chec ...

  3. shell脚本中变量$$、$0等的含义

    $0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上 ...

  4. C++中this指针的用法详解

    转自 http://blog.chinaunix.net/uid-21411227-id-1826942.html 1. this指针的用处: 一个对象的this指针并不是对象本身的一部分,不会影响s ...

  5. getElementByClassName()不兼容的解决办法

    在获取元素时候采用getElementByClassName()方法是比较方便的,但是对于IE6不兼容,可以采用以下代码来自定义这个方法: window.onload=function(){ if(! ...

  6. html的textarea控制字数小案例

    <h3>设计理念说明(200字以内)</h3> <textarea onkeyup="checkLen(this)"></textarea ...

  7. Unity-Animato深入系列---FloatValue阻尼

    回到 Animator深入系列总目录 Animator的SetFloat接口可以设置阻尼,并且4种类型变量只有float是支持阻尼的. public void SetFloat(int id, flo ...

  8. linux系统缓存机制

    http://my.oschina.net/lenglingx/blog/425258 1.缓存机制 为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读 ...

  9. 安装nginx python uwsgi环境 以及模拟测试

    uwsgi帮助文档: http://uwsgi-docs-cn.readthedocs.io/zh_CN/latest/WSGIquickstart.html http://uwsgi-docs.re ...

  10. lua注释

    1. 单行注释 --  功能等同于C++中的// 2. 多行注释  --[[  注释的内容  ]]   功能等同于C++中的 /**/ 3. 多行注释   --[====[   注释和内容  ]=== ...