题目链接: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.若最大流等于从源点出发的边权之和,则存在欧拉回路。
代码如下:
 #include<stdio.h>
#include<string.h>
#include<queue>
#define mem(a, b) memset(a, b, sizeof(a))
const int MAXN = ;
const int MAXM = ;
const int inf = 0x3f3f3f3f;
using namespace std; int n, m, st, ed, tot; //n个点 m条边(有向 + 无向)
int out[MAXN], in[MAXN], head[MAXN], cnt;
queue<int> Q;
int dep[MAXN]; struct Edge
{
int to, next, flow;
}edge[ * MAXM]; void add(int a, int b, int c)
{
cnt ++;
edge[cnt].to = b;
edge[cnt].next = head[a];
edge[cnt].flow = c;
head[a] = cnt;
} int build()
{
for(int i = ; i <= n; i ++)
{
if((out[i] + in[i]) % ) //如果存在有点的出入跟入度的奇偶性不同 那么无论如何调节都无法做到入度和出度相等
return ;
if(out[i] > in[i])
{
int x = (out[i] - in[i]) / ;
tot += x;
add(st, i, x);
add(i, st, );
}
else if(in[i] > out[i])//入度与出度相等的情况 加不加边无影响
{
int x = (in[i] - out[i]) / ;
add(i, ed, x);
add(ed, i, );
}
}
return ;
} int bfs()
{
if(st == ed)
return ;
mem(dep, -);
dep[st] = ;
Q.push(st);
while(!Q.empty())
{
int index = Q.front();
Q.pop();
for(int i = head[index]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(edge[i].flow > && dep[to] == -)
{
dep[to] = dep[index] + ;
Q.push(to);
}
}
}
return dep[ed] != -;
} int dfs(int now, int zx)
{
if(now == ed)
return zx;
for(int i = head[now]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(dep[to] == dep[now] + && edge[i].flow > )
{
int flow = dfs(to, min(zx, edge[i].flow));
if(flow > )
{
edge[i].flow -= flow;
edge[i ^ ].flow += flow;
return flow;
}
}
}
return -;
} int dinic()
{
int ans = ;
while(bfs())
{
while()
{
int inc = dfs(st, inf);
if(inc == -)
break;
ans += inc;
}
}
return ans;
} int main()
{
int T;
scanf("%d", &T);
while(T --)
{
mem(out, ), mem(in, ), mem(head, -);
cnt = -, tot = ;
scanf("%d%d", &n, &m);
st = , ed = n + ;
for(int i = ; i <= m; i ++)
{
int a, b, op;
scanf("%d%d%d", &a, &b, &op);
out[a] ++ ,in[b] ++;
if(op == ) //只有无向边才能自调节
{
add(a, b, );
add(b, a, );
}
}
if(build())
{
int maxflow = dinic();
if(maxflow == tot) //最大流等于源点出去的边满流
printf("possible\n");
else
printf("impossible\n");
}
else
printf("impossible\n");
}
return ;
}

最新文章

  1. 学习MySQL之数据类型(四)
  2. php预定义$_SERVER实例,所有$_SERVER开头的都是预定义服务变量。
  3. Codeforces Round #342 (Div. 2) A. Guest From the Past(贪心)
  4. listview指定某item的点击效果
  5. 对git的理解及常用指令
  6. python(30) 获取网页返回的状态码,状态码对应问题查询
  7. Leetcode 113. Path Sum II
  8. 推荐使用Wiz笔记发表博客
  9. 12、在XAML中定义处理程序
  10. JavaScript核心
  11. 小巧实用的数字加减插件(jquery插件)
  12. ABP应用层——数据传输对象(DTOs)
  13. css3图片动画旋转
  14. Apache中的gzip压缩作用及配置
  15. linux 普通用户批量创建账户与密码
  16. Hello Flask
  17. 不安装Oracle数据库使用plsqldevloper
  18. mui-当使用addeleventlisener()方法绑定事件时选择器无法绑定事件
  19. 20155202张旭 Exp3 免杀原理与实践
  20. IO流程及优化

热门文章

  1. 题解[NOIP2017] 列队
  2. MAC 下视频转换格式软件 之 handbrake
  3. Python常用知识
  4. 安装包设计-------卸载(MFC)---------知识总结
  5. Linux快速编译
  6. ICEM—二维混合网格对齐节点
  7. 单词拼接(dfs/回溯/递归)
  8. Shiro RememberMe 1.2.4远程代码执行漏洞-详细分析
  9. guava常用集合交集,差集,并集,补集操作
  10. git提交异常 fatal: LF would be replaced by..