混合图欧拉回路 POJ 1637


Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7741 Accepted: 3242

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

Source


先随意规定无向边的方向,统计混合图中每个点的出入度,若有一个点的出入度只差为奇数,则不存在欧拉回路,应为无论怎样改变开始时规定的无向图方向,都不可能使改点的出入度之差变成0,来满足欧拉回路的条件。

如果每个点的出入度之差都为偶数,就需要判断有没有可能通过改变初始时规定的无向边方向来使得每个点的出入度之差变成0。

对于每个出度大于入度的节点,从源点s向该点连边,容量为该点出入度之差的一半;对于每个入度大于出度的节点,从该点想汇点t连边,容量为该点出入度之差的一半。并且加入所有刚开始时已经定向的无向边(有向边不加入,应为它们不能反转)按照这样构图后,每一个流都对应了一个改变无向边方向的方案,如果满流,意味着原图存在欧拉回路。

那么此时欧拉图是什么?只需把最大流的网络中的每个有流量的边反转,就是欧拉图。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <ctype.h>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>

#define MAXN 220
#define MAXM 2050
#define INF 2100000000
#define pu system("pause")

#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;

class Edge
{
    public:
    int u, v, next, w;
};
Edge edge[MAXM];
int cnte;

int n, m, head[MAXN], in[MAXN], out[MAXN], x[MAXN];

int fa[MAXN], cur[MAXN], gap[MAXN], dis[MAXN];

void insert(int u, int v, int w)
{
    edge[cnte].u = u; edge[cnte].v = v;
    edge[cnte].w = w; edge[cnte].next = head[u]; head[u] = cnte++;

    edge[cnte].u = v; edge[cnte].v = u;
    edge[cnte].w = 0; edge[cnte].next = head[v]; head[v] = cnte++;
}


int sap(int s, int t, int n)
{
	memset(fa, -1, sizeof(fa));
	memcpy(cur, head, sizeof(head));//初始时当前弧就是当前节点的第一条弧

	//不使用bfs时加上这三条
	memset(dis, 0, sizeof(dis));
	memset(gap, 0, sizeof(gap));
	gap[0] = n;//初始时距离为0的节点有n个
	//bfs(t, n);

	int lowf = INF, top = s, flow = 0;//lowf是增广路上的最小流量,top是增广过程最前端的节点,flow待返回的流量

	while(dis[s] < n)//当汇点不可达时,dis[s]的值会被更改为节点总数n
	{
		bool flag = 0;//标记通过top节点能否找到允许弧

		for(int i = cur[top]; i != -1; i = edge[i].next)//从当前弧开始找允许弧
		{
			int v = edge[i].v;
			if(dis[top] == dis[v]+1 && edge[i].w > 0)//找到允许弧
			{
				flag = 1;//更改标记
				cur[top] = i;//更改当前节点的当前弧,下次搜索时从这条弧开始
				lowf = min(lowf, edge[i].w);//更新增广路上的流量
				fa[v] = top;//记录父节点
				top = v;//更改top节点

				if(top == t)//如果找到终点,说明找到一条增广路,更新总流量
				{
					flow += lowf;

					while(top != s)//沿父节点回溯更新残余网络
					{
						top = fa[top];

						edge[cur[top]].w -= lowf;
						edge[cur[top]^1].w += lowf;

					}

					lowf = INF;//重置最小流量
				}
				break;
			}

		}

		//如果没找到允许弧,撤退,更改当前top节点的距离值

		int mindis;//更改top节点的距离值
		if(!flag)
		{
			mindis = n;//初始化为节点总数

			for(int i = head[top]; i != -1; i = edge[i].next)
			{
				if(edge[i].w > 0 && dis[edge[i].v] < mindis)//如果top节点能从距离比当前节点的距离更小的节点转移来
				{
						mindis = dis[edge[i].v];//更新top节点的距离值
						cur[top] = i;//修改top节点的当前弧
				}
			}
			if((--gap[dis[top]]) == 0) break;//gap优化,如果节点距离值出现断层,必然找不到增广路,直接退出
			gap[dis[top] = mindis+1]++;//更新top节点的距离值以及gap数组

			if(top != s) top = fa[top];//top撤退到它的父节点
		}

	}
	return flow;
}

int main()
{
	//freopen("C:/Users/zts/Desktop/in.txt", "r", stdin);
    int t; scanf("%d", &t);

    while(t--)
    {
        memset(head, -1, sizeof(head));
        memset(in, 0, sizeof(in));
        memset(out, 0, sizeof(out));
        cnte = 0;

        scanf("%d%d", &n, &m);

        int u, v, kind;
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &u, &v, &kind);

            if(!kind) insert(u, v, 1);

            in[v]++; out[u]++;
        }

        bool ok = 1;
        for(int i = 1; i <= n; i++)
        {
            if(abs(out[i]-in[i])&1)
            {
                ok = 0;
                break;
            }
            else
            {
                x[i] = (out[i]-in[i])/2;
            }
        }

        if(!ok)
        {
            printf("impossible\n");
            continue;
        }

        int s = 0, e = n+1;

        int flowin = 0, flowout = 0;

        bool allzero = 1;
        for(int i = 1; i <= n; i++)
        {
            if(x[i] > 0) insert(s, i, abs(x[i])), flowin += abs(x[i]), allzero = 0;

            else if(x[i] < 0) insert(i, e, abs(x[i])), flowout += abs(x[i]), allzero = 0;
        }

        if(allzero)
        {
            printf("possible\n");
            continue;
        }

        int ans = sap(s, e, e+1);

        if(ans == flowin && ans == flowout) printf("possible\n");
        else printf("impossible\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值