Sightseeing tour
Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8276 Accepted: 3489
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.
Sample Input
4
5 8
2 1 0
1 3 0
4 1 1
1 5 0
5 4 1
3 4 0
4 2 1
2 2 0
4 4
1 2 1
2 3 0
3 4 0
1 4 1
3 3
1 2 0
2 3 0
3 2 0
3 4
1 2 0
2 3 1
1 2 0
3 2 0
Sample Output
possible
impossible
impossible
possible
Source
Northwestern Europe 2003
混合欧拉图
参考下面的解释:
【混合图】
混合图(既有有向边又有无向边的图)中欧拉环、欧拉路径的判定需要借助网络流!
(1)欧拉环的判定:
一开始当然是判断原图的基图是否连通,若不连通则一定不存在欧拉环或欧拉路径(不考虑度数为0的点)。
其实,难点在于图中的无向边,需要对所有的无向边定向(指定一个方向,使之变为有向边),使整个图变成一个有向欧拉图(或有向半欧拉图)。若存在一个定向满足此条件,则原图是欧拉图(或半欧拉图)否则不是。关键就是如何定向?
首先给原图中的每条无向边随便指定一个方向(称为初始定向),将原图改为有向图G’,然后的任务就是改变G’中某些边的方向(当然是无向边转化来的,原混合图中的有向边不能动)使其满足每个点的入度等于出度。
设D[i]为G’中(点i的出度 - 点i的入度)。可以发现,在改变G’中边的方向的过程中,任何点的D值的奇偶性都不会发生改变(设将边i,j>改为j, i>,则i入度加1出度减1,j入度减1出度加1,两者之差加2或减2,奇偶性不变)!而最终要求的是每个点的入度等于出度,即每个点的D值都为0,是偶数,故可得:若初始定向得到的G’中任意一个点的D值是奇数,那么原图中一定不存在欧拉环!
若初始D值都是偶数,则将G’改装成网络:设立源点S和汇点T,对于每个D[i]>0的点i,连边S, i>,容量为D[i]/2;对于每个D[j]<0的点j,连边j, T>,容量为-D[j]/2;G’中的每条边在网络中仍保留,容量为1(表示该边最多只能被改变方向一次)。求这个网络的最大流,若S引出的所有边均满流,则原混合图是欧拉图,将网络中所有流量为1的中间边(就是不与S或T关联的边)在G’中改变方向,形成的新图G”一定是有向欧拉图;若S引出的边中有的没有满流,则原混合图不是欧拉图。
为什么能这样建图?
考虑网络中的一条增广路径S–>i–>…–>j–>T,将这条从i到j的路径在G’中全部反向,则:i的入度加1出度减1,j的入度减1出度加1,路径中其它点的入度出度均不变。而i是和S相连的,因此初始D[i]>0,即i的出度大于入度,故这样反向之后D[i]减少2;同理,j是和T相连的,这样反向之后D[j]增加2。因此,若最大流中边S, i>满流(流量为初始D[i]/2),此时D[i]值就变成了0,也就是i的入度等于出度。因此只要使所有S引出的边全部满流,所有初始D值>0的点的D值将等于0,又因为将边变向后所有点的D值之和不变,所有初始D值小于0的点的D值也将等于0,而初始D值等于0的D点既不与S相连也不与T相连,所以它们是网络中的中间点,而中间点的流入量等于流出量,故它们的入度和出度一直不变,即D值一直为0。因此,整个图G’成为欧拉图。
(2)欧拉路径的判定:
首先可以想到的是枚举欧拉路径的起点i和终点j,然后在图中添加边j, i>,再求图中是否有欧拉回路即可。但是,该算法的时间复杂度达到了O(M * 最大流的时间),需要优化。
前面已经说过,在将边变向的过程中任何点的D值的奇偶性都不会改变,而一个有向图有欧拉路径的充要条件是基图连通且有且只有一个点的入度比出度少1(作为欧拉路径的起点),有且只有一个点的入度比出度多1(作为终点),其余点的入度等于出度。这就说明,先把图中的无向边随便定向,然后求每个点的D值,若有且只有两个点的初始D值为奇数,其余的点初始D值都为偶数,则有可能存在欧拉路径(否则不可能存在)。进一步,检查这两个初始D值为奇数的点,设为点i和点j,若有D[i]>0且D[j]<0,则i作起点j作终点(否则若D[i]与D[j]同号则不存在欧拉路径),连边j, i>,求是否存在欧拉环即可(将求出的欧拉环中删去边j, i>即可)。这样只需求一次最大流。
就是转化成最大流,最一次最大流,看是不是满流
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define PI acos(-1.0)
#define MMM 0x3f3f3f3f
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout)
const int INF = 0x3f3f3f3f;
const int Max=250;
struct node
{
int v;
int cap;
int next;
}Edge[Max*100];
int top;
int Head[Max];
int sign[Max];
int n,m;
int out[Max],in[Max];
void AddEdge(int u,int v,int w)
{
Edge[top].v=v;Edge[top].cap=w;
Edge[top].next=Head[u];
Head[u]=top++;
Edge[top].v=u;Edge[top].cap=0;
Edge[top].next=Head[v];
Head[v]=top++;
}
bool bfs()
{
memset(sign,0,sizeof(sign));
queue<int >Q;
Q.push(0);
sign[0]=1;
while(!Q.empty())
{
int a=Q.front();
Q.pop();
for(int i=Head[a];i!=-1;i=Edge[i].next)
{
if(sign[Edge[i].v]==0&&Edge[i].cap>0)
{
sign[Edge[i].v]=sign[a]+1;
Q.push(Edge[i].v);
}
}
}
return sign[n+1];
}
int dfs(int star,int num)
{
if(star==n+1||num==0)
{
return num;
}
int s=0;
int ant;
for(int i=Head[star];i!=-1;i=Edge[i].next)
{
if(sign[star]+1==sign[Edge[i].v]&&(ant=dfs(Edge[i].v,min(num,Edge[i].cap)))>0)
{
Edge[i].cap-=ant;
Edge[i^1].cap+=ant;
s+=ant;
num-=ant;
if(num==0)
{
break;
}
}
}
return s;
}
int Dinic()
{
int ant=0;
while(bfs())
{
ant+=dfs(0,INF);
}
return ant;
}
int main()
{
int T;
int sum;
int u,v,w;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
memset(out,0,sizeof(out));
memset(in,0,sizeof(in));
memset(Head,-1,sizeof(Head));
top=0;
sum=0;
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&u,&v,&w);
out[u]++;
in[v]++;
if(w==0)
{
AddEdge(u,v,1);
}
}
bool flag=false;
for(int i=1;i<=n;i++)
{
if(out[i]-in[i]>0)
{
AddEdge(0,i,(out[i]-in[i])/2);
}
else if(in[i]-out[i]>0)
{
AddEdge(i,n+1,(in[i]-out[i])/2);
sum+=(in[i]-out[i])/2;
}
if((out[i]-in[i])&1)
{
flag=true;
break;
}
}
if(flag)
{
printf("impossible\n");
continue;
}
else
{
if(sum==Dinic())
{
printf("possible\n");
}
else
{
printf("impossible\n");
}
}
}
return 0;
}
Sightseeing tour的更多相关文章
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
- poj1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8859 Accepted: 3728 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- POJ 1637 Sightseeing tour (混合图欧拉回路)
Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tou ...
- POJ1637 Sightseeing tour (混合图欧拉回路)(网络流)
Sightseeing tour Time Limit: 1000MS Me ...
- 网络流(最大流) POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8628 Accepted: 3636 ...
- POJ 1637 Sightseeing tour(最大流)
POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...
- TZOJ 2099 Sightseeing tour(网络流判混合图欧拉回路)
描述 The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that to ...
- POJ 1637 Sightseeing tour (SAP | Dinic 混合欧拉图的判断)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6448 Accepted: 2654 ...
随机推荐
- PHP判断手机号码是否合法
<html> <head> <script language="JavaScript"> function checkMobile(input) ...
- PHP和JS实现多按钮提交表单
JS: <html> <head> <script> function submitit1() //交由程序1处理 { document.myForm.action ...
- c# 隐藏 控制台应用程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- [原创]java WEB学习笔记82:Hibernate学习之路---映射 一对多关联关系,配置,CRUD方法测试及注意点
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Python学习总结2:raw_input() 与 input()
参考http://www.cnblogs.com/way_testlife/archive/2011/03/29/1999283.html 1. 输入数据要求 raw_input() 直接读取控制台的 ...
- Android Preview显示
Android Studio的功能包含preview窗口, 可以查看布局(layout)的样式; 位置: app->src->main->res(资源)->layout(布局) ...
- linux挂接U盘
挂接U盘fdisk -lDisk /dev/sdd: 131 MB, 131072000 bytes/dev/sdd1 * 1 889 127983+ b Win95 FAT32#mkdir -p / ...
- PHP浮点数的一个常见问题的解答 (转载 http://www.laruence.com/2013/03/26/2884.html)
不过, 我当时遗漏了一点, 也就是对于如下的这个常见问题的回答: <?php $f = 0.58; var_dump(intval($f * 100)); //为啥输出57 ?> 为啥输出 ...
- spark streaming - kafka updateStateByKey 统计用户消费金额
场景 餐厅老板想要统计每个用户来他的店里总共消费了多少金额,我们可以使用updateStateByKey来实现 从kafka接收用户消费json数据,统计每分钟用户的消费情况,并且统计所有时间所有用户 ...
- paper 92:图像视觉博客资源2之MIT斯坦福CMU
收录的图像视觉(也包含机器学习等)领域的博客资源的第二部分,包含:美国MIT.斯坦福.CMU三所高校 1)这些名人大家一般都熟悉,本文仅收录了包含较多资料的个人博客,并且有不少更新,还有些名人由于分享 ...