Sightseeing tour

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9276   Accepted: 3924

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

题意:

给出一张混合图,询问这张图是否存在欧拉回路(经过每条边一次且仅一次)...

分析:

如果一个有向图存在欧拉回路满足的条件是所有的点in[i]==out[i],所以我们可以先给无向边定向,然后记录每个点的入度和出度,如果存在某一个点的入度和出度差值为奇数,那么这张图一定不存在欧拉回路,因为我们如果要找欧拉回路一定是通过反向无向边来使得每个点的入度等于出度,而每反向一条边,它所连接的两个点的入度和出度差值都改变了2...

那么对于一个入度不等于出度的点,我们需要把和它相邻的abs(in[i]-out[i])/2条边反向,所以对于一个in[i]>out[i]的点我们从i向T连一条容量为(in[i]-out[i])/2的边,对于一个out[i]>in[i]的点我们从S向i连一条容量为(out[i]-in[i])/2的边,然后我们把对于每个无向边,按照初始的定向连边,有向边删去(因为有向边是不能反向的)...如果可以满流就代表当前图满足要求...

代码:

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
//by NeighThorn
#define inf 0x3f3f3f3f
using namespace std; const int maxn=+,maxm=+; int n,m,S,T,cas,cnt,sum,flag,hd[maxn],fl[maxm],to[maxm],in[maxn],out[maxn],nxt[maxm],pos[maxn]; inline bool bfs(void){
memset(pos,-,sizeof(pos));
int head=,tail=,q[maxn];
q[]=S,pos[S]=;
while(head<=tail){
int top=q[head++];
for(int i=hd[top];i!=-;i=nxt[i])
if(pos[to[i]]==-&&fl[i])
pos[to[i]]=pos[top]+,q[++tail]=to[i];
}
return pos[T]!=-;
} inline int find(int v,int f){
if(v==T)
return f;
int res=,t;
for(int i=hd[v];i!=-&&f>res;i=nxt[i])
if(pos[to[i]]==pos[v]+&&fl[i])
t=find(to[i],min(f-res,fl[i])),res+=t,fl[i]-=t,fl[i^]+=t;
if(!res)
pos[v]=-;
return res;
} inline int dinic(void){
int res=,t;
while(bfs())
while(t=find(S,inf))
res+=t;
return res;
} inline void add(int s,int x,int y){
fl[cnt]=s;to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++;
fl[cnt]=;to[cnt]=x;nxt[cnt]=hd[y];hd[y]=cnt++;
} signed main(void){
// freopen("in.txt","r",stdin);
scanf("%d",&cas);
while(cas--){
flag=cnt=sum=;
scanf("%d%d",&n,&m);
memset(in,,sizeof(in));
memset(hd,-,sizeof(hd));
memset(out,,sizeof(out));
for(int i=,s,x,y;i<=m;i++){
scanf("%d%d%d",&x,&y,&s);
if(s==)
add(,x,y);
in[y]++,out[x]++;
}S=,T=n+;
for(int i=;i<=n&&!flag;i++){
if(abs(in[i]-out[i])&)
flag=;
else if(in[i]>out[i])
add((in[i]-out[i])>>,i,T);
else if(out[i]>in[i])
add((out[i]-in[i])>>,S,i),sum+=(out[i]-in[i])>>;
}
if(flag){
puts("impossible");continue;
}
if(dinic()==sum)
puts("possible");
else
puts("impossible");
}
return ;
}

By NeighThorn

POJ 1637 Sightseeing tour的更多相关文章

  1. POJ 1637 Sightseeing tour(最大流)

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

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

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

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

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

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

    Sightseeing tour   Description The city executive board in Lund wants to construct a sightseeing tou ...

  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——最大流+欧拉回路

    题目:http://poj.org/problem?id=1637 先给无向边随便定向,如果一个点的入度大于出度,就从源点向它连 ( 入度 - 出度 / 2 ) 容量的边,意为需要流出去这么多:流出去 ...

  9. poj 1637 Sightseeing tour —— 最大流+欧拉回路

    题目:http://poj.org/problem?id=1637 建图很妙: 先给无向边随便定向,这样会有一些点的入度不等于出度: 如果入度和出度的差值不是偶数,也就是说这个点的总度数是奇数,那么一 ...

随机推荐

  1. Properties操作指南

    一.简介: Properties是java中用的比较多的一个类,表示一个持久的属性集.继承于Hashtable,Properties可从流中加载,也可保存在流中.属性列表中每个键极其对应值共同组成一个 ...

  2. mongodb安装&简单使用

    转自Mac下使用brew安装mongodb,按着步骤已成功安装. brew常用命令 1.更新brew本身 brew update 2.使用brew安装软件 1 brew install soft_na ...

  3. Bash简明教程--变量

    1. 前言 Bash是一门流行在*nix系统下的脚本语言.作为一门脚本语言,变量是一门语言的基本要素,在这篇教程中,我们将学习Bash中的变量是怎么表示的,以及变量相关的一些语法规则. 2. Bash ...

  4. C#测试题

    阅读下面的程序,程序运行后hovertree值为( ) int x = 3, y = 4, z = 5;String s = "xyz";string hovertree = s ...

  5. [C#] 多线程总结(结合进度条)

    线程生命周期(来源 w3cschool) 未启动状态:当线程实例被创建但 Start 方法未被调用时的状况. 就绪状态:当线程准备好运行并等待 CPU 周期时的状况. 不可运行状态: 已经调用 Sle ...

  6. css设置table表格tr分离

    table { border-collapse:separate; border-spacing:10px 50px; }

  7. PHP unserialize()

    定义和用法 unserialize() 将已序列化的字符串还原回 PHP 的值. 序列化请使用 serialize() 函数. 语法 unserialize(str) 参数 描述 str 必需.一个序 ...

  8. Linux:JDK配置

    1.JDK官网下载"jdk-8u101-linux-i586.tar.gz",32位或64位. 2 命令 #创建jdk所在目录 sudo mkdir /usr/lib/jvm #找 ...

  9. 子div设置浮动无法把父div撑开。

    <div class="mainBox"> <div class="leftBox"></div> <div clas ...

  10. 面向对象设计模式纵横谈:Singelton单件模式(笔记记录)

       李建忠老师讲的<面向对象设计模式纵横谈>,早就看过了,现在有了时间重新整理一下,以前的博客[赛迪网]没有了,现在搬到博客园,重新过一遍,也便于以后浏览. 设计模式从不同的角度分类会得 ...