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. 对JavaScript优化及规范的一些感想

    变量...... 1.一个变量只存一种类型的数据,2.尽量减少对隐式转换的依赖,这样可增强程序的可读性,日后修改程序时不至于混乱,3.使用匈牙利命名法,4.使用局部变量时记得加 var 进行声明,不然 ...

  2. HTML5学习总结-10 Android 控件WebView显示网页

    WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient 1)setWebClient: ...

  3. Interface/接口

    1. 类和结构能够实现接口 2. 接口声明包含如下四种类型:属性.方法.事件和索引:这些函数声明不能包含任何实现代码,而在每一个成员的主体后必须使用分号 3. 继承接口的类或结构必须实现接口中的所有成 ...

  4. CSS学习笔记——CSS选择器样式总结

    <style type="text/css"> * { padding:0; margin:0; } .box h2 { //内边距左边的距离 padding-left ...

  5. Android学习笔记——ListView

    该工程的功能是实现在一个activity中显示一个列表 以下代码是MainActivity.java中的代码 package com.example.listview; import java.uti ...

  6. 移动端图片上传解决方案localResizeIMG先压缩后ajax无刷新上传

    现在科技太发达,移动设备像素越来越高,随便一张照片2M+,但是要做移动端图片上传和pc上略有不同,移动端你不能去限制图片大小,让用户先处理图片再上传,这样不现实.所以理解的解决方案就是在上传先进行图片 ...

  7. 对二进制加密(分散保存-s=sy+a+b)

    #include <stdio.h> #define L 40 void jiaM(int * s,int * a,int *b,int *sy); void jieM(int * a,i ...

  8. PHP----Ajax异步请求

    需要两个PHP页面:1.php是发出请求和接受请求结果的.2.php是处理请求的结果. 1.php中代码: <a href="#" onclick="sendAja ...

  9. JAVA个人知识总结

    1.一个.java文件中只能有一个public类,且必须跟文件名相同,其他类不能以public开头. 2.继承: 子类继承父类的属性和方法. 3.继承和组合: 继承是对“是一种”(is-a)关系建模, ...

  10. [webgrid] – header - (How to Add custom html to Header in WebGrid)

    How to Add custom html to Header in WebGrid MyEvernote Link Posted on March 30, 2013by mtryambake Ho ...