Sightseeing Tour (Euler circuit for POJ 1637 hybrid graphs)

Source: Internet
Author: User

Language:DefaultSightseeing Tour
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7983 Accepted: 3346

Description

The City executive board in Lund wants-construct a sightseeing tour by bus in Lund, so-tourists can see every corn Er of the beautiful city. They want to construct, the tour so, every street in the city is visited exactly once. The bus should also start and end at the same junction. As in any city, the streets is either one-way or two-way, and traffic rules that must is obeyed by the tour bus. Help the Executive Board and determine if it's possible to construct a sightseeing tour under these constraints.

Input

On the first line of the input was a single positive integer n, telling the number of the test scenarios to follow. Each scenario begins with a line containing the positive integers m and s, 1 <= m <= 200,1 <= s <= The number of junctions and streets, respectively. The following S lines contain the streets. Each street was described with three integers, Xi, Yi, and Di, 1 <= xi,yi <= m, 0 <= di <= 1, where Xi and Yi a Re the junctions connected by a street. If Di=1, then the street was a one-way street (going from Xi to Yi), otherwise it ' s a two-way street. Assume that there exists a junction from the where all other junctions can be reached.

Output

For each scenario, output one line containing the text "possible" or "impossible", whether or not it's possible to Constru CT a sightseeing tour.

Sample Input

45 82 1 01 3 04 1 11 5 05 4 13 4 04 2 12 2 04 41 2 12 3 03 4 01 4 13 31 2 02 3 03 2 03 41 2 02 3 11 2 03 2 0

Sample Output

Possibleimpossibleimpossiblepossible

Source

Northwestern Europe 2003

Test instructions: To find out if the mixed graph has a Euler circuit.

Idea: The first is the base map Unicom (regardless of the degree of 0 points), and then need to rely on network flow to judge.

First of all, the non-directed side of the original map to a random direction (initial orientation), the original image is changed to a directed graph G ', and then the task is to change the direction of some side of G ' (of course, no side conversion, the original to the edge unchanged) so that it satisfies each point of the degree equal to the degrees.

Set D[i] to G ' Medium (point I of the out-point I of the degree). It can be seen that in the direction of Change G ', any point of the D value of the parity will not change (set the Edge <i,j> to <j,i>, then I in the degree plus 1 degrees minus 1,j in the degree minus 1 degrees plus 1, the difference of the two plus 2 or minus 2, parity unchanged)! And the final requirement is that each point in the degree equal to the degrees, that is, the D value of each point is 0, is an even number, therefore: if the initial direction obtained by the G ' of any point of the D value is odd, then there must be no Ora in the original image!

If the initial D value is even, then G ' is changed to network: Add the source point S and the meeting point T, for each d[i]>0 point I, the edge <s,i>, the capacity is D[I]/2; For each d[i]<0 Point J, the Edge <j,t>, the capacity is-d[j]/ 2;g ' Each of the non-directed edges remains in the network, with a capacity of 1 (indicating that the edge can only be changed in one direction at a time). To find the maximum flow of this network, if all the edges of s are full stream, then the original is Eulerian graph, otherwise it is not Eulerian graph.


Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 1005#define MAXN 222# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FRL (i,a,b) for (i = A; I < b; i++) #define Mem (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, & AMP;B) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n" ) const int MAXM = 3000;typedef Long Long ll;using namespace std;struct edge{int to,next,cap,flow;} Edge[maxm];int st,ed;int d[maxn];int tol,m,s;int head[maxn];int GAP[MAXN],Dep[maxn],pre[maxn],cur[maxn];void init () {tol=0; memset (head,-1,sizeof (Head));}     Add edge, one-way figure three parameters, bidirectional figure four parameters void Addedge (int u,int v,int w,int rw=0) {edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u]; edge[tol].flow=0;    head[u]=tol++; Edge[tol].to=u; EDGE[TOL].CAP=RW;    EDGE[TOL].NEXT=HEAD[V]; edge[tol].flow=0; head[v]=tol++;}    Input parameters: Starting point, end point, number of points//point number has no effect, as long as the total number of input points int sap (int start,int end,int N) {memset (gap,0,sizeof (GAP));    memset (dep,0,sizeof (DEP));    memcpy (cur,head,sizeof (head));    int U=start;    Pre[u]=-1;    Gap[0]=n;    int ans=0;            while (dep[start]<n) {if (u==end) {int min=inf; for (int i=pre[u];i!=-1;i=pre[edge[i^1].to]) if (min>edge[i].cap-edge[i].flow) min=ed            Ge[i].cap-edge[i].flow;                for (int i=pre[u];i!=-1;i=pre[edge[i^1].to]) {edge[i].flow+=min;            Edge[i^1].flow-=min;            } U=start;     Ans+=min;       Continue        } bool Flag=false;        int V;            for (int i=cur[u];i!=-1;i=edge[i].next) {v=edge[i].to;                if (Edge[i].cap-edge[i].flow && dep[v]+1==dep[u]) {flag=true;                Cur[u]=pre[v]=i;            Break            }} if (flag) {u=v;        Continue        } int min=n;            for (int i=head[u];i!=-1;i=edge[i].next) if (Edge[i].cap-edge[i].flow && dep[edge[i].to]<min)                {min=dep[edge[i].to];            Cur[u]=i;        } gap[dep[u]]--;        if (!gap[dep[u]]) return ans;        dep[u]=min+1;        gap[dep[u]]++;    if (U!=start) u=edge[pre[u]^1].to; } return ans;    int main () {int i,j,cas,u,v,w;    SF (CAS);        while (cas--) {SFF (m,s);        st=0;ed=m+1;        Init ();        MEM (d,0);            FRE (i,1,s) {sfff (u,v,w); d[u]++;           d[v]--;        if (w!=1) Addedge (u,v,1);        } bool Flag=true;        int outflow=0;                FRE (i,1,m) {if (ABS (d[i))%2==1) {flag=false;            Break                } if (d[i]>0) {Addedge (ST,I,D[I]/2);            OUTFLOW+=D[I]/2;        } if (d[i]<0) Addedge (I,ed,abs (D[i])/2);            } if (!flag) {printf ("impossible\n");        Continue        } int Maxflow=sap (st,ed,m+2);        if (maxflow==outflow) printf ("possible\n");    else printf ("impossible\n"); } return 0;} /*45 82 1 01 3 04 1 11 5 05 4 13 4 04 2 12 2 04 41 2 12 3 03 4 01 4 13 31 2 02 3 03 2 03 41 2 02 3 11 2 03 2 0*/



Sightseeing Tour (Euler circuit for POJ 1637 hybrid graphs)

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.