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. 全国行政区划代码(json对象版)

    var area = {"110000":"北京市","110100":"北京市","110101" ...

  2. vi编辑器选项

    Vi编辑器有一些选项设置可以帮助人们更好的使用. 在vi中选项分为两种: 1.  开关选项,如果要打开这类选项就使用ex命令——:set 选项:如果要关闭这类选项就是用ex命令——:set  no选项 ...

  3. 在VS2010中打开VS2012的项目

    修改工程文件来把VS2012的工程文件移植到VS2010中 首先是修改解决方案文件(.sln文件). 使用记事本打开,把里面的 Microsoft Visual Studio Solution Fil ...

  4. 【python】import 模块、包、第三方模块

    xx.py文件,称为模块(module),把不同模块归整到一起的文件夹,叫做包(package) 不同包下的模块可以重名,但是都不能和系统内建模块重名 包里面一定要有个__init__.py文件,否则 ...

  5. XAMPP启动mysql遇到的问题

    Version: '10.1.9-MariaDB' socket: '' port: 3306 mariadb.org binary distribution2016-07-18 10:42:04 1 ...

  6. C++ primer的第二章的主要内容

    这第二章主要是介绍了C++中基本的内置数据类型:整型与浮点型.介绍了什么是变量的过程中了解到了左值与右值的概念.左值是可以出现在赋值语句的左边或者右边,也就是说可以放在等号的左右两边,而右值只能是出现 ...

  7. 《Java实验四》

    //实验4--附录一代码 public class PassValueTest { //静态代码块,类一加载就执行的部分. //所以运行这个程序会输出 class loding static { Sy ...

  8. UNICODE字符集(20140520)

    1多字节字符集,如"IT学吧",sizeof内存长度为7,因为前面2个字母各占用一个字节,后面两个汉字各占用2个字节,结尾的\0占用一个字节.strlen即字符串长度的结果为6. ...

  9. [super init]方法的调用

    当重新覆盖父类的init方法时,需要调用[super init]方法确认父类中的init是返回一个实例,而不是一个空的实例. 那为什么要调用这个呢? 我得猜测是这样的:因为这是一个初始化方法,需要对对 ...

  10. 51 nod 机器人走方格

    从一个长方形的方格的右上角 走到 左下角 , 问一共有多少种不同的路线可以达到 . #include<stdio.h> #include<string.h> #include& ...