Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

SubmitStatus

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 根据欧拉图的性质,如果一个图是欧拉图,那么每一个点的入度出度一定相等,如果之和就是偶数,入度出度只差也应该是偶数,如果差不是偶数的话,那么一定不是欧拉图
差k是偶数的话就可以通过最大流对无向边确定方向,只需要将多出来的度数k/2变换方向,就可使将图变成欧拉图,通过最大流实现多余的度数重新分配,最后判断是否满流,
如果满流欧拉图就可以建成 【建模方法】
把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度
之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,
也就是总度数为偶数,存在奇数度点必不能有欧拉回路。
好了,现在每个点入度和出度之差均为偶数。那么将这个偶数除以 2,得 x。
也就是说,对于每一个点,只要将 x 条边改变方向(入>出就是变入,出>入就是
9
变出),就能保证出=入。如果每个点都是出=入,那么很明显,该图就存在欧拉
回路。
现在的问题就变成了:我该改变哪些边,可以让每个点出=入?构造网络流
模型。首先,有向边是不能改变方向的,要之无用,删。一开始不是把无向边定
向了吗?定的是什么向,就把网络构建成什么样,边长容量上限 1。另新建 s 和
t。对于入>出的点 u,连接边(u, t)、容量为 x,对于出>入的点 v,连接边(s, v),
容量为 x(注意对不同的点 x 不同)。之后,察看是否有满流的分配。有就是能
有欧拉回路,没有就是没有。欧拉回路是哪个?察看流值分配,将所有流量非 0
(上限是 1,流值不是 0 就是 1)的边反向,就能得到每点入度=出度的欧拉图。
由于是满流,所以每个入>出的点,都有 x 条边进来,将这些进来的边反向,
OK,入=出了。对于出>入的点亦然。那么,没和 s、t 连接的点怎么办?和 s 连
接的条件是出>入,和 t 连接的条件是入>出,那么这个既没和 s 也没和 t 连接的
点,自然早在开始就已经满足入=出了。那么在网络流过程中,这些点属于“中
间点”。我们知道中间点流量不允许有累积的,这样,进去多少就出来多少,反
向之后,自然仍保持平衡。
所以,就这样,混合图欧拉回路问题,解了。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 400
#define MAXM 50000
#define INF 10000000+10
int dis[MAXN],vis[MAXN],head[MAXN],in[MAXN],out[MAXN];
int cur[MAXN],m,n,top,sum;
bool flog;
struct node
{
int u,v,cap,flow,next;
}edge[MAXM];
void init()
{
top=0;
memset(head,-1,sizeof(head));
}
void add(int a,int b,int c)
{
node E1={a,b,c,0,head[a]};
edge[top]=E1;
head[a]=top++;
node E2={b,a,0,0,head[b]};
edge[top]=E2;
head[b]=top++;
}
void getmap()
{
init();
scanf("%d%d",&n,&m);
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=1;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a==b) continue;
if(c==0) add(a,b,1);
in[b]++,out[a]++;
}
flog=true;
sum=0;
for(int i=1;i<=n;i++)
{
int k=abs(in[i]-out[i]);
if(k%2)
{
flog=false;
break;
}
k/=2;
if(in[i]<out[i])
add(0,i,k),sum+=k;
else
add(i,n+1,k);
}
}
bool bfs(int s,int e)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
q.push(s);
dis[s]=0;
vis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(E.cap>E.flow&&!vis[E.v])
{
vis[E.v]=1;
dis[E.v]=dis[E.u]+1;
if(E.v==e)
return true;
q.push(E.v);
}
}
}
return false;
}
int dfs(int x,int a,int e)
{
if(x==e||a==0)
return a;
int flow=0,f;
for(int& i=cur[x];i!=-1;i=edge[i].next)
{
node& E=edge[i];
if(dis[E.v]==dis[x]+1&&(f=dfs(E.v,min(a,E.cap-E.flow),e))>0)
{
E.flow+=f;
edge[i^1].flow-=f;
a-=f;
flow+=f;
if(a==0) break;
}
}
return flow;
}
int MAXflow(int s,int e)
{
int flow=0;
while(bfs(s,e))
{
memcpy(cur,head,sizeof(head));
flow+=dfs(s,INF,e);
}
return flow;
}
void slove()
{
if(!flog)
{
printf("impossible\n");
return ;
}
if(MAXflow(0,n+1)==sum)
printf("possible\n");
else
printf("impossible\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
getmap();
slove();
}
return 0;
}

