Sightseeing tour
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9100   Accepted: 3830

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
【分析】

给出一张混合图(有有向边,也有无向边),判断是否存在欧拉回路。

首先是对图中的无向边随意定一个方向,然后统计每个点的入度(indeg)和出度(outdeg),如果(indeg - outdeg)是奇数的话,一定不存在欧拉回路;

如果所有点的入度和出度之差都是偶数,那么就开始网络流构图:

1,对于有向边,舍弃;对于无向边,就按照最开始指定的方向建权值为 1 的边(不一定是1,应该是这条边出现的次数,因为可能重边,我就是在这个地方WA了);

2,对于入度小于出度的点,从源点连一条到它的边,权值为(outdeg - indeg)/2;出度小于入度的点,连一条它到汇点的权值为(indeg - outdeg)/2 的边;

构图完成,如果满流(求出的最大流值 == 和汇点所有连边的权值之和),那么存在欧拉回路,否则不存在。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int power(int a,int b,int c){int ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
struct man
{
int c,f;
}w[N][N];
int dis[N],n,m;
int t,cnt,maxn,ans;
int in[N],out[N];
bool flag;
bool bfs()
{
queue<int>q;
memset(dis,,sizeof(dis));
q.push();
dis[]=;
while(!q.empty() && !dis[t]){
int v=q.front();q.pop();
for(int i=;i<=t;i++){
//if(i==t)printf("w[i][t].c=%d\n",w[i][t].c);
if(!dis[i]&&w[v][i].c>w[v][i].f){
q.push(i);
dis[i]=dis[v]+;
}
}
}
return dis[t]!=;
}
int dfs(int cur,int cp)
{
if(cur==t||cp==)return cp;
int tmp=cp,tt;
for(int i=;i<=t;i++){
if(dis[i]==dis[cur]+ &&w[cur][i].c>w[cur][i].f){
tt=dfs(i,min(w[cur][i].c-w[cur][i].f,tmp));
w[cur][i].f+=tt;
w[i][cur].f-=tt;
tmp-=tt;
}
}
return cp-tmp;
}
void dinic()
{
ans=;
while(bfs())ans+=dfs(,inf);
if(ans==maxn)puts("possible");
else puts("impossible");
} void init()
{
int a,b,d;
scanf("%d%d",&n,&m);t=n+;
for(int i=;i<=n;i++)in[i]=out[i]=;
while(m--){
scanf("%d%d%d",&a,&b,&d);
if(d==)w[a][b].c++;//有重边,若把它赋值为1,WA
out[a]++;in[b]++;
}
}
void solve()
{
flag=true;
for(int i=;i<=n;i++){
if((in[i]-out[i])&){
puts("impossible");flag=false;return;
}
if(in[i]<out[i])w[][i].c=(out[i]-in[i])/;
else if(in[i]>out[i])w[i][t].c=(in[i]-out[i])/,maxn+=(in[i]-out[i])/;
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(w,,sizeof(w));
maxn=;
init();
solve();
if(flag) dinic();
}
return ;
}

POJ1637 Sightseeing tour (混合图欧拉回路)(网络流)的更多相关文章

  1. poj1637 Sightseeing tour(混合图欧拉回路)

    题目链接 题意 给出一个混合图(有无向边,也有有向边),问能否通过确定无向边的方向,使得该图形成欧拉回路. 思路 这是一道混合图欧拉回路的模板题. 一张图要满足有欧拉回路,必须满足每个点的度数为偶数. ...

  2. poj1637 Sightseeing tour 混合图欧拉回路判定

    传送门 第一次做这种题, 尽管ac了但是完全不知道为什么这么做. 题目就是给一些边, 有向边与无向边混合, 问你是否存在欧拉回路. 做法是先对每个点求入度和出度, 如果一条边是无向边, 就随便指定一个 ...

  3. POJ 1637 Sightseeing tour ★混合图欧拉回路

    [题目大意]混合图欧拉回路(1 <= N <= 200, 1 <= M <= 1000) [建模方法] 把该图的无向边随便定向,计算每个点的入度和出度.如果有某个点出入度之差为 ...

  4. POJ1637:Sightseeing tour(混合图的欧拉回路)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10581   Accepted: 4466 ...

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

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

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

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

  7. poj1637Sightseeing tour(混合图欧拉回路)

    题目请戳这里 题目大意:求混合图欧拉回路. 题目分析:最大流.竟然用网络流求混合图的欧拉回路,涨姿势了啊啊.. 其实仔细一想也是那么回事.欧拉回路是遍历所有边一次又回到起点的回路.双向图只要每个点度数 ...

  8. HDU 3472 混合图欧拉回路 + 网络流

    九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/13799337 题意: T个测试数据 n串字符 能否倒过来用(1表示能倒着用) 问 ...

  9. poj1637 Sightseeing tour[最大流+欧拉回路]

    混合图的欧拉回路定向问题. 顺便瞎说几句,有向图定欧拉回路的充要条件是每个点入度等于出度,并且图联通.无向图的话只要联通无奇点即可. 欧拉路径的确定应该是无向图联通且奇点数0个或2个,有向图忘了,好像 ...

随机推荐

  1. HDU 3336 扩展kmp

    题目大意: 找到字符串中所有和前缀字符串相同的子串的个数 对于这种前缀的问题,通常通过扩展kmp来解决 其实吧这是我第一次做扩展kmp的题目,原来确实看过这个概念,今天突然做到,所以这个扩展kmp的模 ...

  2. 经典线程同步 关键段CS

    上一篇<秒杀多线程第四篇 一个经典的多线程同步问题>提出了一个经典的多线程同步互斥问题,本篇将用关键段CRITICAL_SECTION来尝试解决这个问题. 本文首先介绍下如何使用关键段,然 ...

  3. 后台框架--HUI 的学习跟使用1

    下载跟查看说明文档:官方 https://github.com/jackying/ 官网:http://www.h-ui.net/H-ui.admin.shtml 后台,http://www.h-ui ...

  4. [转]Putty中文乱码解决方法

    from: http://www.putty.ws/putty-luanma putty使用的过程中,你是否遇到过putty出现中文乱码的情况呢?putty终端出现乱码,是情况,多数是由于系统或软件缺 ...

  5. VMware Workstation 10.0.0.1295980 CN

    从V10版本开始,VMware Workstation 官方自带简体中文了,以后大家不需要汉化啦! Winner of more than 50 industry awards, VMware Wor ...

  6. Oracle GoldenGate 12c实时捕获SQL Server数据

    在Oracle GoldenGate 12c中,对一些最新的数据库提供了支持,比如SQL Server 2012/2014,当然12c也支持sql server 2008.主要新增特性有: 捕获进程可 ...

  7. string(Integer)类的equals和==区别和联系(验证密码的时候用得到)

    “==”在八种原始数据类型中,判断的是两边的值是否相等.对于对象类型来说,判断的是内存地址,所以为true所满足的条件就是两边的引用指向同一个对象. 比如String s1 = "abcde ...

  8. java.lang包的分类

    提供利用 Java 编程语言进行程序设计的基础类. 1>  最重要的类是 Object(它是类层次结构的根)和 Class(它的实例表示正在运行的应用程序中的类).   2>  把基本类型 ...

  9. poj3280 区间dp

    //Accepted 15880 KB 250 ms #include <cstdio> #include <cstring> #include <iostream> ...

  10. UIlabel设置不同的颜色

    NSString *string = @"注册过程中出现问题,致电400-650-5167联系会养车工作人员";    NSRange range = [string rangeO ...