Poj 1637 Sightseeing Tour (Euler loop determination of mixed graphs)

Source: Internet
Author: User
Tags in degrees mixed printf

Test instructions: A mixed graph is given to determine if the Euler loop is present, input x y D, if D is 0 is a non-forward edge, and 1 is a forward edge.

The Euler circuit of the graph and the graph can refer to my other Boven Oraton Luola circuit Judgement Hdu 1878 Euler circuit

The following reference is made to: ZOJ 1992 & POJ 1637 (mixed graph Euler circuit) | Wings ~

The connectivity of graphs should be judged first. The diagram given in the subject is connected, so there is no judgment.

For all the non-directed side of the random orientation, and then adjust.

Statistics of the degree of entry and exit of each point, if there is a point in the difference between the number of odd, there is no Euler circuit, because the difference is odd, regardless of if the adjustment side, can not make each point of the degree of access is equal.

Now the difference in the degree of access for each point is even, dividing the even number by 2, which is X. The direction of the X-bar that is connected to each vertex can make the point enter and exit equal. If each point can achieve the same degree of ingress and egress, there is a natural existence of Euler circuit.

Now the problem is to change which side of the direction can make each point in and out of the same degree, construct the network flow model.

A forward edge cannot change direction, so there is no edge to add. For an arbitrarily oriented non-directional edge at the beginning, add the edge in the direction you set, and the capacity is 1.

For the x just mentioned, if X is greater than 0, then an S (source point) is built to the edge of the current point capacity of X, if X is less than 0, an edge from the current point to the T (meeting point) capacity of |x| is built.

At this point is connected to the origin of the missing points, and sinks are connected to the point of lack of degrees,

The map is completed, the maximum flow is solved, and the Euler circuit is present if the full flow distribution is possible. So which side changes direction to get the Euler loop? To view traffic allocation, all traffic with a non-0 edge is the edge to change direction.

The principle is that because of the full flow distribution, the points that are connected to the source point must have an X-side inflow, and these edges are reversed to equal the degree of access, and so are connected to the sink point. There is no connection with the source, the sink is equal, of course, without modification, so that the Euler loop solution is complete.

#include <cstdio> #include <cstring> #include <iostream> using namespace std;   const int INF = 0X3FFFFFFF;     Weight upper bound const int MAXPT = 205;    Maximum number of top points const int maxeg = 5005;	   Maximum number of edges const int maxque = 10005; Queue Length/* s = 1;
    The source point t varies depending on the initialization function */Template<typename type> class Mnf_sap {private:int s,t;   int DIS[MAXPT];   Distance designator int PRE[MAXPT];  Predecessor Vertex Type FLOW[MAXPT];   To the current point, the minimum value of all arcs int CUREDGE[MAXPT];   Current arc cur int CNT[MAXPT];
    K number occurrences int queue[maxque],front,rear;

    BOOL VIS[MAXPT];
        void BFS () {int i,u;
        memset (vis,false,sizeof (VIS));
        front=rear=0;
        dis[t]=0;
        Vis[t]=true;
        queue[++rear]=t;
            while (front!=rear) {u=queue[(++front)%maxque];
                for (I=head[u];i!=0;i=edges[i].next) if (Vis[edges[i].v]==false &&!edges[i].cap)
   {dis[edges[i].v]=dis[u]+1;                 Vis[edges[i].v]=true;
                queue[(++rear)%maxque]=edges[i].v;
    }} for (i=1;i<=n;i++) cnt[dis[i]]++;
		} public:struct Node {int v,next;
		Type cap;
			Node () {} node (int _v,type _cap,int _next) {v=_v;
			Cap=_cap;
		Next=_next;
    }}edges[maxeg];      int n;
    Total number of nodes int e;

	int HEAD[MAXPT];   void Init (int _n)//algorithm initialization {s=1,t=_n;
		SOURCE point 1 Meeting point n n=_n;
		e=2;
	memset (head,0,sizeof (head));   } void Init (int _s,int _t)//algorithm initialization, subsequent need for n assignment {S=1;
		Source point 1, Meeting point designation t=_t;
		e=2;
	memset (head,0,sizeof (head));
		} void Add (int u,int v,type cap)//Start, end, Volume {Edges[e]=node (v,cap,head[u]);
		head[u]=e++;
		Edges[e]=node (U,0,head[v]);
	head[v]=e++;
		} Type SAP () {int u,v,i;   Type maxflow=0;
        Total maximum flow u=s;
        Flow[s]=inf;     for (i=1;i<=n;i++) curedge[i]=head[i];
        The current arc initializes the BFS ();
        Cnt[0]=n;
     while (Dis[s]<n)   {for (I=curedge[u];i!=0;i=edges[i].next)//Find allowed arc if (edges[i].cap>0 && di
            S[EDGES[I].V]+1==DIS[U])//break;         if (i!=0)//exists allow arc {curedge[u]=i;
                Set the current Arc v=edges[i].v;
                if (Edges[i].cap<flow[u]) Flow[v]=edges[i].cap;  else Flow[v]=flow[u];
                The smallest arc u=v to mark the current vertex;  Pre[v]=i;
                        Predecessor Vertex edge number if (u==t) {do {          EDGES[PRE[U]].CAP-=FLOW[T];        Forward Arc minus A[t] edges[pre[u]^1].cap+=flow[t];
                    Reverse arc u=edges[pre[u]^1].v is found by XOR operation;
                    } while (u!=s);
                    MAXFLOW+=FLOW[T];
                    memset (flow,0,sizeof (flow));
                Flow[s]=inf; }} else//does not existAllow arc {if (--cnt[dis[u]]==0) break;
                Clearance optimization dis[u]=n;
                Curedge[u]=head[u];
                        for (I=head[u];i!=0;i=edges[i].next) if (Edges[i].cap && dis[edges[i].v]+1<dis[u])       dis[u]=dis[edges[i].v]+1;
                Modify the distance designator to the minimum non-allowable arc plus 1 cnt[dis[u]]++;
            if (u!=s) u=edges[pre[u]^1].v;
    }} return maxflow;

}
};
Mnf_sap<int> ob;  int n,degree[210];
	Insufficient in degrees bool Judge (int n) {int sum=0;
		for (int i=1;i<=n;i++) if (degree[i]%2==1)//An odd number is not possible to return false; else {if (degree[i]>0) {ob.
				ADD (1,1+I,DEGREE[I]/2);
			SUM+=DEGREE[I]/2; } else ob.  ADD (1+I,N+2,-DEGREE[I]/2); Connect to the meeting point} if (ob.
	SAP () ==sum) return true;
else return false;
	} int main () {int t,m;
	scanf ("%d", &t);
		while (t--) {scanf ("%d%d", &n,&m); Ob.
		Init (n+2); memset (Degree,0,sizeof (DEGree));
			for (int i=1;i<=m;i++) {int x,y,d;
			scanf ("%d%d%d", &x,&y,&d);
			degree[x]++;
			degree[y]--; if (d==0)//no-graph OB.
		ADD (1+x,1+y,1);
		} if (Judge (n)) printf ("possible\n");
	else printf ("impossible\n");
} 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.