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/Others)
Total Submission(s): 1853    Accepted Submission(s): 400
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.
T1001n1051m3105n2105m7105.
 

 

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 01 for direct the ith edge to uivi0 for uivi.
 

 

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
 

 

Source
 

 解题:欧拉回路+欧拉路径,开始还以为是混合图的欧拉回路呢。。。哎。。。注意爆栈。。。

 1 #pragma comment(linker, "/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn = 300000;
 7 struct arc {
 8     int to,next;
 9     bool vis;
10     arc(int x = 0,int y = -1) {
11         to = x;
12         next = y;
13         vis = false;
14     }
15 } e[1000010];
16 int head[300000],d[maxn],tot,n,m;
17 void add(int u,int v) {
18     e[tot] = arc(v,head[u]);
19     head[u] = tot++;
20 }
21 bool dfs(int u) {
22     for(int &i = head[u]; ~i; i = e[i].next) {
23         if(e[i].vis || e[i^1].vis) continue;
24         e[i].vis = true;
25         if(d[e[i].to]) {
26             d[e[i].to] = 0;
27             return true;
28         }
29         if(dfs(e[i].to)) return true;
30     }
31     return false;
32 }
33 void cao() {
34     for(int i = 1; i <= n; ++i)
35         if(d[i]) {
36             d[i] = 0;
37             dfs(i);
38         }
39     for(int i = 1; i <= n; ++i)
40         while(~head[i]) dfs(i);
41 }
42 int main() {
43     int kase,u,v;
44     scanf("%d",&kase);
45     while(kase--){
46         scanf("%d%d",&n,&m);
47         memset(head,-1,sizeof head);
48         for(int i = tot = 0; i < m; ++i){
49             scanf("%d%d",&u,&v);
50             add(u,v);
51             add(v,u);
52             d[u] ^= 1;
53             d[v] ^= 1;
54         }
55         cao();
56         for(int i = 0; i < tot; i += 2)
57             printf("%d\n",e[i].vis);
58     }
59     return 0;
60 }
View Code

 

posted @ 2015-08-06 10:34  狂徒归来  阅读(167)  评论(0编辑  收藏  举报