【最大流+混合图欧拉回路】POJ-1637 Sightseeing tour

34 篇文章 0 订阅
30 篇文章 0 订阅
Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000K
   

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
————————————————————灰心的分割线————————————————————
前言:关于混合图的欧拉回路问题,夏天的风解释得很详细。
思路:有向边不必理会,因为是固定的。对于无向边,暂时将其当做单向边,统计出每个点的度数。
一旦某个点是奇度点,一定不存在欧拉回路。因为该点不可能通过调整边的方向使得入度 = 出度。
如果不存在奇度点,那么用网络流来判断能否通过修改边得到欧拉回路。
对于之前人为定向的单向边,容量为1,表示可以修改一个出度。
对于出度 > 入度的点,将源点与它相连,容量为可以减少的出度。((出度 - 入度) / 2)
对于入度 > 出度的点,将它与汇点相连,容量为可以增加的出度。((入度 - 出度) / 2)
这样一来,一旦可以满流(流入 = 流出),说明所有需要修改的度都修改了。入度 = 出度。也就是说存在欧拉回路。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 222, M = 1e4;
struct Node {
	int u, v, cap;
	int next;
}edge[M];
int tot, head[N], lev[N], cur[N], q[N], s[N], ind[N], oud[N], dif[N], dir[N], n, m, S, T;
bool vis[N];


void init()
{
	tot = 0; memset(head, -1, sizeof(head));
}

void add(int u, int v, int c)
{
	edge[tot].u = u; edge[tot].v = v; edge[tot].cap = c;
	edge[tot].next = head[u]; head[u] = tot++;
}

bool judge()
{
	for(int i = 1; i <= n; i++) if(vis[i]) {
		if((oud[i] + ind[i]) & 1) 
			return false;
		dif[i] = (oud[i] - ind[i]) >> 1;
	}
	return true;
}

bool bfs()
{
	memset(lev, -1, sizeof(lev));
	int fron = 0, rear = 0;
	q[rear++] = S; lev[S] = 0;
	while(fron < rear) {
		int u = q[fron%N]; fron++;
		for(int i = head[u]; i != -1; i = edge[i].next) {
			int v = edge[i].v;
			if(edge[i].cap && lev[v] == -1) {
				lev[v] = lev[u] + 1;
				q[rear%N] = v; rear++;
				if(v == T) return true;
			}
		}
	}
	return false;
}

int Dinic()
{
	int ret = 0;
	while(bfs()) {
		memcpy(cur, head, sizeof(head));
		int top = 0, u = S;
		while(1) {
			if(u == T) {
				int mini = INF, loc;
				for(int i = 0; i < top; i++) {
					if(mini > edge[s[i]].cap) {
						mini = edge[s[i]].cap; 
						loc = i;
					}
				}
				for(int i = 0; i < top; i++) {
					edge[s[i]].cap -= mini;
					edge[s[i]^1].cap += mini;
				}
				ret += mini;
				top = loc;
				u = edge[s[top]].u;
			}
			int &i = cur[u];
			for(; i != -1; i = edge[i].next) {
				int v = edge[i].v;
				if(edge[i].cap && lev[v] == lev[u] + 1) break;
			}
			if(i != -1) {
				s[top++] = i;//新的允许弧入栈
				u = edge[i].v;
			}
			else {
				if(!top) break;
				lev[u] = -1;//阻塞流
				u = edge[s[--top]].u;
			}
		}
	}
	return ret;
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	int kase;
	scanf("%d", &kase);
	while(kase--) {
		memset(ind, 0, sizeof(ind));
		memset(oud, 0, sizeof(oud));
		memset(vis, 0, sizeof(vis));
		scanf("%d%d", &n, &m);
		S = 0; T = n+1;
		int u, v, dir;
		init();
		for(int i = 1; i <= m; i++) {
			scanf("%d%d%d", &u, &v, &dir);
			vis[u] = vis[v] = 1;
			oud[u]++; ind[v]++;
			if(!dir) add(u, v, 1), add(v, u, 0);
		}
		bool flag = judge();
		if(flag) {
			int max_flow = 0;
			for(int i = 1; i <= n; i++) if(vis[i]) {
				if(oud[i] > ind[i]) {//对于出大于入的点,可以提供一个修改边
					add(S, i, dif[i]); add(i, S, 0);
					max_flow += dif[i];
				}
				else if(ind[i] > oud[i]) {
					add(i, T, -dif[i]); add(T, i, 0);
				}
			}
			int ret = Dinic();
			if(ret == max_flow) puts("possible");
			else puts("impossible");
 		}
		else {
			puts("impossible");
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值