题目链接:http://poj.org/problem?id=1637

Sightseeing tour
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:10837   Accepted: 4560

Description

The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visited exactly once. The bus should also start and end at the same junction. As in any city, the streets are either one-way or two-way, traffic rules that must be obeyed by the tour bus. Help the executive board and determine if it's possible to construct a sightseeing tour under these constraints.

Input

On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two positive integers m and s, 1 <= m <= 200,1 <= s <= 1000 being the number of junctions and streets, respectively. The following s lines contain the streets. Each street is described with three integers, xi, yi, and di, 1 <= xi,yi <= m, 0 <= di <= 1, where xi and yi are the junctions connected by a street. If di=1, then the street is a one-way street (going from xi to yi), otherwise it's a two-way street. You may assume that there exists a junction from where all other junctions can be reached.

Output

For each scenario, output one line containing the text "possible" or "impossible", whether or not it's possible to construct a sightseeing tour.
题目大意:给出一张混合图(有双向边以及单向边),求是否存在欧拉路径
解题思路:
2.说说自己对混合图欧拉回路的理解。因为有向边的存在,所以一定是需要满足每个点的入度等于出度,但是无向边,我们可以去进行两种选择,来达到寻找欧拉回路的目的。所以首先我们要对无向边进行定向处理,即随意的给无向边一个方向。这样转化成了有向图。
3.转化成有向图之后我们要计算每个点的出入以及出度,若其奇偶性不同,很明显无法通过自调整来实现入度与出度的相等,这幅图就一定不存在欧拉回路。
4.自调整的过程就是跑最大流的过程。对于建图:无向边定向并且容量为1,有向边不可以加入建图中,因为有向边是不可以进行自调整的。对于每个出度大于入度的点,我们从源点向该点连边,边权为出度与入度之差的一半,对于每个入度大于出入的点,从该点向汇点连边,边权为入度与出度之差的一半。从源点到汇点跑一遍最大流。
5.若最大流等于从源点出发的边权之和,则存在欧拉回路。
代码如下:
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. #define mem(a, b) memset(a, b, sizeof(a))
  5. const int MAXN = ;
  6. const int MAXM = ;
  7. const int inf = 0x3f3f3f3f;
  8. using namespace std;
  9.  
  10. int n, m, st, ed, tot; //n个点 m条边(有向 + 无向)
  11. int out[MAXN], in[MAXN], head[MAXN], cnt;
  12. queue<int> Q;
  13. int dep[MAXN];
  14.  
  15. struct Edge
  16. {
  17. int to, next, flow;
  18. }edge[ * MAXM];
  19.  
  20. void add(int a, int b, int c)
  21. {
  22. cnt ++;
  23. edge[cnt].to = b;
  24. edge[cnt].next = head[a];
  25. edge[cnt].flow = c;
  26. head[a] = cnt;
  27. }
  28.  
  29. int build()
  30. {
  31. for(int i = ; i <= n; i ++)
  32. {
  33. if((out[i] + in[i]) % ) //如果存在有点的出入跟入度的奇偶性不同 那么无论如何调节都无法做到入度和出度相等
  34. return ;
  35. if(out[i] > in[i])
  36. {
  37. int x = (out[i] - in[i]) / ;
  38. tot += x;
  39. add(st, i, x);
  40. add(i, st, );
  41. }
  42. else if(in[i] > out[i])//入度与出度相等的情况 加不加边无影响
  43. {
  44. int x = (in[i] - out[i]) / ;
  45. add(i, ed, x);
  46. add(ed, i, );
  47. }
  48. }
  49. return ;
  50. }
  51.  
  52. int bfs()
  53. {
  54. if(st == ed)
  55. return ;
  56. mem(dep, -);
  57. dep[st] = ;
  58. Q.push(st);
  59. while(!Q.empty())
  60. {
  61. int index = Q.front();
  62. Q.pop();
  63. for(int i = head[index]; i != -; i = edge[i].next)
  64. {
  65. int to = edge[i].to;
  66. if(edge[i].flow > && dep[to] == -)
  67. {
  68. dep[to] = dep[index] + ;
  69. Q.push(to);
  70. }
  71. }
  72. }
  73. return dep[ed] != -;
  74. }
  75.  
  76. int dfs(int now, int zx)
  77. {
  78. if(now == ed)
  79. return zx;
  80. for(int i = head[now]; i != -; i = edge[i].next)
  81. {
  82. int to = edge[i].to;
  83. if(dep[to] == dep[now] + && edge[i].flow > )
  84. {
  85. int flow = dfs(to, min(zx, edge[i].flow));
  86. if(flow > )
  87. {
  88. edge[i].flow -= flow;
  89. edge[i ^ ].flow += flow;
  90. return flow;
  91. }
  92. }
  93. }
  94. return -;
  95. }
  96.  
  97. int dinic()
  98. {
  99. int ans = ;
  100. while(bfs())
  101. {
  102. while()
  103. {
  104. int inc = dfs(st, inf);
  105. if(inc == -)
  106. break;
  107. ans += inc;
  108. }
  109. }
  110. return ans;
  111. }
  112.  
  113. int main()
  114. {
  115. int T;
  116. scanf("%d", &T);
  117. while(T --)
  118. {
  119. mem(out, ), mem(in, ), mem(head, -);
  120. cnt = -, tot = ;
  121. scanf("%d%d", &n, &m);
  122. st = , ed = n + ;
  123. for(int i = ; i <= m; i ++)
  124. {
  125. int a, b, op;
  126. scanf("%d%d%d", &a, &b, &op);
  127. out[a] ++ ,in[b] ++;
  128. if(op == ) //只有无向边才能自调节
  129. {
  130. add(a, b, );
  131. add(b, a, );
  132. }
  133. }
  134. if(build())
  135. {
  136. int maxflow = dinic();
  137. if(maxflow == tot) //最大流等于源点出去的边满流
  138. printf("possible\n");
  139. else
  140. printf("impossible\n");
  141. }
  142. else
  143. printf("impossible\n");
  144. }
  145. return ;
  146. }

