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

对于混合图我们可以把一条无向边随便定向

再进行判断若一个点的入度和出度之差的绝对值是奇数那么这个这个图一定不是欧拉回路

因为改变一条无向边的的方向对差的影响是2,所以对无向边的定向并不能影响我们的判断

确定全是偶数之后,我们就可以通过改有向图边的方向来判断是否能够组成欧拉回路

#define LOCAL
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
int IN[205];
int OUT[205];

//0 源点 cnt+1汇点
const int INF = 0x7fffffff;
int level[405];
int cnt=1;
int m;
 
struct Dinic
{
    int c;
    int f;
} edge[405][405];
 
bool dinic_bfs()      //bfs方法构造层次网络
{
    //cout<<"level"<<endl;
    queue<int> q;
    memset(level, 0, sizeof(level));
    q.push(0);
    level[0] = 1;
    int u, v;
    while (!q.empty())
    {
        u = q.front();
        q.pop();
        for (v = 1; v <= cnt+1; v++)
        {
            if (!level[v] && edge[u][v].c>edge[u][v].f)
            {
                level[v] = level[u] + 1;
                q.push(v);
            }
        }
    }
    return level[cnt+1] != 0;                //question: so it must let the sink node is the Mth?/the way of yj is give the sink node's id
}
 
int dinic_dfs(int u, int cp)             //use dfs to augment the flow
{
    int tmp = cp;
    int v, t;
    if (u == cnt+1)
        return cp;
    for (v = 1; v <= cnt+1&&tmp; v++)
    {
        if (level[u] + 1 == level[v])
        {
            if (edge[u][v].c>edge[u][v].f)
            {
                t = dinic_dfs(v, min(tmp, edge[u][v].c - edge[u][v].f));
                edge[u][v].f += t;
                edge[v][u].f -= t;
                tmp -= t;
            }
        }
    }
    return cp - tmp;
}
 
int dinic()
{
    int sum=0, tf=0;
    while (dinic_bfs())
    {
        while (tf = dinic_dfs(0, INF))
            sum += tf;
    }
    return sum;
}


int main()
{
    #ifdef LOCAL
    //freopen("in.txt","r",stdin);
    #endif
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(edge,0,sizeof(edge));
        memset(IN,0,sizeof(IN));
        memset(OUT,0,sizeof(OUT));
        scanf("%d%d",&cnt,&m);
        for(int i=1;i<=m;i++)
        {
            int tmp1,tmp2,tmp3;
            scanf("%d%d%d",&tmp1,&tmp2,&tmp3);
            IN[tmp2]++;
            OUT[tmp1]++;
            if(tmp3==0)
            {
                edge[tmp1][tmp2].c+=1;
            }
        }
        int flag=0;
        int num=0;
        for(int i=1;i<=cnt;i++)
        {
            if(abs(IN[i]-OUT[i])%2==1)
            {
                flag=-1;
                break;
            }
            else 
            {
                if(IN[i]>OUT[i])
                {
                    edge[i][cnt+1].c+=(IN[i]-OUT[i])/2;
                }
                if(IN[i]<OUT[i])
                {
                    edge[0][i].c+=(OUT[i]-IN[i])/2;
                    num+=(OUT[i]-IN[i])/2;
                }
            }
        }
        if(flag==0&&dinic()==num)
        {
            printf("possible\n");
        }
        else 
        {
            printf("impossible\n");
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值