Poj 1637 Sightseeing Tour (hybrid plot Euler loop)

Source: Internet
Author: User
Poj 1637 sightseeing tour Link: http://poj.org/problem? Id = 1637
Question:Given a hybrid graph, there are both directed edges and directed edges, and asked if there is an Euler loop.
Ideas:1 Definition
Euler Tour: a path that passes through each vertex once and only once through each edge in the graph.
Euler Circuit: a loop that passes through each vertex once and only once through each edge in the graph.
Euclidean-a diagram with Euler's loop exists.

2. determine whether an undirected graph has an Euler's path or loop
The sufficient and necessary condition for g to have Euler's path is: G connectivity. In G, there are only two odd vertices (they are the two ends of Euler's path ).
G has an Euler Loop (G is an Euclidean loop): G is connected, and G is an even degree vertex.

3. Determine whether a directed graph has an Euler's path or loop.
D has an Euler path: D is connected. Except two vertices, the inbound degrees of the other vertices are equal to the outbound degrees. Among the two special vertices, the inbound degrees of one vertex are 1 greater than the outbound degrees, the inbound degree of the other vertex is 1 lower than the outbound degree.
D has an Euler Loop (D is an Euclidean): D is connected, and the inbound degrees of all vertices in D are equal to the outbound degrees.


4. Hybrid graph. A hybrid graph is a mixture of an undirected graph and a directed graph. That is, the edges in the graph have both directed edges and directed edges.


