Poj1637 sightseeing tour, the Euler Loop problem of the hybrid graph, the maximum flow solution

Source: Internet
Author: User

Euler's loop problem of hybrid Graphs

Question address



Euler Loop Problems

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. Euler's 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

Network stream solution is used in the Euler loop of the hybrid graph.
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 ). 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.
  Therefore, the Euler Loop problem of the hybrid graph is solved.




# Include <cstdio> # include <vector> # include <queue> # include <cmath> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 500 + 5; const int INF = 100000000; struct edge {int from, to, Cap, flow ;}; struct dinic {int n, m, S, T; vector <edge> edges; vector <int> G [maxn]; bool vis [maxn]; int d [maxn]; int cur [maxn]; void Init (int n) {This-> N = N; For (INT I = 0; I <= N; ++ I) g [I]. clear (); edges. cl Ear ();} void addedge (int from, int to, int cap) {edges. push_back (edge) {from, to, Cap, 0}); edges. push_back (edge) {to, from, 0, 0}); M = edges. size (); G [from]. push_back (m-2); G [to]. push_back m-1);} bool BFS () {memset (VIS, 0, sizeof vis); queue <int> q; q. push (s); vis [s] = 1; d [s] = 0; while (! Q. empty () {int x = Q. front (); q. pop (); For (INT I = 0; I <G [X]. size (); ++ I) {edge & E = edges [G [x] [I]; If (! Vis [E. to] & E. cap> E. flow) {vis [E. to] = 1; d [E. to] = d [x] + 1; q. push (E. to) ;}}} return vis [T];} int DFS (int x, int A) {If (x = T | A = 0) return; int flow = 0, F; For (Int & I = cur [X]; I <G [X]. size (); ++ I) {edge & E = edges [G [x] [I]; If (d [x] + 1 = d [E. to] & (F = DFS (E. to, min (A, E. cap-e.flow)> 0) {e. flow + = f; edges [G [x] [I] ^ 1]. flow-= f; flow + = f; A-= f; if (a = 0) break;} return flow ;} Int maxflow (int s, int t) {This-> S = s; this-> T = T; int flow = 0; while (BFS ()) {memset (cur, 0, sizeof cur); flow + = DFS (S, INF);} return flow ;}}; dinic solver; int in [maxn], out [maxn]; int main () {int t, n, m, I, x, y, z; scanf ("% d", & T ); while (t --) {scanf ("% d", & N, & M); memset (in, 0, sizeof in); memset (Out, 0, sizeof out); solver. init (n + 1); for (I = 0; I <m; ++ I) {scanf ("% d", & X, & Y ,& Z); out [x] ++; in [y] ++; If (! Z) {// X-> Y solver. addedge (X, Y, 1) ;}} bool OK = true; for (I = 1; I <= N; ++ I) if (ABS (in [I]-out [I]) & 1) {OK = false; break;} If (OK) {int S = 0, t = n + 1; int full_flow = 0; for (I = 1; I <= N; ++ I) {If (out [I]-in [I]> 0) {solver. addedge (S, I, (out [I]-in [I])/2); full_flow + = (out [I]-in [I])/2 ;} else {solver. addedge (I, T, (in [I]-out [I])/2);} int max_flow = solver. maxflow (S, T); If (max_flow! = Full_flow) OK = false;} If (OK) 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.