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.
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
 
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 = ;
  7. struct arc {
  8. int to,next;
  9. bool vis;
  10. arc(int x = ,int y = -) {
  11. to = x;
  12. next = y;
  13. vis = false;
  14. }
  15. } e[];
  16. int head[],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^].vis) continue;
  24. e[i].vis = true;
  25. if(d[e[i].to]) {
  26. d[e[i].to] = ;
  27. return true;
  28. }
  29. if(dfs(e[i].to)) return true;
  30. }
  31. return false;
  32. }
  33. void cao() {
  34. for(int i = ; i <= n; ++i)
  35. if(d[i]) {
  36. d[i] = ;
  37. dfs(i);
  38. }
  39. for(int i = ; 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,-,sizeof head);
  48. for(int i = tot = ; i < m; ++i){
  49. scanf("%d%d",&u,&v);
  50. add(u,v);
  51. add(v,u);
  52. d[u] ^= ;
  53. d[v] ^= ;
  54. }
  55. cao();
  56. for(int i = ; i < tot; i += )
  57. printf("%d\n",e[i].vis);
  58. }
  59. return ;
  60. }

2015 Multi-University Training Contest 5 hdu 5348 MZL's endless loop的更多相关文章

  1. Hdu 5348 MZL's endless loop (dfs)

    题目链接: Hdu 5348 MZL's endless loop 题目描述: 给出一个无向图(有环,有重边),包含n个顶点,m条边,问能否给m条边指定方向,使每个顶点都满足abs(出度-入度)< ...

  2. HDU 5348 MZL's endless loop(DFS去奇数度点+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题目大意:给你一张图,有n个点,和m条无向边,让你把m条无向边变成有向边,使得每个节点的|出度- ...

  3. HDU 5348 MZL's endless loop 给边定向(欧拉回路,最大流)

    题意: 给一个所有你可能想得到的奇葩无向图,要求给每条边定向,使得每个点的入度与出度之差不超过1.输出1表示定向往右,输出0表示定向往左. 思路: 网络流也是可以解决的!!应该挺简单理解的.但是由于复 ...

  4. HDU 5348 MZL's endless loop

    乱搞题...第一直觉是混合图的欧拉通路,但是感觉并没有多大关系.最终AC的做法是不断的寻找欧拉通路,然后给边标号.所有边访问了一遍,所有点访问了一遍,效率是o(n+m).不存在-1的情况. #incl ...

  5. 图论 HDOJ 5348 MZL's endless loop

    题目传送门 /* 题意:给一个n个点,m条边的无向图,要求给m条边定方向,使得每个定点的出入度之差的绝对值小于等于1. 输出任意一种结果 图论:一个图,必定存在偶数个奇度顶点.那么从一个奇度定点深搜, ...

  6. 2015 Multi-University Training Contest 5 hdu 5352 MZL's City

    MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. 2015 Multi-University Training Contest 5 hdu 5349 MZL's simple problem

    MZL's simple problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  8. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  9. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

随机推荐

  1. JavaScript(DOM编程三)

    节点的移动,insertBefore <body> <p>你喜欢哪个城市?</p> <ul id="city"><li id= ...

  2. asp.net mvc-identity初步学习

    Install-Package Microsoft.AspNet.Identity.EntityFramework Install-Package Microsoft.AspNet.Identity. ...

  3. R语言基础-数组和列表

    数组(array) 一维数据是向量,二维数据是矩阵,数组是向量和矩阵的直接推广,是由三维或三维以上的数据构成的. 数组函数是array(),语法是:array(dadta, dim),当中data必须 ...

  4. Autodesk 举办的 Revit 2015 二次开发速成( 1.5 天),教室培训, 地点武汉

    2014年8月26日9:00 – 17:00 2014年8月27日9:00 – 12:00 培训地点: Ø 湖北工业大学 实训楼605教室 Ø 地址:武汉市武昌区南湖李家墩一村一号 Ø 交通路线说明: ...

  5. 使用CNN做文本分类——将图像2维卷积换成1维

    使用CNN做文本分类 from __future__ import division, print_function, absolute_import import tensorflow as tf ...

  6. Laravel异常处理

    Laravel异常处理 标签(空格分隔): php 自定义异常类 <?php namespace App\Exceptions; use Throwable; use Exception; cl ...

  7. 12. Integer to Roman[M]整数转罗马数字

    题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  8. Linux中安装redis的phpredis扩展

    下载phpredis扩展 http://pecl.php.net/package/redis wget http://pecl.php.net/get/redis-4.0.2.tgz 需要环境配置文件 ...

  9. python爬虫:爬取医药数据库drugbank

    这个是帮朋友做的,难点就是他们有一个反爬虫机制,用request一直不行,后面我就用selenium直接把网页copy下来,然后再来解析本地的html文件,就木有问题啦. 现在看来,写得有点傻,多包涵 ...

  10. 指定的WSDL可能与所选的工具包不兼容

    使用LoadRunner在webservice协议下,import service的时候,报错: can not fetch WSDL,the specified WSDL datathe speci ...