5. Euler's loop of the hybrid Graph
The Euler loop of the hybrid graph uses network streams.
The undirected edge of the graph is directed randomly to calculate the inbound and outbound degrees of each vertex. If the difference between the degree of entry and exit of a certain point is odd, there is certainly no Euler loop. Because the Euler Loop requires that each entry degree = exit level, that is, the total degree is an even number, there must be an Euler Loop for an odd degree point.
The difference between the inbound and outbound degrees of each vertex is an even number. Divide this even number by 2 to get X. That is to say, for each vertex, as long as the X edge is reversed (in> out is changed into, out> In is changed out), the output = in can be guaranteed. If each vertex is inbound, it is obvious that this graph has an Euler loop.
The problem now becomes: Which side should be changed to allow each point to exit =? Construct a network flow model. The directed edge cannot be changed. Delete it directly. The directed undirected edge is set to what direction, and the network is constructed. The maximum side length is 1. Create another S and T. For the inbound> outbound vertex u, the connection edge (u, T) and capacity are X. For the outbound> inbound vertex v, the connection edge (S, V ), the capacity is X (note that X is different for different points. Due to carelessness, I made several mistakes here ). Then, check whether there is a full stream allocation. There is an Euler loop, and there is no or no. View the stream value allocation, and reverse the edges of all traffic other than 0 (the upper limit is 1, and the stream value is not 0 or 1) to obtain the European region with inbound = outbound.
Because the stream is full, each inbound> outbound vertex has x edges. The incoming edges are reversed, OK, and inbound = outbound. The same is true for the entry point. So what should I do if it is not connected to S or t? The connection condition with S is "out"> "in", and "T" is "in"> "out", so this is neither a point connected with s nor T, as early as the beginning, = has been satisfied. In the network flow process, these points are "intermediate points ". We know that the traffic at the intermediate point is not allowed to accumulate. In this way, the incoming traffic will come out as much as possible. After the reverse flow, we will naturally continue to maintain a balance.
So, in this case, the Euler Loop problem of the hybrid graph is solved.
Source: http://www.cnblogs.com/destinydesigner/archive/2009/09/28/1575674.html
Code:
/* ID: [email protected] prog: Lang: c ++ */# include <map> # include <set> # include <queue> # include <stack> # include <cmath> # include <cstdio> # include <vector> # include <string> # include <fstream> # include <cstring> # include <ctype. h> # include <iostream> # include <algorithm> using namespace STD; # define linf (1ll <60) # define Inf (1 <30) # define PI ACOs (-1.0) # define MEM (a, B) memset (a, B, sizeof (A) # define rep (I, A, n) for (INT I = A; I <n; I ++) # define per (I, A, n) for (INT I = n-1; I> = A; I --) # define EPS 1e-6 # define debug puts ("================ ") # define Pb push_back // # define MP make_pair # define all (x ). begin (), (x ). end () # define Fi first # define se second # define SZ (x) (INT) (x ). size () # define posin (x, y) (0 <= (x) & (x) <n & 0 <= (y) & (y) <m) typedef long ll; typedef unsigned long ull; const int maxn = 210; Const int maxm = 2000; int St, Ed, N; int M, s; int in [maxn], out [maxm], sum; struct node {int V; // vertex int cap; // capacity int flow; // current flow in this arc int NXT;} e [maxm * 2]; int G [maxn], CNT; void add (int u, int V, int c) {e [++ CNT]. V = V; E [CNT]. CAP = C; E [CNT]. flow = 0; E [CNT]. NXT = G [u]; G [u] = CNT; E [++ CNT]. V = u; E [CNT]. CAP = 0; E [CNT]. flow = 0; E [CNT]. NXT = G [v]; G [v] = CNT;} bool Init () {MEM (G, 0); CNT = 1; sum = 0; scanf ("% d", & M, & S); ST = 0, ed = m + 1; for (INT I = 1; I <= m; I ++) in [I] = out [I] = 0; int U, V, c; For (INT I = 0; I <s; I ++) {scanf ("% d", & U, & V, & C ); out [u] ++, in [v] ++; If (C = 0) add (u, v, 1) ;}for (INT I = 1; I <= m; I ++) {int K = in [I]-out [I]; If (K % 2! = 0) return false; If (k> 0) add (I, Ed, K/2), sum + = K/2; If (k <0) add (St, i,-k/2);} n = m + 3; return true;} int Dist [maxn], numbs [maxn], Q [maxn]; void rev_bfs () {int font = 0, rear = 1; for (INT I = 0; I <= N; I ++) {// n indicates the total number of DIST [I] = maxn; numbs [I] = 0;} Q [font] = Ed; Dist [ed] = 0; numbs [0] = 1; while (font! = Rear) {int u = Q [font ++]; for (INT I = G [u]; I; I = E [I]. NXT) {If (E [I ^ 1]. CAP = 0 | Dist [E [I]. v] <maxn) continue; Dist [E [I]. v] = DIST [u] + 1; ++ numbs [Dist [E [I]. v]; Q [rear ++] = E [I]. V ;}}int maxflow () {rev_bfs (); int U, totalflow = 0; int curg [maxn], revpath [maxn]; for (INT I = 0; I <= N; ++ I) curg [I] = G [I]; u = sT; while (Dist [st] <n) {If (u = ed) {// find an Augmenting Path int augf Low = inf; For (INT I = sT; I! = Ed; I = E [curg [I]. v) augflow = min (augflow, E [curg [I]. CAP); For (INT I = sT; I! = Ed; I = E [curg [I]. v) {e [curg [I]. cap-= augflow; E [curg [I] ^ 1]. cap + = augflow; E [curg [I]. flow + = augflow; E [curg [I] ^ 1]. flow-= augflow;} totalflow + = augflow; u = sT;} int I; for (I = curg [u]; I; I = E [I]. NXT) if (E [I]. cap> 0 & Dist [u] = DIST [E [I]. v] + 1) break; if (I) {// find an admissible arc, then advance curg [u] = I; revpath [E [I]. v] = I ^ 1; u = E [I]. v;} else {// no admissible arc, Then relabel This vertex if (0 = (-- numbs [Dist [u]) break; // gap cut, important! Curg [u] = G [u]; int mindist = N; For (Int J = G [u]; j = E [J]. NXT) if (E [J]. cap> 0) mindist = min (mindist, DIST [E [J]. v]); Dist [u] = mindist + 1; ++ numbs [Dist [u]; If (u! = ST) u = E [revpath [u]. v; // backtrack} return totalflow;} int main () {int CAS; scanf ("% d", & CAS); While (CAS --) {If (! Init () {puts ("impossible"); continue;} If (maxflow () = sum) puts ("possible"); else puts ("impossible ");} return 0 ;}


Poj 1637 Sightseeing Tour (hybrid plot Euler loop)

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.