"POJ1637" sightseeing tour mixed graph to find the existence of the Euler loop network flow,

Source: Internet
Author: User

Test instructions: Multiple sets of data, and the last 0/1 indicates 0 non-direction 1.

Ask if there is a Euro-pull loop.


Solving the puzzle : giving it any direction without a direction .


First, the point-in degree of the Euler loop = the degree of exit.

And then found that each non-directional side if the change direction, the original entry point of +1, out of 1, out of the point.

Then we might as well be different in degrees and out of the point with the Yuanhui in one of the edges, the capacity is half of the difference in the size of the (each change of the margin-2)

Then the original non-maxflow==sum to the points in the graph, the capacity 1, and the last check if (difference/4).


It's not a good idea. Weak vegetables may wish to be more than the source, into a number of connected sinks, the capacity of the difference in half of the degree, and then by the direction of the non-directional to it in the network flow map orientation, capacity 1. Then analyze why you want to build it (as explained above).


1 A code:

#include <cmath> #include <queue> #include <cstdio> #include <cstring> #include <iostream > #include <algorithm> #define N 405#define G 100000#define inf 0x3f3f3f3f#define inf 0x3f3f3f3f3f3f3f3fll# Define LL intusing namespace std;struct ksd{int V,next,len;} E[g];int head[n],cnt;void Add (int u,int v,int len) {cnt++;e[cnt].v=v;e[cnt].len=len;e[cnt].next=head[u];head[u]=cnt;} Queue<int>q;int D[n],s,t;bool BFs () {memset (d,0,sizeof (d)); int I,u,v;while (!q.empty ()) Q.pop (); Q.push (s), D[s] =1;while (!q.empty ()) {U=q.front (), Q.pop (); for (I=head[u];i;i=e[i].next) if (E[i].len) {v=e[i].v;if (!d[v]) {D[v]=d[u] +1;if (v==t) return 1;q.push (v);}} return 0;} int dinic (int x,int flow) {if (x==t) return flow;int remain=flow,k;int i,v;for (i=head[x];i&&remain;i=e[i].next) {v=e[i].v;if (e[i].len&&d[v]==d[x]+1) {k=dinic (V,min (Remain,e[i].len)), if (!k) d[v]=0;e[i^1].len+=k,e[i]. Len-=k;remain-=k;}} return flow-remain;} int rd[n],od[n];int n,m,sum,maxflow;void init () {cnt=1;sum=maxflow=0;S=n+1,t=n+2;memset (head,0,sizeof (head)); Memset (rd,0,sizeof (RD)); memset (od,0,sizeof (OD));} void work () {int i,a,b,c;scanf ("%d%d", &n,&m), Init (); for (i=1;i<=m;i++) {scanf ("%d%d%d", &a,&b, &AMP;C); Rd[b]++,od[a]++;if (!c) Add (a,b,1), add (b,a,0);} for (i=1;i<=n;i++) {if ((Rd[i]+od[i]) &1) {puts ("impossible"); return;} Sum+=abs (Rd[i]-od[i]), if (Od[i]>rd[i]) Add (s,i,od[i]-rd[i]>>1), add (i,s,0), else if (Rd[i]>od[i]) Add (i,t , rd[i]-od[i]>>1), add (t,i,0);} while (BFS ())) Maxflow+=dinic (S,inf), if (Maxflow*4==sum) puts ("possible"), else puts ("impossible");} int main () {//freopen ("test.in", "R", stdin), int g;for (scanf ("%d", &g), g--;) work ();}


"POJ1637" sightseeing tour mixed graph to find the existence of the Euler loop network flow,

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.