POJ 1637:Sightseeing tour 混合图欧拉回路

来源:互联网 发布:linux init命令 编辑:程序博客网 时间:2024/05/01 19:11

Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 8768 Accepted: 3684

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

45 82 1 01 3 04 1 11 5 05 4 13 4 04 2 12 2 04 41 2 12 3 03 4 01 4 13 31 2 02 3 03 2 03 41 2 02 3 11 2 03 2 0

Sample Output

possibleimpossibleimpossiblepossible

题意是求混合图的欧拉回路,要求就是每个点的入度=出度,考虑如何判断最终能够达到每一个点的入度=出度,使用网络流来判定。源点与i节点相连,代表出度大于入度,节点j与汇点相连,代表出度大于入度。这时如果有一条增广路经s->i->j->t,就代表了i到j的边转换了方向,i的出度-1,入度+1,j的入度+1,出度-1。这样发现,如果能够满流的情况下,就能够使得每一个点的入度=出度了。

代码:

#pragma warning(disable:4996)#include <iostream>#include <functional>#include <algorithm>#include <cstring>#include <vector>#include <string>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <deque>#include <set>#include <map>using namespace std;typedef long long ll;#define INF 0x333f3f3f#define repp(i, n, m) for (int i = n; i <= m; i++)#define rep(i, n, m) for (int i = n; i < m; i++)#define sa(n) scanf("%d", &(n))const ll mod = 1e9 + 7;const int maxn = 1e3 + 5;const double PI = acos(-1.0);struct ed{int to;int cap;int flow;int next;}edge[4 * maxn];int edgen;int out[maxn], in[maxn];int stac[maxn], head[maxn], dis[maxn], vis[maxn], pre[maxn];void addedge(int u, int v, int cap){edge[edgen].to = v;edge[edgen].cap = cap;edge[edgen].flow = 0;edge[edgen].next = head[u];head[u] = edgen++;edge[edgen].to = u;edge[edgen].cap = 0;edge[edgen].flow = 0;edge[edgen].next = head[v];head[v] = edgen++;}void init(){edgen = 0;memset(head, -1, sizeof(head));memset(edge, -1, sizeof(edge));memset(out, 0, sizeof(out));memset(in, 0, sizeof(in));}bool bfs(int S, int T){int i, j, k, h, t;memset(dis, -1, sizeof(dis));dis[S] = 0;stac[0] = S;t = 1;for (h = 0; h < t; h++){for (k = head[stac[h]]; k != -1; k = edge[k].next){j = edge[k].to;if (dis[j] == -1 && edge[k].cap>edge[k].flow){dis[j] = dis[stac[h]] + 1;stac[t++] = j;}}}if (dis[T] == -1){return false;}else{return true;}}int dfs(int S, int T, int low){if (S == T)return low;int i, j, k;int res = 0, tmp;for (i = head[S]; i != -1 && res < low; i = edge[i].next){k = edge[i].to;if (dis[k] == dis[S] + 1 && edge[i].cap>edge[i].flow){if (tmp = dfs(k, T, min(low - res, edge[i].cap - edge[i].flow))){res += tmp;edge[i].flow += tmp;edge[i ^ 1].flow -= tmp;}}}if (res == 0){dis[S] = -1;}return res;}int dinic(int s, int t){int maxflow = 0, tmp;while (bfs(s, t)){while (tmp = dfs(s, t, INF))maxflow += tmp;}return maxflow;}int n, m;void solve(){int i, j, k;sa(n), sa(m);repp(i,1,m){int u, v, dir;sa(u), sa(v), sa(dir);out[u]++, in[v]++;if (dir == 0)addedge(u, v, 1);}repp(i, 1, n){if (abs(out[i] - in[i]) & 1){puts("impossible");return;}}int maxflow = 0;repp(i, 1, n){if (out[i] > in[i]){maxflow += (out[i] - in[i]) / 2;addedge(0, i, (out[i] - in[i]) / 2);}else{addedge(i, n + 1, abs(out[i] - in[i]) / 2);}}if (maxflow == dinic(0, n + 1)){puts("possible");}else{puts("impossible");}}int main(){#ifndef ONLINE_JUDGE  freopen("i.txt", "r", stdin);freopen("o.txt", "w", stdout);#endifint t;scanf("%d", &t);while (t--){init();solve();}return 0;}


0 0
原创粉丝点击