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

    JavaScript 文件拖拽上传插件 dropzone.js 介绍 February 19, 2014 / 编程指南 dropzone.js 是一个开源的 JavaScript 库,提供 AJAX ...

  2. hibernate-聚合函数分组统计数据查询

    聚合函数: 实例: package Test; import static org.junit.Assert.*; import java.util.List; import org.hibernat ...

  3. Compiler Warning (level 3) C4800

    #pragma warning( disable : 4800 ) // forcing bool 'true' or 'false' ,忽略4800 警告#pragma warning( disab ...

  4. Alpha版本十天冲刺--Day4

    站立式会议 会议总结 队员 今天完成 遇到的问题 明天要做 感想 鲍亮 解决线程信息传递问题(使用函数回调),Android登录验证接口完善 无 json解析,忘记密码界面验证码获取接口,忘记密码请求 ...

  5. zabbix监控模式、分布式、自动化

    适用场景: 1.监控主机多,性能瓶颈 2.多机房,防火墙 zabbix监控模式 针对agent来说 - 被动模式 - 主动模式(主动汇报服务端) 1)当监控主机超过300台,建议使用主动模式 2)当队 ...

  6. sql自带函数语句

    --取数值表达式的绝对值select abs(-41)      41select abs(41)       41select abs(-41.12)   41.12select abs(41.12 ...

  7. 从.o文件中提取指定开头依赖于外部接口的脚本

    nm -g audio_la-audio.o | grep " U " | awk '{ print $2}' | grep "^gst_"

  8. stamp-po的作用

    stamp-po是表示po文件是否有更新,有更新,则重新编译一次

  9. MyEclipse取消自动跳到Console窗口

    在Myeclipse中当全屏查看其它文件时,如果控制台有东西输出,就会弹出控制台窗口,如何取消? 方法1: -->右键在console窗口中点Preferences, -->将Show w ...

  10. Java Map 简介

    AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, H ...