poj--1637--Sightseeing tour(网络流,最大流判断混合图是否存在欧拉图)的更多相关文章

  1. POJ 1637 Sightseeing tour(最大流)

    POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...

  2. poj 1637 Sightseeing tour【最大流+欧拉路】

    参考:https://www.cnblogs.com/kuangbin/p/3537525.html 这篇讲的挺好的 首先分清欧拉路和欧拉环: 欧拉路:图中经过每条边一次且仅一次的路径,要求只有两个点 ...

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

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

  4. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  5. 网络流(最大流) POJ 1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8628   Accepted: 3636 ...

  6. POJ 1637 Sightseeing tour (SAP | Dinic 混合欧拉图的判断)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6448   Accepted: 2654 ...

  7. POJ 1637 Sightseeing tour(混合图欧拉回路+最大流)

    http://poj.org/problem?id=1637 题意:给出n个点和m条边,这些边有些是单向边,有些是双向边,判断是否能构成欧拉回路. 思路: 构成有向图欧拉回路的要求是入度=出度,无向图 ...

  8. [POJ 1637] Sightseeing tour(网络流)

    题意 (混合图的欧拉回路判定) 给你一个既存在有向边, 又存在无向边的图. 问是否存在欧拉回路. \(N ≤ 200, M ≤ 1000\) 题解 难点在于无向边. 考虑每个点的度数限制. 我们先对无 ...

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

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

随机推荐

  1. BZOJ3413: 匹配(后缀自动机,Parent树,线段树合并)

    Description Input 第一行包含一个整数n(≤100000). 第二行是长度为n的由0到9组成的字符串. 第三行是一个整数m. 接下来m≤5·10行,第i行是一个由0到9组成的字符串s, ...

  2. 紫书 习题8-5 UVa 177 (找规律)

    参考了https://blog.csdn.net/weizhuwyzc000/article/details/47038989 我一开始看了很久, 拿纸折了很久, 还是折不出题目那样..一脸懵逼 后来 ...

  3. ASP.NET-使用事件监视诊断程序异常

    用windows自带的事件监视程序来监视网站的异常 来自为知笔记(Wiz)

  4. hdu5391Zball in Tina Town

    //求(n-1)!%n //n 为合数,答案为0,n为素数 . 威尔逊定理可得 //判定一个自然数是否为素数的充分必要条件. 即:当且仅当p为素数时:( p -1 )! ≡ -1 ( mod p ) ...

  5. What are the differences between WebAPI and WebAPI 2

    http://stackoverflow.com/questions/21298961/what-are-the-differences-between-webapi-and-webapi-2 Maj ...

  6. 详细图解mongodb下载、安装、配置与使用

    记得在管理员模式下运行CMD,否则服务将启动失败 转载:http://blog.csdn.net/boby16/article/details/51221474 详细图解,记录 win7 64 安装m ...

  7. 17.I/O系统访问方式和类型

    I/O方式 轮询  中断  DMA  通道

  8. MetaSploit攻击实例讲解------终端下PostgreSQL数据库的使用(包括kali linux 2016.2(rolling) 和 BT5)

    不多说,直接上干货! 配置msf连接postgresql数据库 我这里是使用kali linux 2016.2(rolling)   用过的博友们都知道,已经预安装好了PostgreSQL. 1. p ...

  9. WHERE、ORDER BY、GROUP BY、HAVING语句解析(二十八)

    之前啊,我们提及到,对于update和delete,若不带where条件,则对所有记录都有效. 一.WHERE条件表达式 (1)对记录进行过滤,如果没有指定WHERE子句,则显示所有记录. (2)在W ...

  10. HDFS文件上传下载过程(详图)