poj 1637 Sightseeing tour 混合欧拉图判定

POJ - 1637点我点我:-)
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %lld & %llu

Status

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


题意:给出一个n个节点,m条边的图,m条边的描述为0, 1,如果是1则是单向路,0是双向路,问是否存在欧拉回路

题解:这是混合欧拉图的判定,有一些不错的收获!

          我们原来做的欧拉路判定只是单向的,现在加了一些双向的边,我们这么办:

          1.判断出入度时,如果我们给双向边随意定一个方向,那我们就可以算出所有的点的出入度,我们发现不论是把双向边定义为哪个方向,都不会改变点的出入度之差(一个数量加一,一个数量减一)。如果有点的出入度之差为奇数,那么不存在欧拉回路(--->所有点的出入度之差为0

          2.但是双向路的方向还不能确定,我们的最终目的是把所有点的出入度之差变为0,我们需要调整路的方向看最终是否可以达到条件,方法是网络流: 我们把所有双向边的点按所定的初始方向加边,然后把入度小于出度的点与总源点连边,大小为(出度-入度)/2,  把出度小于入度的点与总汇点连边,大小为(入度-出度)/2, 这表示每个点的需调整量,算算此网络流是否满流,满流说明可以将所有点的出入度之差变为0,否则不行。


poj 1637 代码如下:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;

#define MAXN (200+5)
#define pb push_back
#define INF 0x3f3f3f3f
#define Set(a, v) memset(a, v, sizeof(a))
#define For(i, a, b) for(int i = (a); i <= (int)(b); i++)

struct Edge{
	int from, to, cap, flow;
};

int n, rm;

struct Dinic{
	int m, s, t;
	vector<Edge> edges;
	vector<int> G[MAXN];
	bool vis[MAXN];
	int d[MAXN], cur[MAXN];
	
	void init(){
		edges.clear();
		For(i, 0, MAXN-1) G[i].clear();
	}
	
	void AddEdge(int u, int v, int w){
		edges.pb((Edge){u, v, w, 0});	
		edges.pb((Edge){v, u, 0, 0});
		
		m = edges.size();
		G[u].pb(m-2); G[v].pb(m-1);
	};
	
	bool Bfs(){
		Set(vis, 0);
		queue<int> q;
		q.push(s);
		
		d[s] = 0; vis[s] = true;
		while(!q.empty()){
			int now = q.front(); q.pop();
			
			For(i, 0, G[now].size()-1){
				Edge e = edges[G[now][i]];
				if(!vis[e.to] && e.cap > e.flow){
					vis[e.to] = true;
					d[e.to] = d[now]+1;
					q.push(e.to);
				}
			}
		}
		
		return vis[t];
	}
	
	int Dfs(int now, int a){
		if(now == t || !a) return a;
		
		int flow = 0, f;
		for(int& i = cur[now]; i < G[now].size(); i++){
			Edge &e = edges[G[now][i]];
			if(d[now]+1 == d[e.to] && (f = Dfs(e.to, min(a, e.cap-e.flow))) > 0){
				e.flow += f;
				edges[G[now][i]^1].flow -= f;
				flow += f; a -= f;
				if(!a) break;
			}
		}
		
		return flow;
	}
	
	int Max_flow(int s, int t){
		this->s = s; this->t = t;
		int flow = 0;
		while(Bfs()){
			Set(cur, 0);
			flow += Dfs(s, INF);
		}
		return flow;
	}
}Din;

int in[MAXN], out[MAXN];

int get_map(){
	int ret = 0;
	For(i, 1, n){
		if(in[i] < out[i]) Din.AddEdge(0, i, (out[i]-in[i])/2);
		else{
			Din.AddEdge(i, n+1, (in[i]-out[i])/2);
			ret += (in[i]-out[i])/2;
		}
	}
	return ret;
}

void solve(){
	bool flag = true;
	For(i, 1, n)
	  if((in[i]-out[i]) % 2){
	      flag = false;
	      break;
	  }
	  
	if(!flag) printf("impossible\n");
	else{
		int sum = get_map();
		if(Din.Max_flow(0, n+1) == sum) printf("possible\n");
		else printf("impossible\n");
	}
}

int main(){
	int T;
	scanf("%d", &T);
	
	while(T--){
		Set(in, 0); Set(out, 0);
		Din.init();
		
		scanf("%d%d", &n, &rm);
		For(i, 1, rm){
			int u, v, w;
			scanf("%d%d%d", &u, &v, &w);
			in[v]++; out[u]++;
			
			if(!w) Din.AddEdge(u, v, 1);
		}
		
		solve();
	}
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值