Poj 1637 sightseeing tour hybrid graph Euler's loop existence judgment

Source: Internet
Author: User

I didn't think network streams could solve this problem, but I couldn't think of @ [email protected]

In the beginning, all the undirected edges are defined as directed edges, and the inbound and outbound degrees of each vertex are calculated. In the past, the Euler's loop of a directed graph determined that the entry of each vertex is equal to the exit level, which ensures that the entry can return to the starting point. Now, when some sides can change the direction, the difference between the inbound and outbound degrees of all fixed points must be an even number, because changing the direction of any edge will change the inbound and outbound degrees of the two fixed points by 2, therefore, the deviation between the inbound and outbound degrees of all points in an Euler loop is an even number and the deviation is set to deg.

The problem now turns to whether the inbound and outbound degrees of all vertices can be zero by changing the direction of some edges, because the direction of the directed edges has been determined, so ignore it, delete all of them. If any of the remaining edges has an outbound degree greater than the inbound degree, you must replace the-deg/2 edges to make them 0 and create the edges from The Source Vertex to these vertices, the capacity is-deg/2. Similarly, if there is an inbound or outbound value greater than the inbound value, the edge of these points is established, and the capacity is also deg/2. The other side capacity is 1, and then the maximum flow is performed. if the traffic is equal to the number of sides to be swapped, that is, all the edges at the source point are full, there is an Euler loop, otherwise, there is no Euler loop.

Why is this true? My idea is that, apart from connecting the source and sink, the capacity of other edges is, which corresponds to one edge, when the Source Vertex is connected to a vertex, the capacity is T. If the full load is equivalent to the full load of the t edge starting from the vertex, it is equivalent to replacing the t edge, but this will affect the degree of the back edge, however, because the stream will always flow to the sink point, all the fully loaded edges in the middle will be exchanged, so the degree of the original 0 in the middle will not change at the end.

# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <climits> # include <string> # include <iostream> # include <map> # include <cstdlib> # include <list> # include <set> # include <queue> # include <stack> using namespace STD; typedef long ll; const int maxn = 205; const int INF = int_max/3; struct edge {int U, V, cap; edge (int u, int V, int cap): U (u), V (V), Cap (CAP) {}}; int n, m, incnt [Ma XN], outcnt [maxn]; int deg [maxn], S, T; vector <edge> edges; vector <int> E [maxn]; void Adde (int u, int V, int W) {int M = edges. size (); edges. push_back (edge (U, V, W); edges. push_back (edge (v, U, 0); e [u]. push_back (m); e [v]. push_back (M ^ 1);} int level [maxn], Q [maxn * 2], QS, QE; bool BFS () {// create a hierarchical network memset (level, 0, sizeof (level); level [s] = 1; Qs = Qe = 0; Q [QE ++] = s; while (QS <QE) {int now = Q [QS ++], Nm = E [No W]. size (); If (now = T) break; For (INT I = 0; I <Nm; I ++) {edge & Ne = edges [E [now] [I]; If (ne. cap & level [NE. v] = 0) {level [NE. v] = level [now] + 1; Q [QE ++] = ne. V ;}}} return level [T];} int DFS (INT now, int alpha) {If (now = T) return Alpha; int sum = 0, nm = E [now]. size (); For (INT I = 0; I <Nm; I ++) {edge & Ne = edges [E [now] [I]; if (level [now] + 1 = level [NE. v] & ne. cap & alpha) {int ret = DFS (ne. v, min (alpha, ne. CAP); ne. cap-= ret; edges [E [now] [I] ^ 1]. cap + = ret; sum + = ret; alpha-= ret;} If (sum = 0) level [now] =-1; return sum;} void dinic () {While (BFS () DFS (S, INF);} bool solve () {S = 0; t = n + 1; // determine whether the deviation between inbound and outbound degrees is an even number for (INT I = 1; I <= N; I ++) {deg [I] = incnt [I]-outcnt [I]; If (deg [I] & 1) return false ;} // create a capacity network for (INT I = 1; I <= N; I ++) {// If the inbound degree is smaller than the outbound degree, create an edge from the start point to this point, capacity: deg/2 If (DEG [I] <0) Adde (S, I,-deg [I]/2); // if the degree of exit is greater than entry, the edge from the current point to the sink point is established, the capacity is also deg/2 If (deg [I]> 0) Adde (I, T, deg [I]/2);} // calculates the maximum stream dinic (); // judge whether all edges starting from the source point are full streams int M = E [s]. size (); For (INT I = 0; I <m; I ++) {If (edges [E [s] [I]. cap! = 0) return false;} return true;} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); edges. clear (); For (INT I = 0; I <= n + 1; I ++) E [I]. clear (); memset (incnt, 0, sizeof (incnt); memset (outcnt, 0, sizeof (outcnt); For (INT I = 1; I <= m; I ++) {int U, V, C; scanf ("% d", & U, & V, & C ); // process all undirected edges as directed edges first. incnt [v] ++; outcnt [u] ++; // If (C = 0) Adde (u, v, 1);} If (solve () 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.