HDOJ 5348 MZL's endless loop

来源:互联网 发布:中国印度边境冲突 知乎 编辑:程序博客网 时间:2024/06/06 01:20

MZL's endless loop

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2043    Accepted Submission(s): 442
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
23 31 22 33 17 61 21 31 41 51 61 7
 

Sample Output
111010101
 

Source
2015 Multi-University Training Contest 5
 

Recommend
wange2014


#include <cstdio>#include <cstring>const int N = 1e5 + 10;int degree[N];int targ[N][2];int head[N];bool vis[N];int edge_cnt, found;struct node{int v, next, dict;}edge[N * 6];void Init() {memset(head, -1, sizeof(head));memset(vis, false, sizeof(vis));memset(targ, 0, sizeof(targ));memset(degree, 0, sizeof(degree));edge_cnt = 0;}void add_edge(int u, int v) {node tmp = {v, head[u], -1};head[u] = edge_cnt;edge[edge_cnt++] = tmp;}void dfs(int u, int dict) {if (vis[u] == true) {found = u;return ;}vis[u] = true;while (~head[u]) {int lct = head[u];node tmp = edge[lct];head[u] = tmp.next;int v = tmp.v;if (edge[lct].dict != -1 || (u != v && targ[v][dict ^ 1] > targ[v][dict]))continue;edge[lct].dict = dict;edge[lct ^ 1].dict = dict ^ 1;targ[u][dict]++;targ[v][dict ^ 1]++;dfs(v, dict);if (found != -1) {if (found == u) {found = -1;continue;}else {vis[u] = false;break;}}break;}vis[u] = false;}int main() {int T;scanf("%d", &T);while (T--) {Init();int n, m;int u, v;scanf("%d%d", &n, &m);for (int i = 0; i < m; ++i) {scanf("%d%d", &u, &v);degree[u]++;degree[v]++;add_edge(u, v);add_edge(v, u);}for (int i = 1; i <= n; ++i) {while (targ[i][0] + targ[i][1] < degree[i]) {found = -1;if (targ[i][0] <= targ[i][1])dfs(i, 0);elsedfs(i, 1);}}for (int i = 0; i < edge_cnt; i += 2)printf("%d\n", edge[i].dict);}return 0;}



0 0