HDU 1956 POJ 1637 Sightseeing Tour

Source: Internet
Author: User

Euler circuit determination method for mixed graphs:

1. First to determine whether the base diagram is connected, the non-connected words are impossible, otherwise enter the next step.

2. For the non-directional side, determine a direction arbitrarily

3. After the determination is made, the whole graph becomes a graph of graphs, calculating the degree of penetration and the degree of each node

4. If there is a node in the degree-out is an odd number, then it is not possible, otherwise enter the next step

5. Set up a network, add a new Origin s, and a meeting point T, and then set up a network

 for (i=1; i<=m; i++)     if (ff[i]==0) // If there is a forward edge         Addedge (U[i],v[i],1);  for (i=1; i<=n; i++) {    if(ru[i]>chu[i])        Addedge (I,t, (ru[i) -chu[i])/2);     Else          Addedge (S,i, (Chu[i]++-ru[i])/2);}            

6. Calculate the maximum network flow.

7. If the edge from S is full-flow, it means that it exists, otherwise it does not exist.

8. Find the edges in the network stream that are not associated with s,t, and if there is an edge with traffic equal to 1, then reverse these edges and finally get a Eulerian graph.

AC code (dinic continuous shortest augmented path algorithm for maximum network flow):

#include <cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>#include<algorithm>using namespacestd;Const intmaxn= -+Ten;Const intinf=0x7FFFFFFF;structedge{int  from, To,cap,flow;}; Vector<Edge>Edges;vector<int>G[MAXN];BOOLVIS[MAXN];intD[MAXN];intCUR[MAXN];intRU[MAXN];intCHU[MAXN];intU[MAXN],V[MAXN],FF[MAXN];intFATHER[MAXN];intM,s,t,tot;intn,m;//finding a hierarchical networkBOOLBFS () {memset (Vis,0,sizeof(VIS)); Queue<int>Q;    Q.push (s); D[s]=0; Vis[s]=1;  while(!Q.empty ()) {        intx=Q.front ();        Q.pop ();  for(intI=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); }        }    }    returnvis[t];}//Add EdgevoidAddedge (int  from,intTo,intcap)    {Edge R; R. from= from; R.to=to ; R.cap=cap; R.flow=0;    Edges.push_back (R);    Edge D; D. from=to ; D.to= from; D.cap=0; D.flow=0;    Edges.push_back (d); M=edges.size (); g[ from].push_back (M-2); G[to].push_back (M-1);}//Each phase comes with a DFS augmentationintDFS (intXinta) {    if(x==t| | a==0)returnA; intflow=0, F;  for(intI=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; }    }    returnflow;}//multiple stages, several times to establish a hierarchical network. intMaxflow (intSsintTT) {    intflow=0;  while(BFS ()) {memset (cur,0,sizeof(cur)); Flow+=DFS (Ss,inf); }    returnflow;}intFind (intx) {    if(X!=father[x]) father[x]=Find (father[x]); returnfather[x];}intMain () {intT,flag,i; scanf ("%d",&T);  while(t--) {scanf ("%d%d",&n,&M);        Edges.clear ();  for(i=0; i<maxn; i++) g[i].clear (); Flag=1; S=0, t=n+1;//set up super-origin and super-sink pointsmemset (Ru,0,sizeof(Ru)); memset (Chu,0,sizeof(Chu));  for(i=0; i<=n;i++) father[i]=i; Tot=N;  for(i=1; i<=m; i++) {scanf ("%d%d%d",&u[i],&v[i],&Ff[i]); intfx=Find (U[i]); intfy=Find (V[i]); if(fx!=FY) {FATHER[FX]=fy; Tot--; } Ru[v[i]]++; Chu[u[i]]++; }        if(tot!=1) flag=0; if(flag) { for(i=1; i<=n; i++)                if(ABS (Ru[i]-chu[i])%2==1) {flag=0;  Break; }        }        if(flag) { for(i=1; i<=m; i++)                if(ff[i]==0)//If there is a forward edgeAddedge (U[i],v[i],1);  for(i=1; i<=n; i++)            {                if(ru[i]>Chu[i]) Addedge (I,t, (Ru[i]-chu[i])/2); ElseAddedge (S,i, (Chu[i]++-ru[i])/2);            } maxflow (S,t);  for(i=0; I<edges.size (); i++)                if(Edges[i]. from==s&&edges[i].cap!=Edges[i].flow) {Flag=0;  Break; }        }        if(flag) printf ("possible\n"); Elseprintf"impossible\n"); }    return 0;}

HDU 1956 POJ 1637 Sightseeing Tour

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.