[Hybrid plot Euler Loop] poj 1637

Source: Internet
Author: User

Euler's loop of a hybrid GraphProblem.

A hybrid graph means that the edge set has a directed edge and a undirected edge at the same time. At this time, we need to use network stream modeling to solve the problem.

Modeling:

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.

Now, 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 changes the direction (inbound> outbound is changed to inbound, outbound> inbound is changed to outbound), the inbound = outbound is guaranteed. If each vertex is inbound, it is obvious that this graph has an Euler loop.

Now the question is: which side should I change to allow each point to exit =? Construct a network flow model.

First, the directed edge cannot be changed. It is useless to delete it. Didn't the undirected edge be targeted at the beginning? Set the direction of the network, and 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 all edges sent from S are full streams. There is an Euler loop, and there is no or no. What is the Euler loop? 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.

# Define min int_min # define Max int_max # define n 220int min (int A, int B) {return A> B? B: A;} int G [N] [N]; int du [N]; int res [N]; int pre [N]; int f [N] [N]; int N; bool EK (int s, int T, int CNT) {int I, j; queue <int> QQ; memset (F, 0, sizeof (f); int ans = 0; while (1) {memset (Res, 0, sizeof (RES); Res [s] = max; pre [s] =-1; QQ. push (s); While (! QQ. empty () {int u = QQ. front (); QQ. pop (); For (INT v = s; v <= T; V ++) {If (! Res [v] & F [u] [v] <G [u] [v]) {QQ. push (V); Pre [v] = u; Res [v] = min (RES [u], G [u] [v]-f [u] [v]) ;}} if (! Res [T]) break; int K = T; while (pre [k]! =-1) {f [pre [k] [k] + = res [T]; F [k] [pre [k]-= res [T]; k = pre [k] ;}}for (I = 1; I <= N; I ++) {ans + = f [0] [I];} return ans = CNT;} int main () {int t; scanf ("% d", & T); While (t --) {int m; scanf ("% d", & N, & M); int I, j; memset (DU, 0, sizeof (DU); memset (G, 0, sizeof (g); While (M --) {int A, B, type; scanf ("% d", & A, & B, & type ); du [a] ++; Du [B] --; If (! Type) {G [a] [B] ++ ;}} bool OK = 1; for (I = 1; I <= N; I ++) {If (ABS (Du [I]) % 2 = 1) {// If the outbound degree and inbound degree of a vertex are odd, there must be no Euler's circuit OK = 0; break;} else {du [I] = Du [I]/2; // the weight of each edge in the network is half the difference between the outbound and inbound.} If (! OK) {puts ("impossible"); continue;} int CNT = 0; for (I = 1; I <= N; I ++) {If (Du [I] <0) {// inbound <, connected to the sink point G [I] [n + 1] = ABS (Du [I]);} else if (Du [I]> 0) {// output>, connect to the source point G [0] [I] = Du [I]; CNT + = Du [I]; // CNT is used to verify whether the stream is full} If (EK (0, n + 1, CNT) {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.