MZL's endless loop

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 898    Accepted Submission(s): 178
Special Judge

Problem Description
As we all kown, MZL hates the endless loop deeply, and he commands you to solve this problem to end the loop.
You are given an undirected graph with n vertexs and m edges. Please direct all the edges so that for every vertex in the graph the inequation |out degree − in degree|≤1 is satisified.
The graph you are given maybe contains self loops or multiple edges.
 
Input
The first line of the input is a single integer T, indicating the number of testcases.
For each test case, the first line contains two integers n and m.
And the next m lines, each line contains two integers ui and vi, which describe an edge of the graph.
T≤100, 1≤n≤105, 1≤m≤3∗105, ∑n≤2∗105, ∑m≤7∗105.
 
Output
For each test case, if there is no solution, print a single line with −1, otherwise output m lines,.
In ith line contains a integer 1 or 0, 1 for direct the ith edge to ui→vi, 0 for ui←vi.
 
Sample Input
2
3 3
1 2
2 3
3 1
7 6
1 2
1 3
1 4
1 5
1 6
1 7
 
Sample Output
1
1
1
0
1
0
1
0
1
 
 #include<vector>
#include<string.h>
#include<stdio.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int M = 3e5 + ;
struct Edge {
int v ;
bool vis ;
int nxt ;
Edge () {}
Edge (int v , int vis , int nxt) :
v(v) , vis(vis) , nxt(nxt) {}
}e[M << ] ;
int H[M] , E ; int n , m ;
int in[M] ;
int res[M << ] ;
void addedge (int u , int v) {
e[E] = Edge (v , , H[u]) ;
H[u] = E ++ ;
e[E] = Edge (u , , H[v]) ;
H[v] = E ++ ;
} void dfs (int u) {
for (int &i = H[u] ; ~i ; ) {
int v = e[i].v ;
if (e[i].vis) {
i = e[i].nxt ;
continue ;
}
e[i].vis = ;
e[i^].vis = ;
res[i >> ] = i & ;
in[v] -- ;
in[u] -- ;
i = e[i].nxt ;
dfs (v) ;
}
} void mend () {
int p = - ;
for (int i = ; i <= n ; i ++) {
if (in[i] & ) {
if (p == -) {
p = i ;
}
else {
addedge (p , i) ;
in[p] ++ ;
in[i] ++ ;
p = -;
}
}
}
} void solve () {
mend () ;
for (int i = ; i <= n ; i ++) {
if(in[i]) {
dfs (i) ;
}
}
for (int i = ; i < m ; i ++) printf ("%d\n" , res[i]) ;
} int main () {
int T ;
scanf ("%d" , &T ) ;
while (T --) {
scanf ("%d%d" , &n , &m) ;
for (int i = ; i <= n ; i ++) H[i] = - ;
E = ;
for (int i = ; i < m ; i ++) {
int u , v ;
scanf ("%d%d" , &u , &v) ;
addedge (u , v) ;
in[u] ++ ;
in[v] ++ ;
}
solve () ;
}
return ;
}

根据“欧拉回路”的定义,当连通图所有点的度数为偶数时,那么必然会存在一条路线,使得经过所有点并且每条边只经过一次

所以很明显如果我们能在构造是利用好这个性质的话,整个复杂度为O(m + k)
为什么还有一个常数k?你很容易回发现,题目给定的边数不一定回使每个点的度数为 偶数 , 那么怎么办呢?补边咯,把两两为奇数度的点之间加一条边即可。
那么你可定又会有疑问了,这样添加边会不会导致最后的 “题设的条件” 收到影响?
没事的,因为题目说了 |入度 - 出度| <= 1 ,因为你构造的是欧拉回路,所以找到后的欧拉回路肯定满足所有点|入度 - 出度| = 0 , 而我们在每个点上最多只加了
一条边,所以去掉后,肯定 <= 1 的。
 
铭神说,构造回路时,因为每个点可能会被遍历到多次,这样如果姿势不对,很容易导致又把那个点的所有边遍历一遍,导致复杂度又变成O(n*m),所以去仔细
看代码吧233
 

(欧拉通路:除了两个点外度数为奇数,其他点的度数为偶数)

