POJ1637 Sightseeing tour (混合图欧拉回路)(网络流)

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=405;
const int M=300005;
int power(int a,int b,int c){int ans=1;while(b){if(b%2==1){ans=(ans*a)%c;b--;}b/=2;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,0,sizeof(dis));
    q.push(0);
    dis[0]=1;
    while(!q.empty() && !dis[t]){
        int v=q.front();q.pop();
        for(int i=1;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]+1;
            }
        }
    }
    return dis[t]!=0;
}
int dfs(int cur,int cp)
{
    if(cur==t||cp==0)return cp;
    int tmp=cp,tt;
    for(int i=1;i<=t;i++){
        if(dis[i]==dis[cur]+1 &&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=0;
    while(bfs())ans+=dfs(0,inf);
    if(ans==maxn)puts("possible");
    else puts("impossible");
}

void init()
{
    int a,b,d;
    scanf("%d%d",&n,&m);t=n+1;
    for(int i=1;i<=n;i++)in[i]=out[i]=0;
    while(m--){
        scanf("%d%d%d",&a,&b,&d);
        if(d==0)w[a][b].c++;//有重边,若把它赋值为1,WA
        out[a]++;in[b]++;
    }
}
void solve()
{
    flag=true;
    for(int i=1;i<=n;i++){
        if((in[i]-out[i])&1){
            puts("impossible");flag=false;return;
        }
        if(in[i]<out[i])w[0][i].c=(out[i]-in[i])/2;
        else if(in[i]>out[i])w[i][t].c=(in[i]-out[i])/2,maxn+=(in[i]-out[i])/2;
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        memset(w,0,sizeof(w));
        maxn=0;
        init();
        solve();
        if(flag) dinic();
    }
    return 0;
}

时间: 2024-10-13 00:41:29

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

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

传送门 第一次做这种题, 尽管ac了但是完全不知道为什么这么做. 题目就是给一些边, 有向边与无向边混合, 问你是否存在欧拉回路. 做法是先对每个点求入度和出度, 如果一条边是无向边, 就随便指定一个方向, 然后连一条边, 权值为1. 最后统计入度出度, 如果一个点的(入度-出度)%2==1, 就说明不存在欧拉回路. 如果全都满足, 就判断每个点的入度出度的大小关系, 入度>出度, 就向汇点连一条边, 权值为(入度-出度)/2, 相反的话就向源点连边. 跑一遍最大流, 看是否满流, 如果满流就说

POJ 1637 Sightseeing tour 混合图欧拉回路存在性判断

没有想到网络流还能解决这一类问题,完全想不到@[email protected] 一开始把所有的无向边制定任意方向有当做有向边看,然后统计每个点的入度和出度.以前有向图的欧拉回路判定是每个点的入读都等于出度,这样可以保证可以回到起点,现在在一些边可以调换方向的情况下,所有定点的入度和出度之差必定为偶数,因为调换任意一条边的方向都会使两个定点的入度和出度变化2,所以要构成一个欧拉回路所有点的入度和出度之差都为偶数,并设差为deg. 现在问题转化成了能否通过改变一些边的方向来是的所有点的入度出度都为

POJ 1637 Sightseeing tour (混合图欧拉回路,网络最大流)

http://poj.org/problem?id=1637 Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7498   Accepted: 3123 Description The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can

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

Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10581   Accepted: 4466 题目链接:http://poj.org/problem?id=1637 Description: The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that touri

POJ 1637 Sightseeing tour 混合图欧拉回路 最大流

题目大意:给出一张混合图,问是否存在欧拉回路. 思路:成题,直接看题解吧. http://www.cnblogs.com/Lyush/archive/2013/05/01/3052847.html CODE: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 510 #define M

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

Sightseeing tour 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 visit

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

题目链接 建个图,套个模板. #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <algorithm> #include <vector> #include <string> #include <queue> using namespace std; #define INF 0x3ffffff str

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

混合图的欧拉回路定向问题. 顺便瞎说几句,有向图定欧拉回路的充要条件是每个点入度等于出度,并且图联通.无向图的话只要联通无奇点即可. 欧拉路径的确定应该是无向图联通且奇点数0个或2个,有向图忘了,好像复杂一点,这个真考到就暴力瞎搜吧. 既然每个点的度数都定了,又入度等于出度,那两者对半分,在二分图里左向右连上原图的边,左点集与s连容量为待补充的出度,右点集反之.这样如果我真可以定下来的话,就会有左边所有连边都满流.所以跑最大流看能不能到满流(就是差的总出度)即可. 1 #include<iost

【POJ1637】Sightseeing tour 混合图求欧拉回路存在性 网络流、

题意:多组数据,最后的0/1表示0无向1有向. 问是否存在欧拉回路. 题解:无向边给它任意定个向. 首先欧拉回路中点入度=出度. 然后发现每个无向边如果修改个方向,原来的入点的入度+1,出度-1,出点反之. 然后我们不妨对入度和出度不同的点跟源汇中之一连边,容量为入出度差一半(每改一条边差-2) 然后原来的无向边联系图中各点,容量1,最后check if(maxflow==sum差/4). 这都没看懂的弱菜们不妨出度多的连源,入度多的连汇,容量为入出度差一半,然后按无向边的定向给它在网络流图中定