题目链接: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 ;
}

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. string::at

    char& at (size_t pos); const char& at (size_t pos) const; #include <string>#include &l ...

  2. P5312 [Ynoi2011]D2T1

    思路:01trie 按位维护 提交:5边 错因:爆int + 少处理询问时的右端点 题解: 见代码(已经不想说什么了) 代码 //I have my own flg; #include<bits ...

  3. word黏贴图片显示不出来

    word图片转存,是指UEditor为了解决用户从word中复制了一篇图文混排的文章粘贴到编辑器之后,word文章中的图片数据无法显示在编辑器中,也无法提交到服务器上的问题而开发的一个操作简便的图片转 ...

  4. eclipse-jee-luna安装ADT-23.0.6出现的问题,以及解决办法

    刚安装好ADT-23.0.6,然后配置sdk路径(最新的版本android-22),然后创建一个新的Android Project; 对于布局界面会出现如下错误,导致无法显示布局界面: java.la ...

  5. MAC 下视频转换格式软件 之 handbrake

    下载地址: https://handbrake.fr/

  6. MySQL数据分析(7)-SQL的两大学习框架

    大家好,我是jacky,很高兴继续跟大家分享<MySQL数据分析实战>课程,前面的课程基本上我把MySQL的原理都做了一定的介绍,有好多朋友说学习MySQL是没有逻辑的,其实jacky是非 ...

  7. vue draggable 火狐拖拽搜索问题

    最近在使用vuedraggable做导航时候,谷歌拖拽是没问题的,但是在火狐测试时候,拖拽时候是可以成功,但是火狐还是打开了一个新的tab,并且搜索了,一开始想着是阻止默认行为,但是在@end时间中阻 ...

  8. java安全学习-环境准备/基础知识

    补java的坑,开始! 1.Intellij一些快捷键 intell常用快捷键: ctrl+n 快速查找定位类的位置 ctrl+q 快速查看某个类的文档信息 shift + F6 快速类.变量重命名 ...

  9. HTTP之实体和编码

    1. Content-Length: 实体的大小 Content-Length 首部指示出报文中实体主体的字节大小.这个大小是包含了所有内容编码的,比如,对文本文件进行了 gzip 压缩的话,Cont ...

  10. suduku

    github地址 PSP: PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 30 30 Estimate 估 ...