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
Source
题目类型:混合图欧拉回路
算法分析:给原图中的每个无向边指定一个方向并看作是有向边。然后记录有向边两个端点的出入度并建图。若存在一个顶点的出入度之差是奇数,则一定不可能存在欧拉回路。否则从入度大于出度的顶点向汇点连接一条边,边权为出入度之差的绝对值除以2,从源点向入度小于出度的顶点连接一条边,边权为出入度之差的绝对值除以2。最后跑一个最大流判断是否满流即可。详细内容可以参考: http://www.cnblogs.com/kuangbin/p/3537525.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
/************************************************* Author :supermaker Created Time :2016/2/7 17:20:01 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 3000 + 66; struct Node { int to, nxt, c, f; }; Node edge[maxn]; int head[maxn], edgelen; int dep[maxn], cnt[maxn], cur[maxn]; void Init () { memset (head, -1, sizeof (head)); memset (edge, -1, sizeof (edge)); edgelen = 0; } void AddEdge (int u, int v, int w, int rw = 0) { edge[edgelen].to = v, edge[edgelen].c = w, edge[edgelen].f = 0; edge[edgelen].nxt = head[u], head[u] = edgelen++; edge[edgelen].to = u, edge[edgelen].c = rw, edge[edgelen].f = 0; edge[edgelen].nxt = head[v], head[v] = edgelen++; } void Bfs (int s, int e) { memset (dep, -1, sizeof (dep)); memset (cnt, 0, sizeof (cnt)); dep[e] = 0; cnt[dep[e]]++; queue <int> qu; qu.push (e); while (!qu.empty ()) { int u = qu.front (); qu.pop (); for (int i = head[u]; i != -1; i = edge[i].nxt) { int v = edge[i].to; if (dep[v] != -1) continue; qu.push (v); dep[v] = dep[u] + 1; cnt[dep[v]]++; } } } int sta[maxn]; int MaxFlow (int s, int e, int n) { Bfs (s, e); int res = 0, top = 0, u = s; memcpy (cur, head, sizeof (head)); while (dep[s] < n) { if (u == e) { int minval = INF, minval_pos = -1; for (int i = 0; i < top; i++) if (minval > edge[sta[i]].c - edge[sta[i]].f) { minval = edge[sta[i]].c - edge[sta[i]].f; minval_pos = i; } for (int i = 0; i < top; i++) { edge[sta[i]].f += minval; edge[sta[i]^1].f -= minval; } res += minval; top = minval_pos; u = edge[sta[top]^1].to; } else { bool is_have = false; for (int i = cur[u]; i != -1; i = edge[i].nxt) { int v = edge[i].to; if (edge[i].c - edge[i].f && dep[v] + 1 == dep[u]) { cur[u] = i; sta[top++] = cur[u]; u = v; is_have = true; break; } } if (is_have) continue; int minval = n; for (int i = head[u]; i != -1; i = edge[i].nxt) if (edge[i].c - edge[i].f && dep[edge[i].to] < minval) { minval = dep[edge[i].to]; cur[u] = i; } cnt[dep[u]]--; if (!cnt[dep[u]]) return res; dep[u] = minval + 1; cnt[dep[u]]++; if (u != s) u= edge[sta[--top]^1].to; } } return res; } int indeg[maxn], outdeg[maxn]; int n, m; void SS () { bool is_have = true; int full = 0; for (int i = 1; i <= n; i++) { if (abs (indeg[i] - outdeg[i]) & 1) { is_have = false; break; } if (indeg[i] > outdeg[i]) { AddEdge (i, n + 1, (indeg[i] - outdeg[i]) / 2); full += (indeg[i] - outdeg[i]) / 2; } else if (indeg[i] < outdeg[i]) AddEdge (0, i, (outdeg[i] - indeg[i]) / 2); } if (!is_have) puts ("impossible"); else { if (MaxFlow (0, n + 1, n + 2) == full) puts ("possible"); else puts ("impossible"); } } int main() { //CFF; //CPPFF; int t; scanf ("%d", &t); while (t--) { Init (); memset (indeg, 0, sizeof (indeg)); memset (outdeg, 0, sizeof (outdeg)); scanf ("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int u, v, w; scanf ("%d%d%d", &u, &v, &w); outdeg[u]++, indeg[v]++; if (!w) AddEdge (u, v, 1); } SS (); } return 0; } |
- « 上一篇:poj1459
- poj3469:下一篇 »