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

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

 1 #include<vector>
 2 #include<string.h>
 3 #include<stdio.h>
 4 #pragma comment(linker, "/STACK:1024000000,1024000000")
 5 using namespace std;
 6 const int M = 3e5 + 10 ;
 7 struct Edge {
 8         int v ;
 9         bool vis ;
10         int nxt ;
11         Edge () {}
12         Edge (int v , int vis , int nxt) :
13                 v(v) , vis(vis) , nxt(nxt) {}
14 }e[M << 2] ;
15 int H[M] , E ;
16
17 int n , m ;
18 int in[M] ;
19 int res[M << 1] ;
20 void addedge (int u , int v) {
21         e[E] = Edge (v , 0 , H[u]) ;
22         H[u] = E ++ ;
23         e[E] = Edge (u , 0 , H[v]) ;
24         H[v] = E ++ ;
25 }
26
27 void dfs (int u) {
28         for (int &i = H[u] ; ~i ; ) {
29                 int v = e[i].v ;
30                 if (e[i].vis) {
31                         i = e[i].nxt ;
32                         continue ;
33                 }
34                 e[i].vis = 1 ;
35                 e[i^1].vis = 1 ;
36                 res[i >> 1] = i & 1 ;
37                 in[v] -- ;
38                 in[u] -- ;
39                 i = e[i].nxt ;
40                 dfs (v) ;
41         }
42 }
43
44 void mend () {
45         int p = -1 ;
46         for (int i = 1 ; i <= n ; i ++) {
47                 if (in[i] & 1) {
48                         if (p == -1) {
49                                 p = i ;
50                         }
51                         else {
52                                 addedge (p , i) ;
53                                 in[p] ++ ;
54                                 in[i] ++ ;
55                                 p = -1;
56                         }
57                 }
58         }
59 }
60
61 void solve () {
62         mend () ;
63         for (int i = 1 ; i <= n ; i ++) {
64                 if(in[i]) {
65                         dfs (i) ;
66                 }
67         }
68         for (int i = 0 ; i < m ; i ++) printf ("%d\n" , res[i]) ;
69 }
70
71 int main () {
72         int T ;
73         scanf ("%d" , &T ) ;
74         while (T --) {
75                 scanf ("%d%d" , &n , &m) ;
76                 for (int i = 0 ; i <= n ; i ++) H[i] = -1 ;
77                 E = 0 ;
78                 for (int i = 0 ; i < m ; i ++) {
79                         int u , v ;
80                         scanf ("%d%d" , &u , &v) ;
81                         addedge (u , v) ;
82                         in[u] ++ ;
83                         in[v] ++ ;
84                 }
85                 solve () ;
86         }
87         return 0 ;
88 }

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

所以很明显如果我们能在构造是利用好这个性质的话,整个复杂度为O(m + k)

为什么还有一个常数k?你很容易回发现,题目给定的边数不一定回使每个点的度数为 偶数 , 那么怎么办呢?补边咯,把两两为奇数度的点之间加一条边即可。

那么你可定又会有疑问了,这样添加边会不会导致最后的 “题设的条件” 收到影响?

没事的,因为题目说了 |入度 - 出度| <= 1 ,因为你构造的是欧拉回路,所以找到后的欧拉回路肯定满足所有点|入度 - 出度| = 0 , 而我们在每个点上最多只加了

一条边,所以去掉后,肯定 <= 1 的。

铭神说,构造回路时,因为每个点可能会被遍历到多次,这样如果姿势不对,很容易导致又把那个点的所有边遍历一遍,导致复杂度又变成O(n*m),所以去仔细

看代码吧233

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

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

时间: 2024-12-14 00:54:36

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

MZL&#39;s endless loop(欧拉回路,欧拉路径)

MZL's endless loop Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 751    Accepted Submission(s): 138 Special Judge Problem Description As we all kown, MZL hates the endless loop deeply, and

hdu 5348 MZL&#39;s endless loop 欧拉回路

MZL's endless loop Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1502    Accepted Submission(s): 331Special Judge Problem Description As we all kown, MZL hates the endless loop deeply, and h

hdu5348 MZL&#39;s endless loop(欧拉回路)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud MZL's endless loop Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1705    Accepted Submission(s): 369Special Judge Problem Descripti

hdu5348(2015多校5)--MZL&#39;s endless loop(搜索)

题目链接:点击打开链接 题目大意:给出n个点,m条无向边,现在要求将无向边变为有向边,要保证每个点的出度和入度的差不超过1 直接进行搜索,对每个点进行出度和入度的判断,如果出度大,就先进行反向的搜索(每搜索一条边u,v就认为这是一条v到u的有向边),反之,进行正向搜索(每搜到一条边u,v认为这是一条u到v的有向边),一直搜索到找不到边能继续为止. 注意: 1.已经使用过的边为了防止再次被遍历,可以修改head,head[u] = edge[i].next 2.注意自环,因为搜索是判断能不能继续向

[2015hdu多校联赛补题]hdu5348 MZL&#39;s endless loop

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1,如果无解输出-1 解:考虑奇数度的点一定会成对出现(因为所有度数和肯定是偶数个->因为一条边产生两度~),那么我们可以将奇数度的点两两一连消除掉(两奇数度点的出度入读差的绝对值都为1, 路径上的点的差绝对值为0) 然后偶数度的点可以成环,那么可以搜出所有的环 1 /* 2 * Problem: 3

HDU 5348 MZL&#39;s endless loop 给边定向(欧拉回路,最大流)

题意:给一个所有你可能想得到的奇葩无向图,要求给每条边定向,使得每个点的入度与出度之差不超过1.输出1表示定向往右,输出0表示定向往左. 思路: 这题本来很简单的事,在每两个奇数度的点添加1条无向边,就变成了个欧拉回路了.欧拉回路的定义时,只要每个点的度为偶数,必定有欧拉回路的存在.只需要选择任1个点出发,遍历每条边仅1次,得到就是欧拉回路路径了,再根据每条边的方向选0/1.添加的那些无向边都是不会影响答案的,因为欧拉回路跑得起来肯定是每个点的出度等于入度,而每个点最多只会被多加1条边,根据题目

HDU5348——DFS——MZL&#39;s endless loop

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

HDU 5348 MZL&#39;s endless loop(思想用的是深搜)经典

MZL's endless loop Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1343    Accepted Submission(s): 282 Special Judge Problem Description As we all kown, MZL hates the endless loop deeply, and

图论 HDOJ 5348 MZL&#39;s endless loop

题目传送门 1 /* 2 题意:给一个n个点,m条边的无向图,要求给m条边定方向,使得每个定点的出入度之差的绝对值小于等于1. 输出任意一种结果 3 图论:一个图,必定存在偶数个奇度顶点.那么从一个奇度定点深搜,当碰到另外一个奇度顶点时结束,这样能保证度数差<=1 3.5 详细解释:http://blog.csdn.net/ZSGG_ACM/article/details/47287681 4 */ 5 /*********************************************