Poj 1637 Sightseeing Tour (hybrid plot Euler loop, maximum network flow)

Source: Internet
Author: User

Http://poj.org/problem? Id = 1637

Sightseeing Tour
Time limit:1000 ms   Memory limit:10000 K
Total submissions:7498   Accepted:3123

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 shoshould 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 <= 1000 ,1 <= S <= 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

Source

Northwestern Europe 2003


The Euler's loop solution for a hybrid graph is as follows:

Add any direction to the undirected edge, and then calculate the inbound outbound degree of the vertex in the graph. If the inbound and outbound degrees of all vertices are an even number, the Euler's loop may be formed;

Delete the original directed edge. The capacity of the directed edge is 1;

Increase the Source and Sink points. If the inbound degree of a point is greater than the outbound degree, it is connected to the sink point. The capacity is (inbound-outbound)/2. If the outbound degree of a point is greater than the inbound level, the source point is connected to it, and the capacity is (outbound-Inbound)/2;

Run the largest stream in the new graph. If the stream is full, it may constitute an Euler loop.


# Include <cstdio> # include <iostream> # include <cstdlib> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (x) (x) * (x) # define ll long # define ITN int # define INF 0x3f3f3f3f # define PI 3.1415926535897932384626 # define EPS 1e-10 # define maxm 4004 # define maxn 404 Using namespace STD; int FIR [maxn]; int U [maxm], V [maxm], Cap [maxm], flow [maxm], NEX [maxm]; int e_max; int ITER [maxn], Q [maxn], LV [maxn]; void add_edge (INT _ u, int _ v, int _ w) {int e; E = e_max ++; U [e] = _ u; V [e] = _ v; CAP [e] = _ w; nex [e] = FIR [U [e]; FIR [U [e] = E; E = e_max ++; U [e] = _ V; V [e] = _ u; CAP [e] = 0; NEX [e] = FIR [U [e]; FIR [U [e] = E ;} void dinic_bfs (INT s) {int F, R; memset (LV,-1, sizeof LV); Q [f = r = 0] = s; LV [s] = 0; while (F <= r) {in T x = Q [f ++]; for (int e = FIR [X]; ~ E; E = NEX [e]) {If (Cap [e]> flow [e] & LV [V [e] <0) {LV [V [e] = lv [U [e] + 1; Q [++ R] = V [e] ;}}} int dinic_dfs (INT _ u, int T, int _ f) {If (_ u = T) return _ F; For (Int & E = ITER [_ u]; ~ E; E = NEX [e]) {If (Cap [e]> flow [e] & LV [_ u] <LV [V [e]) {int _ d = dinic_dfs (V [e], T, min (_ F, Cap [e]-flow [e]); If (_ d> 0) {flow [e] + = _ d; flow [E ^ 1]-= _ d; return _ d ;}} return 0 ;}int max_flow (INT s, int t) {memset (flow, 0, sizeof flow); int total_flow = 0; For (;) {dinic_bfs (s); If (LV [T] <0) return total_flow; memcpy (ITER, fir, sizeof ITER); int _ F; while (_ f = dinic_dfs (S, T, INF)> 0) total_flow + = _ F;} return tot Al_flow;} struct edge {int U, V, W;} edge [1111]; int in [222], out [222]; int main () {# ifndef online_judge freopen ("/home/fcbruce/document/code/T", "r", stdin); # endif // online_judge int t_t; unsigned long seed = ~ 0ull; scanf ("% d", & t_t); While (t_t --) {int n, m, S, T, full_flow = 0; e_max = 0; memset (FIR, -1, sizeof fir); memset (in, 0, sizeof in); memset (Out, 0, sizeof out); scanf ("% d", & N, & M); seed = seed ^ t_t ^ n ^ m ^ (~ 0ull); srand (SEED); s = 0; t = n + 1; for (INT I = 0; I <m; I ++) {scanf ("% d", & edge [I]. u, & edge [I]. v, & edge [I]. w); If (edge [I]. W = 1) {out [edge [I]. u] ++; in [edge [I]. v] ++; edge [I]. W = 0; continue;} If (RAND () & 1) {edge [I]. W = 1; out [edge [I]. u] ++; in [edge [I]. v] ++; add_edge (edge [I]. u, edge [I]. v, 1);} else {edge [I]. W =-1; out [edge [I]. v] ++; in [edge [I]. u] ++; add_edge (edge [I]. v, edge [I]. u, 1) ;}} bool flag = true; for (I NT I = 1; I <= N; I ++) {If (ABS (in [I]-out [I]) & 1) {flag = false; break ;} else {If (in [I] = out [I]) continue; If (in [I]> out [I]) add_edge (I, T, in [I]-out [I]> 1); else add_edge (S, I, out [I]-in [I]> 1 ), full_flow + = out [I]-in [I]> 1 ;}} if (! Flag) {puts ("impossible"); continue;} else {If (max_flow (s, t) = full_flow) puts ("possible "); else puts ("impossible") ;}} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.