天天看點

POJ 1637 Sightseeing tour(網絡流,混合歐拉回路)

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      
#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 1e9
#define maxn 250
#define maxm 5000
struct Edge
{
    int to,next,flow;
} e[maxm];
int d[maxn],n,m,head[maxn],cur[maxn],out[maxn],in[maxn],cnt,s,s1,t,mp[maxn][maxn];
void add(int u,int v,int flow)
{
    e[cnt].to=v;
    e[cnt].flow=flow;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
void init()
{
    cnt=0;
    s=0;
    t=n+1;
    memset(out,0,sizeof(out));
    memset(in,0,sizeof(in));
    memset(mp,0,sizeof(mp));
    memset(head,-1,sizeof(head));
}
int dfs(int now,int t,int lim)
{
    if(!lim||now==t)
        return lim;
    int flow=0,f=0;
    for(int i=cur[now];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(d[v]==d[now]+1&&(f=dfs(v,t,min(lim,e[i].flow))))
        {
            flow+=f;
            lim-=f;
            e[i].flow-=f;
            e[i^1].flow+=f;
            if(!lim) break;
        }
    }
    return flow;
}
int bfs(int s,int t)
{
    for(int i=0;i<=t;i++)
    {
        cur[i]=head[i];
        d[i]=0x7f7f7f7f;
    }
    queue<int>q;
    d[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
       if(head[u]!=-1) //printf("u=%d\n",u);
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;

            if(d[v]>inf&&e[i].flow)
            {//printf(" v=%d w=%d\n\n",v,e[i].flow);
                d[v]=d[u]+1;
                q.push(v);
            }
        }
    }
    if(d[t]<inf) return 1;
    else return 0;
}
int slove()
{
    int ans=0;
    while(bfs(s,t))
    {
        ans+=dfs(s,t,inf);
    }
    return ans;
}
bool Euler(int n)
{
	s1 = 0;
	for (int i = 1; i <= n; i++)
	{
		if (in[i] - out[i] > 0) s1 += (in[i] - out[i])/2;
		if ((in[i] - out[i]) % 2 != 0)
			return false;
	}
	return true;
}

int main()
{
    int t1;
    scanf("%d",&t1);
    while(t1--)
    {

        scanf("%d %d",&n,&m);
        init();
        for(int i=1; i<=m; i++)
        {
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            out[u]++;
            in[v]++;
            if(w==0)
                mp[u][v]+=1;
        }
        int i;s1=0;
        if(!Euler(n))
        {
            printf("impossible\n");
            continue;
        }
        for(i=1; i<=n; i++)
        {
            //if((in[i]-out[i])%2!=0) break;
            if(in[i]>out[i])
            {
                //printf("u=%d v=%d w=%d\n",s,i,(in[i]-out[i])/2);
                add(s,i,(in[i]-out[i])/2);
                add(i,s,0);
                //s1+=(in[i]-out[i])/2;
            }
            else if (out[i] > in[i])
                {
                    //printf("u=%d v=%d w=%d\n",i,t,(out[i]-in[i])/2);
                    add(i,t,(out[i]-in[i])/2);
                    add(t,i,0);
                }
            for (int j = 1; j <= n; j++)
            {
                if (mp[i][j])
                    {
                        //printf("u=%d v=%d w=%d\n",j,i,mp[j][i]);
                        add(j, i, mp[i][j]);
                        add(i,j,0);
                    }
            }
        }
        if(i<=n)
        {
            printf("impossible\n");
            continue;
        }
        int ans=slove();
        //printf("s1=%d ans=%d\n",s1,ans);
        if(s1==ans)
        {
            printf("possible\n");
        }
        else printf("impossible\n");
    }
}