2015多校.MZL's endless loop(欧拉回路的机智应用 || 构造)的更多相关文章

  1. hdu5348 MZL's endless loop(欧拉回路)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud MZL's endless loop Time Limit: 3000/1500 ...

  2. 2015 Multi-University Training Contest 5 hdu 5348 MZL's endless loop

    MZL's endless loop Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  3. Hdu 5348 MZL's endless loop (dfs)

    题目链接: Hdu 5348 MZL's endless loop 题目描述: 给出一个无向图(有环,有重边),包含n个顶点,m条边,问能否给m条边指定方向,使每个顶点都满足abs(出度-入度)< ...

  4. 图论 HDOJ 5348 MZL's endless loop

    题目传送门 /* 题意:给一个n个点,m条边的无向图,要求给m条边定方向,使得每个定点的出入度之差的绝对值小于等于1. 输出任意一种结果 图论:一个图,必定存在偶数个奇度顶点.那么从一个奇度定点深搜, ...

  5. [2015hdu多校联赛补题]hdu5348 MZL's endless loop

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1, ...

  6. HDU 5348 MZL's endless loop 给边定向(欧拉回路,最大流)

    题意: 给一个所有你可能想得到的奇葩无向图,要求给每条边定向,使得每个点的入度与出度之差不超过1.输出1表示定向往右,输出0表示定向往左. 思路: 网络流也是可以解决的!!应该挺简单理解的.但是由于复 ...

  7. HDU 5348 MZL's endless loop(DFS去奇数度点+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题目大意:给你一张图,有n个点,和m条无向边,让你把m条无向边变成有向边,使得每个节点的|出度- ...

  8. HDU 5348 MZL's endless loop

    乱搞题...第一直觉是混合图的欧拉通路,但是感觉并没有多大关系.最终AC的做法是不断的寻找欧拉通路,然后给边标号.所有边访问了一遍,所有点访问了一遍,效率是o(n+m).不存在-1的情况. #incl ...

  9. 2015 多校联赛 ——HDU5348(搜索)

    Problem Description As we all kown, MZL hates the endless loop deeply, and he commands you to solve ...

随机推荐

  1. JS, Node.js, npm简介

    序 听过JS,听过Node,也听过Node.js,还听过npm,然而并不是很清楚的知道都代表什么,这两天调接口,然后前端同学很忙,就自己把前端代码拿过来跑了,也趁机了解一下这几个概念,下边做个小的总结 ...

  2. Markdown的使用简介

    以前有摘抄过,然而onenote速度感人,现在又主要用Linux,所以在这里备份一下,好方便用 Linux下推荐remakeble软件,或者直接sublime text,再或者vim,反正我不会ema ...

  3. java语言:Linux与JVM的内存关系分

    在一些物理内存为8g的服务器上,主要运行一个Java服务,系统内存分配如下:Java服务的JVM堆大小设置为6g,一个监控进程占用大约 600m,Linux自身使用大约800m.从表面上,物理内存应该 ...

  4. Raspberry Pi 3 FAQ --- connect automatically to 'mirrors.zju.edu.cn' when downloading and how to accelerate download

    modify the software source: The software source is a place where several free application for linux ...

  5. ELKstack搭建

    开源实时日志分析ELK平台部署 官网地址:https://www.elastic.co/products 介绍: Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现 ...

  6. Git / 程序员需要知道的12个Git高级命令

    众所周知,Git目前已经是分布式版本控制领域的翘楚,围绕着Git形成了完整的生态圈.学习Git,首先当然是学习Git的基本工作流.相比于SVN等传统版本控制系统来说,Git是专为分布式版本控制而生的强 ...

  7. UML活动图与流程图的区别

    http://blog.chinaunix.net/uid-11572501-id-3847592.html UML活动图与流程图的区别 (1).流程图着重描述处理过程,它的主要控制结构是顺序.分支和 ...

  8. WinForm------PopupMenu控件的使用

    转载: http://www.cnblogs.com/xlx0210/archive/2010/07/14/1777366.html

  9. ORACLE ORA-01157: 无法标识/锁定数据文件

    create undo tablespace MOZI datafile 'E:\oracle\product\10.2.0\oradata\orcl\MOZI.DBF' size 2048M ext ...

  10. Java数据结构——双端链表

    //================================================= // File Name : FirstLastList_demo //------------ ...