POJ-1637 hybrid diagram Euler Loop-network flow solution

Source: Internet
Author: User

Given a hybrid graph, a hybrid graph has both one-way and two-way edges. Now, check whether such a graph has an Euler loop.

Solution: first, let's review the condition of the Euler loop of the directed graph: the outbound degrees of all nodes are equal to inbound. The following figure shows the conversion process:

Assume that the two points A and B in the figure above represent the EDGE connection between any two points when the directed graph has an Euler loop. Obviously, the inbound degrees of two points are equal to the outbound degrees. Now we want to change the relationship between an edge, so we can see the new inbound and outbound as shown in, this constant is the parity between the inbound and outbound degrees of any point.

The steps are as follows:

1. Select a direction for any undirected edge. If there is an Euler loop, it can be obtained that the inbound degree of each point must be at least an even number when the outbound degree difference exists. Otherwise, it cannot constitute an Euler loop.

2. on the basis of meeting the above conditions, we need to correct our hypothetical edge, if the inbound degree of all vertices is equal to the outbound degree, the Euler's loop of the hybrid graph can be obtained (correct undirected edge as directed edge ). How can we solve this problem? It can be converted into a network flow problem:

It can be defined that some traffic needs to flow out for nodes with an inbound degree greater than the outbound degree. A node with an outbound degree greater than the inbound level needs to receive some traffic, and then a super source point and a super sink point need to maintain a stream balance. The edge construction is as follows:

A. If the inbound I of a node is greater than the outbound I, a traffic from S is connected to an edge (in [I]-out [I])/2 to I.
B. If the I degree of a node is greater than the inbound level, I connects an edge of (out [I]-in [I])/2 to the vertex T.
C. traverse <I, j> If I and j are connected to MP [I] [J] undirected edges, connect an edge with the traffic from J to I as MP [I] [J. Why, in turn, because this is what we assume, which means we have a lot of regretful capital.

The Code is as follows:

# Include <cstdlib> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; const int Ss = 202, TT = 203; const int INF = 0x3fffffff; int n, m, flow; int in [205], out [205]; int MP [205] [205]; // records undirected edge struct edge {int V, C, next ;}; edge e [10000]; int idx, head [205]; int LV [205], que [205]; int front, tail; void insert (int A, int B, int c) {e [idx]. V = B, E [idx]. C = C; E [Idx]. next = head [a]; head [a] = idx ++;} bool Euler () {flow = 0; For (INT I = 1; I <= N; ++ I) {If (in [I]> out [I]) flow + = (in [I]-out [I])> 1; // define the outbound traffic of a point whose inbound traffic is greater than the outbound traffic if (ABS (in [I]-out [I]) & 1) {// If the difference between inbound and outbound values is odd return false;} return true;} bool BFS () {front = tail = 0; memset (LV, 0xff, sizeof (LV); LV [ss] = 0; que [tail ++] = SS; while (front <tail) {int u = que [Front ++]; for (INT I = head [u]; I! =-1; I = E [I]. Next) {int v = E [I]. V; If (! (~ LV [v]) & E [I]. c) {LV [v] = lv [u] + 1; if (V = TT) return true; que [tail ++] = V ;}}} return false;} int DFS (int u, int sup) {If (u = TT) return sup; int TF = 0, F; for (INT I = head [u]; I! =-1; I = E [I]. next) {int v = E [I]. v; If (LV [u] + 1 = lv [v] & E [I]. C & (F = DFS (v, min (E [I]. c, sup-tf) {TF + = f; E [I]. c-= F, E [I ^ 1]. c + = f; If (tf = sup) return sup ;}} if (! TF) LV [u] =-1; return TF;} int dinic () {int ret = 0; while (BFS () {RET + = DFS (SS, INF);} return ret;} void solve () {idx = 0; memset (Head, 0xff, sizeof (head); For (INT I = 1; I <= N; ++ I) {If (in [I]> out [I]) {insert (SS, I, (In [I]-out [I])> 1); insert (I, SS, 0);} else if (out [I]> in [I]) {insert (I, TT, (out [I]-in [I])> 1); insert (TT, I, 0) ;}for (Int J = 1; j <= N; ++ J) {If (MP [I] [J]) {// This is assumed to be a bidirectional edge direction insert (J, I, MP [I] [J]); insert (I, j, 0) ;}} if (dinic () = flow) {puts ("possible");} else {puts ("impossible") ;}} int main () {int T, A, B, C; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); // n points, M edge memset (in, 0, sizeof (in); memset (Out, 0, sizeof (out); memset (MP, 0, sizeof (MP )); for (INT I = 0; I <m; ++ I) {scanf ("% d", & A, & B, & C ); ++ in [B], ++ out [a]; If (! C) {// indicates that this edge is bidirectional ++ MP [a] [B]; // A is used as the end of the arc by default, and B is used as the arc header} If (! Euler () {puts ("impossible"); continue;} solve ();} 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.