【混合图欧拉回路】

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<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 1011
#define INF 0x3f3f3f3f
int map[MAXN][MAXN];
int in[MAXN],out[MAXN];
int a[MAXN],book[MAXN];
int f[MAXN];
int EK(int n)
{
    
    
    int i,sum=0,flog;
    while(1)
    {
    
    
        memset(a,0,sizeof(a));
        memset(book,0,sizeof(book));
        queue<int> q;
        f[0]=INF;
        q.push(0);
        flog=0;
        while(q.size()&&!flog)
        {
    
    
            int k=q.front();
            q.pop();
            for(i=1; i<=n; i++)
            {
    
    
                if(book[i]==0&&map[k][i]>0)
                {
    
    
                    book[i]=1;
                    a[i]=k;
                    f[i]=min(map[k][i],f[k]);
                    q.push(i);
                    if(i==n)
                    {
    
    
                        flog=1;
                        break;
                    }
                }
            }
        }
        if(flog==0)
            break;
        for(i=n; i!=0; i=a[i])
        {
    
    
            int l=a[i];
            map[l][i]-=f[n];
            map[i][l]+=f[n];
        }
        sum=sum+f[n];
    }
    return sum;
}
int main()
{
    
    
    int q;
    scanf("%d",&q);
    while(q--)
    {
    
    
        memset(map,0,sizeof(map));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        int n,m,flog=1,num=0,i;
        scanf("%d%d",&n,&m);
        while(m--)
        {
    
    
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            in[b]++;
            out[a]++;
            if(c==0)
            {
    
    
                map[a][b]++;
            }
        }
        for(i=1; i<=n; i++)
        {
    
    
            if(abs(in[i]-out[i])%2==1)
            {
    
    
                flog=0;
                break;
            }
            else
            {
    
    
                if(in[i]>out[i])
                {
    
    
                    map[i][n+1]+=abs(in[i]-out[i])/2;
                }
                else if(in[i]<out[i])
                {
    
    
                    map[0][i]+=abs(in[i]-out[i])/2;
                    num+=abs(in[i]-out[i])/2;
                }
            }

        }
        if(flog&&num==EK(n+1))
            printf("possible\n");
        else
            printf("impossible\n");
    }
}

猜你喜欢

转载自blog.csdn.net/m0_46312382/article/details/108992850