Sightseeing tour 【混合图欧拉回路】的更多相关文章

  1. POJ 1637 Sightseeing tour ★混合图欧拉回路

    [题目大意]混合图欧拉回路(1 <= N <= 200, 1 <= M <= 1000) [建模方法] 把该图的无向边随便定向,计算每个点的入度和出度.如果有某个点出入度之差为 ...

  2. poj1637 Sightseeing tour(混合图欧拉回路)

    题目链接 题意 给出一个混合图(有无向边,也有有向边),问能否通过确定无向边的方向,使得该图形成欧拉回路. 思路 这是一道混合图欧拉回路的模板题. 一张图要满足有欧拉回路,必须满足每个点的度数为偶数. ...

  3. POJ1637 Sightseeing tour (混合图欧拉回路)(网络流)

                                                                Sightseeing tour Time Limit: 1000MS   Me ...

  4. poj 1637 Sightseeing tour 混合图欧拉回路 最大流 建图

    题目链接 题意 给定一个混合图,里面既有有向边也有无向边.问该图中是否存在一条路径,经过每条边恰好一次. 思路 从欧拉回路说起 首先回顾有向图欧拉回路的充要条件:\(\forall v\in G, d ...

  5. POJ 1637 Sightseeing tour (混合图欧拉回路)

    Sightseeing tour   Description The city executive board in Lund wants to construct a sightseeing tou ...

  6. poj1637 Sightseeing tour 混合图欧拉回路判定

    传送门 第一次做这种题, 尽管ac了但是完全不知道为什么这么做. 题目就是给一些边, 有向边与无向边混合, 问你是否存在欧拉回路. 做法是先对每个点求入度和出度, 如果一条边是无向边, 就随便指定一个 ...

  7. poj1637Sightseeing tour(混合图欧拉回路)

    题目请戳这里 题目大意:求混合图欧拉回路. 题目分析:最大流.竟然用网络流求混合图的欧拉回路,涨姿势了啊啊.. 其实仔细一想也是那么回事.欧拉回路是遍历所有边一次又回到起点的回路.双向图只要每个点度数 ...

  8. POJ1637:Sightseeing tour(混合图的欧拉回路)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10581   Accepted: 4466 ...

  9. POJ 1637 Sightseeing tour(混合图的欧拉回路)

    题目链接 建个图,套个模板. #include <cstdio> #include <cstring> #include <iostream> #include & ...

  10. POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]

    嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...

随机推荐

  1. bzoj2725

    * 给出一张图 * 每次删掉一条边后求 the shortest path from S to T * 线段树维护最短路径树 * 具体维护从某点开始偏离最短路而到达 T 的最小距离 * 首先记录下最短 ...

  2. [Luogu] U18430 萌萌的大河

    https://www.luogu.org/problemnew/show/U18430 思路比较好想 树链剖分 对于1操作 只需将以该点为根的子树打标记,将所有数存入数组排序 每次进行1操作时,判断 ...

  3. Codeforces Round #572 (Div. 2)

    目录 Contest Info Solutions A. Keanu Reeves B. Number Circle C. Candies! D1. Add on a Tree D2. Add on ...

  4. Ubuntu18.04安装和配置Django,并实现简单示例

    一.前言(系统,django介绍,window.mac.linux简单区别) Django是python开发过程最重要的web框架.因为在看的Django教学视频是在mac下安装的,我自己用的是Lin ...

  5. Applications (ZOJ 3705)

    题解:就是题目有点小长而已,可能会不想读题,但是题意蛮好理解的,就是根据条件模拟,计算pts.(送给队友zm. qsh,你们不适合训练了.) #include <iostream> #in ...

  6. sql文件导入老是失败

    这是因为sql文件中的编码格式与库的格式不一致造成的,遇到这种问题先进入sql文件看看其编程格式是什么后再建立库的格式一般我们建立库都只选择utf-8下面的格式都没有选择

  7. sublime的一些记录

    { "keys": ["tab"], "command": "reindent", "context" ...

  8. 图解Linux进程间通信实现原理(1)

    为Linux应用程序的开发人员,对Linux的进程间通信方式肯定是了如指掌,平时的开发中应该会大量的使用到.当你迅速的在键盘上按下[CTRL+C]终止掉一个正在运行中的命令时,你有没有仔细的思考过背后 ...

  9. linux安装puppeteer

    1.安装 下载淘宝镜像的,可以同时下载puppeteer和chromium下面两条语句即可 npm install -g cnpm --registry=https://registry.npm.ta ...

  10. 冲刺阶段——Day1

    [成员分工及任务量] 成员 分工 任务量(小时) 王梓鸿 完成页面设计并编写输入输出图形界面,部分代码测试 20 童皓祯 编写注册和登录模块代码,部分代码测试 20 林郅聪 绘制燃尽图,程序功能整合及 ...