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. iOS-生成国际化包-配置App多语言支持

      标签: ios国际化 ios多语言支持 xcode多语言支持 xcode生成多语言 国际化 it 分类: 功能知识   如果你的App需要支持多国语言.那么,就应该为你的App应用添加“国际化”支 ...

  2. Unity场景道具模型拓展自定义编辑器

    (一)适用情况 当游戏主角进入特定的场景或者关卡,每个关卡需要加载不同位置的模型,道具等.这些信息需要先在unity编辑器里面配置好,一般由策划干这事,然后把这些位置道具信息保存在文件,当游戏主角进入 ...

  3. Java基础毕向东day05 对象与对象的区别,匿名内部类,函数的执行流程。

    1.Car c = new Car(); Car c2 = new Car(); 1> c 和 c2之间的区别? public static void main(String[] args) { ...

  4. Ogre中TerrainSceneManager

    转自:http://blog.csdn.net/yanonsoftware/article/details/1103665 TerrainSceneManager是一个OctreeSceneManag ...

  5. atomic和nonatomic的区别

    1.使用atomic进行修饰的属性,在实现文件中不能重写setter方法和getter方法,不然在编译过程会发生报错. 1.使用nonatomic进行修饰的属性,在实现文件中可以重写setter方法和 ...

  6. win下隐藏任务栏

    C# 隐藏任务栏开始按钮 关闭shell 分类: .NET C# 2011-07-14 15:26 789人阅读 评论(1) 收藏 举报 shell任务c#stringnulluser 一.隐藏任务栏 ...

  7. 查看ubuntu文件目录的大小和文件夹包含的文件数 zT

    查看ubuntu文件目录的大小和文件夹包含的文件数 查看linux文件目录的大小和文件夹包含的文件数 统计总数大小 du -sh xmldb/ du -sm * | sort -n //统计当前目录大 ...

  8. AmazeUI基本样式

    AmazeUI是一个轻量级.Mobile first的前端框架,基于开源社区流行的前端框架编写. Normalize AmazeUI使用了normalize.css,但做了些调整:html添加了-we ...

  9. iOS:开发者中心证书创建流程

    一,首先点击开发者首页(https://developer.apple.com/)里面的Member Center.二,输入开发者账号和密码,点击sign in登录.三,点击Certificates, ...

  10. Channel Allocation_四色定理

    Description When a radio station is broadcasting over a very large area, repeaters are used to retra ...