poj1637

Sightseeing tour

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7796   Accepted: 3264

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

netflow:首先要使这个图为欧拉回路,需满足:所有点的入度等于出度,这样就比较好建模了。

我们对于无向边按照输入的顺度定向,这样有可能造一些点不满足条件,我们发现对于一个点,如果它出入度之差为z,那么通过这个点的边至少需要修改z/2条,那么对于一个点,如果入度大于出变,建边(s,i,z/2),出度大于入度则建边(i,t,z/2),对于某条无向边,如果开始定义的方向为x->z,建边(z,x,1),(注意,只建(z,x,1),因为x->z贡献了z的入度,所以改这条边是减少z的入度,再加出我们对于入度大于点连s,所以这条边只能由z流向x。

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>

using namespace std;

int dis[2011],g[2011],que[2011],d[2011];
int next[23411],y[23411],flow[23411];
int tt,data,tl,n,m,tot,x,z,kind,i,j,xzq,sum,s,t;
bool pl;

void star(int i,int j,int k)
{
    tt++;
    next[tt]=g[i];
    g[i]=tt;
    y[tt]=j;
    flow[tt]=k;
    tt++;
    next[tt]=g[j];
    g[j]=tt;
    y[tt]=i;
    flow[tt]=0;
}

void Bfs()
{
    int l,r,x,j,k;
    memset(dis,255,sizeof(dis));
    dis[s]=0;
    que[l=r=1]=s;
    while(l<=r){
        x=que[l];
        j=g[x];
        while(j!=0){
            k=y[j];
            if(flow[j]>0&&dis[k]==-1){
                r++;
                que[r]=k;
                dis[k]=dis[x]+1;
            }
            j=next[j];
        }
        l++;
    }
}

int Dfs(int x,int fl)
{
    if(x==t)return fl;
    int j,k,z,e;
    z=0;
    j=g[x];
    while(j!=0){
        k=y[j];
        if(flow[j]>0&&dis[k]==dis[x]+1){
            e=Dfs(k,min(flow[j],fl));
            z+=e;
            fl-=e;
            flow[j]-=e;
            flow[j^1]+=e;
            if(!fl)return z;
        }
        j=next[j];
    }
    dis[x]=-1;
    return z;
}

void Dinic()
{
    while(true){
        Bfs();
        if(dis[t]==-1)break;
        xzq+=Dfs(s,0x7fffffff);
    }
}

int main()
{
    scanf("%d",&data);
    for(tl=1;tl<=data;tl++){
        memset(g,0,sizeof(g));
        memset(d,0,sizeof(d));
        tt=1;
        scanf("%d%d",&n,&m);
        for(i=1;i<=m;i++){
            scanf("%d%d%d",&x,&z,&kind);
            if(x==z)continue;
            if(kind==1){
                d[z]++;
                d[x]--;
            }
            else{
                d[z]++;
                d[x]--;
                star(z,x,1);
            }
        }
        s=n+1;
        t=n+2;
        xzq=0;
        sum=0;
        pl=true;
        for(i=1;i<=n;i++){
            if(abs(d[i])%2!=0)pl=false;
            if(d[i]>0){
                star(s,i,d[i]/2);
                sum+=d[i]/2;
            }
            if(d[i]<0)star(i,t,-d[i]/2);
        }
        Dinic();
        if(xzq!=sum||pl==false)printf("impossible\n");
        else printf("possible\n");
    }
}
时间: 2024-07-31 14:32:53

poj1637的相关文章

poj1637 Sightseeing tour,混合图的欧拉回路问题,最大流解

混合图的欧拉回路问题 题目地址 欧拉回路问题 1 定义 欧拉通路 (Euler tour)--通过图中每条边一次且仅一次,并且过每一顶点的通路. 欧拉回路 (Euler  circuit)--通过图中每条边一次且仅一次,并且过每一顶点的回路. 欧拉图--存在欧拉回路的图.  2 无向图是否具有欧拉通路或回路的判定  G有欧拉通路的充分必要条件为:G 连通,G中只有两个奇度顶点(它们分别是欧拉通路的两个端点). G有欧拉回路(G为欧拉图):G连通,G中均为偶度顶点.  3 有向图是否具有欧拉通路或

poj1637 混合图欧拉回路的求解 网络流

题目链接: POJ1637 题意: 一幅图 ,给出有向边和无向边,问是否有经过所有边仅一次的欧拉回路 解题思路: 混合图欧拉回路的求解需要用到网络流,具体的建模方法如下: 1.先给所有无向边定向,然后统计所有点的入度和出度, 2.如果某点   入度-出度=奇数  那么一定不能构成欧拉回路   //入度+x  出度-x  度数差奇偶性不变 3.如果某点   出度>入度  建一条与源点连接的边  边容量为 (出度-入度)/2; 如果某点   出度<入度  建一条与汇点连接的边  边容量为 (入度-

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

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

poj1637 混合欧拉回路的判定

参考下面的解释: [混合图]混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定不存在欧拉环或欧拉路径(不考虑度数为0的点). 其实,难点在于图中的无向边,需要对所有的无向边定向(指定一个方向,使之变为有向边),使整个图变成一个有向欧拉图(或有向半欧拉图).若存在一个定向满足此条件,则原图是欧拉图(或半欧拉图)否则不是.关键就是如何定向? 首先给原图中的每条无向边随便指定一个方向(称为初始定向),将原图

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 beauti

ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)

//网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Memory:348K #include <iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> using namespace std; #de

POJ1637 Sightseeing tour(判定混合图欧拉回路)

有向连通图存在欧拉回路的充要条件是所有点入度=出度. 首先随便给定所有无向边一个方向(不妨直接是u->v方向),记录所有点的度(记:度=入度-出度). 这时如果有点的度不等于0,那么就不存在欧拉回路,就需要改变那些无向边的方向. 而改变一个无向边的方向,相当于边上两个端点的入度和出度都变化了1,它们的度±2. 另外,这样可以证明如果这时某个点的度为奇数那么一定不存在存在欧拉回路的解. 构图如下:所有无向边(u,v),建立容量为1的(u,v)边:所有度小于0的点u,建立容量为-deg/2的(vs,

【POJ1637】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 visited exactly once.

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

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