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

题:给出n个点,m条无向边,现在要求将无向边变为有向边,要保证每个点的出度和入度的差不超过1

思路来自:http://blog.csdn.net/winddreams/article/details/47281397

直接进行搜索,对每个点进行出度和入度的判断,如果出度大,就先进行反向的搜索(每搜索一条边u,v就认为这是一条v到u的有向边),反之,进行正向搜索(每搜到一条边u,v认为这是一条u到v的有向边),一直搜索到找不到边能继续为止。

对于已经判断过的边删去:head[u] = edge[i].next;

证明不会有-1的情况,对于一个点v,假设入度比出度多2,那么,第一:就会有一条边搜索到v后找不到后继,导致v的入度比出度多1,第二:又有一条边搜索到v,导致v的入度比出度多2,但是这样的话就会和第一条找不到后继冲突,所以不会出现入度比出度多2的情况,(其他情况也是类似),所以不会有-1的结果。

(果然稍难一点自己就不知道怎么办了  好坑orz)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<functional>
  6. #include<queue>
  7. typedef long long ll;
  8. using namespace std;
  9.  
  10. struct node
  11. {
  12. int u,v,ci;
  13. int next;
  14. } edge[700000];
  15. int n,m,to;
  16. int head[100010] , vis[700000] ;
  17. int in[100010] , out[100010] , num[100010] ;
  18. int ans[700000] ;
  19.  
  20. void add(int u,int v,int c)
  21. {
  22. edge[to].u= u;
  23. edge[to].v= v;
  24. edge[to].ci = c;
  25. edge[to].next = head[u];
  26. head[u] = to++;
  27. }
  28.  
  29. void dfs1(int u) //正向搜索
  30. {
  31. for(int i = head[u]; ~i; i = edge[i].next)
  32. {
  33. if(vis[i])
  34. {
  35. head[u] = edge[i].next; //删边
  36. continue;
  37. }
  38. int v = edge[i].v;
  39. if(u != v && in[v] > out[v]) //u->v,in[v] > out[v]时,跳过
  40. continue;
  41. vis[i] = vis[i^1] = 1; //将u,v之间的两条边标记
  42. if(i %2) //有输入可知,i为偶时u->v; i为奇时,v->u
  43. ans[i/2] = 0;
  44. else
  45. ans[i/2] = 1;
  46. out[u]++;
  47. in[v]++;
  48. head[u] = edge[i].next;
  49. dfs1(v);
  50. break;
  51. }
  52. }
  53.  
  54. void dfs2(int u)
  55. {
  56. for(int i = head[u]; ~i; i = edge[i].next)
  57. {
  58. if(vis[i])
  59. {
  60. head[u] = edge[i].next;
  61. continue;
  62. }
  63. int v = edge[i].v;
  64. if(u != v && in[v] < out[v])
  65. continue;
  66. vis[i] = vis[i^1] = 1;
  67. if(i %2)
  68. ans[i/2] = 1;
  69. else
  70. ans[i/2] = 0;
  71. out[v]++;
  72. in[u]++;
  73. head[u] = edge[i].next;
  74. dfs2(v);
  75. break;
  76. }
  77. }
  78.  
  79. int main()
  80. {
  81. int T;
  82. int a, b;
  83. scanf("%d",&T);
  84. while(T--)
  85. {
  86. to = 0;
  87. scanf("%d%d",&n,&m);
  88. memset(vis,0,sizeof(vis));
  89. for(int i = 0;i <= n;i++)
  90. {
  91. in[i] = out[i] = num[i] = 0;head[i] = -1;
  92. }
  93. for(int i = 0; i < m; i++)
  94. {
  95. scanf("%d%d",&a,&b);
  96. add(a,b,i);
  97. add(b,a,i);
  98. num[a]++;
  99. num[b]++;
  100. }
  101.  
  102. for(int i = 1; i <= n; i++)
  103. {
  104. while(in[i] + out[i] < num[i])
  105. {
  106. if(in[i] >= out[i]) //正反不停搜,直到找不到边为止
  107. dfs1(i);
  108. else
  109. dfs2(i);
  110. }
  111. }
  112. for(int i = 0; i < m; i++)
  113. printf("%d\n",ans[i]);
  114. }
  115. return 0;
  116. }

  

2015 多校联赛 ——HDU5348(搜索)的更多相关文章

  1. 2015 多校联赛 ——HDU5323(搜索)

    Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  2. 2015 多校联赛 ——HDU5305(搜索)

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  3. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  4. 2015 多校联赛 ——HDU5335(Walk out)

    Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total S ...

  5. 2015 多校联赛 ——HDU5302(构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. 2015 多校联赛 ——HDU5294(最短路,最小切割)

    Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  7. 2015 多校联赛 ——HDU5325(DFS)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  8. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  9. 2015 多校联赛 ——HDU5319(模拟)

    Painter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

随机推荐

  1. Python科学计算(一)

    作者 J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/ 最新版本的 IPython notebook 课程文件 http://git ...

  2. DOM相关知识

    一.查找元素 间接查找 parentNode // 父节点 childNodes // 所有子节点 firstChild // 第一个子节点 lastChild // 最后一个子节点 nextSibl ...

  3. 用anaconda安装最新的TensorFlow版本

    Google发布了TensorFlow1.4正式版 在anaconad搜索依旧是1.2的版本,通过一番查阅,找到了方法 1,打开anaconda-prompt 2,激活你要安装的环境 activate ...

  4. vue-cli项目中,全局引入jquery

    命令行执行 npm install --save jquery 找到webpack.base.conf.js文件,写入代码: const webpack = require('webpack') 在m ...

  5. Python内置函数(63)——property

    英文文档: class property(fget=None, fset=None, fdel=None, doc=None) Return a property attribute. fget is ...

  6. Python内置函数(48)——__import__

    英文文档: __import__(name, globals=None, locals=None, fromlist=(), level=0) This function is invoked by ...

  7. Electron的代码调试

    刚接触Electron,尝试调试程序时,竟无从下手,所以把这个过程做了下记录 参考工程 根据Electron的官方文档:使用 VSCode 进行主进程调试:https://electronjs.org ...

  8. django 配置URLconf和获取值

    django中正确配置url匹配找到视图: 1 在项目下的settings.py中ROOT_URLCONF = "项目名.urls" 表示 前台发来请求会先去项目下的test3/u ...

  9. Java-Maven(五):Eclipse&Maven下创建java工程&web工程

    本章文章主要学习集成了maven插件的eclipse下,创建java project和web project的步骤. 创建java工程  第一步:使用使用maven project来创建java pr ...

  10. Mysql:查用的基本操作

    查看MySQL提供什么存储引擎: mysql> show engines; 查看MySQL当前默认的存储引擎: mysql> show variables like '%storage_e ...