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. C++容器(三):pair类型

    pair类型 在开始介绍关联容器之前,我们有必要了解一种与之相关的标准库类型–pair类型. 操作 含义 pair<T1, T2> p1 创建一个空的pair对象,它的两个元素分别为T1和 ...

  2. unity3d 自己主动文件更新系统

    游戏内容变更之后.一般而言不会想让玩家下载整个游戏包又一次安装,由于这样会流失大量玩家.全部游戏更新是必须的. 更新的内容包含 数据.资源.代码. 基本原理: 1.将须要更新的文件打包成AssetBu ...

  3. Android笔记---点击事件的四种写法

    Android 点击事件的四种写法: 1. 以内部类的形式实现 OnClickListener 接口.定义点击事件 class MainActivity extents Activity{ // .. ...

  4. zoj 3820 Building Fire Stations (二分+树的直径)

    Building Fire Stations Time Limit: 5 Seconds      Memory Limit: 131072 KB      Special Judge Marjar ...

  5. zzulioj--1813--good string(模拟)

    1813: good string Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 93  Solved: 15 SubmitStatusWeb Boa ...

  6. Dictionary subtraction

    Finding the words from the book that are not in the word list from words.txt is a problem you might ...

  7. RadioButton的drawableTop图片文字不居中

    在安卓应用的开发中,一般普通应用用到最多的就是底部放一个RadioGroup实现切换的布局,今天在实现的时候,却出现了底部RadiButton的drawableTop图片及文字无法居中的情况,经过对比 ...

  8. 用jquery控制表格奇偶行及活动行颜色

    虽然jquery流行已经很多年了,一直都感觉很难,也没有花时间去学习它,只是偶尔哪天心血来潮了去看一点点,时隔多日又会忘得一干二净.最近用到表格奇偶行不同色,不得不去再看jquery,虽然感觉还是难, ...

  9. AngularJS 导航栏动态添加.active

    在传统jQuery中,实现导航栏动态添加.active类的思路比较简单,就是当点击的时候,清除其他.active,然后给当前类加上.active. 但是在AngularJS中,就不能再采用这种jQue ...

  10. OpenGL编程(二)绘制矩形

    上次只是创建了一个简单的窗口,把背景颜色修改为蓝色(默认是黑色),并没有向窗口添加任何图形.这次在上次代码的基础上往窗口中添加一个矩形,设置矩形的颜色,大小等. 1.添加矩形 在(参考上次代码)ren ...