POJ1637 Sightseeing Tour (mixed-graph Euler circuit determination)

Source: Internet
Author: User
Tags in degrees mixed printf
Sightseeing Tour
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9709 Accepted: 4089

Description The City executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can se e every corner 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 Construct a sightseeing tour.

Sample Input

4
5 8
2 1 0
1 3 0 4
1 1
1 5 0
5 4 1
3 4 0
4 2 1
2 2 0
4 4
1 2 1 2
3 0
  3 4 0
1 4 1
3 3
1 2 0
2 3 0
3 2 0
3 4
1 2 0
2 3 1
1 2 0
3 2 0

Sample Output

Possible
Impossible
impossible
possible

Source Northwestern Europe 2003 —————————————————————————————— the title of the topic is to give a picture, there is the direction of the edge and no direction, whether there is Euler circuit thinking: the Euler circuit of the mixed graph to determine, First, the non-directional random value of a direction, to determine the degree of each point in the difference in degrees, if an odd number is not possible. Otherwise, further determination is made. You set the direction of the non-directed Dean, calculate the degree of each point in degrees, the weak penetration is greater than the degree of the source point and it is connected, and vice versa, to determine whether full stream

#include <iostream> #include <cstdio> #include <cstring> #include <string> #include < algorithm> #include <cmath> #include <map> #include <set> #include <stack> #include <queue
 
> #include <vector> #include <bitset> using namespace std;
#define LL Long Long const int INF = 0X3F3F3F3F;
#define MAXN-struct Node {int u, V, next, Cap;} EDGE[MAXN*MAXN];
int NT[MAXN], S[MAXN], D[MAXN], VISIT[MAXN];
int cnt;
int a[2005];
int b[2005];
 
struct ed {int u,v,f;} e[2005];
    void init () {cnt = 0;
Memset (S,-1, sizeof (s));
    } void Add (int u, int v, int c) {edge[cnt].u = u;
    EDGE[CNT].V = v;
    Edge[cnt].cap = C;
    Edge[cnt].next = S[u];
    S[u] = cnt++;
    edge[cnt].u = v;
    EDGE[CNT].V = u;
    Edge[cnt].cap = 0;
    Edge[cnt].next = S[v];
S[V] = cnt++;
    } bool BFS (int ss, int ee) {memset (d, 0, sizeof D);
    D[SS] = 1;
    queue<int>q;
    Q.push (ss); while (!q.emptY ()) {int pre = Q.front ();
        Q.pop ();
            for (int i = s[pre]; ~i; i = edge[i].next) {int v = EDGE[I].V;
                if (Edge[i].cap > 0 &&!d[v]) {D[v] = D[pre] + 1;
            Q.push (v);
}}} return D[ee]; } int DFS (int x, int. exp, int ee) {if (x = = ee| |!
    EXP) return exp;
    int temp,flow=0;
        for (int i = nt[x]; ~i; i = Edge[i].next, nt[x] = i) {int v = EDGE[I].V; if (d[v] = = D[x] + 1&& (temp = (DFS (V, MIN (exp, edge[i].cap), ee))) > 0) {edge[i].cap-= Te
            mp
            edge[i ^ 1].cap + = temp;
            Flow + = temp;
            Exp-= temp;
        if (!exp) break;
    }} if (!flow) d[x] = 0;
return flow;
    } int Dinic_flow (int ss, int ee) {int ans = 0;
        while (BFS (SS, ee)) {for (int i = 0; I <= ee; i++) nt[i] = s[i];
    ans+= DFS (ss, INF, EE);
   } return ans;
    } int main () {int T;
    int n,m; For (scanf ("%d", &t);
    t--;)
        {init ();
        scanf ("%d%d", &n,&m);
        memset (a,0,sizeof a);
        memset (b,0,sizeof b);
            for (int i=0; i<m; i++) {scanf ("%d%d%d", &AMP;E[I].U,&AMP;E[I].V,&AMP;E[I].F);
        a[e[i].u]++,b[e[i].v]++;
        } int flag=0;
                for (int i=1; i<=n; i++) if (((int) fabs (A[i]-b[i])) (%2==1) {flag=1;
            Break
            } if (!flag) {memset (a,0,sizeof a);
            memset (b,0,sizeof b);
 
                    for (int i=0; i<m; i++) {if (e[i].f==0) Add (e[i].u,e[i].v,1);
            a[e[i].u]++,b[e[i].v]++;
            } int ct=0;
                for (int i=1; i<=n; i++) if (A[i]<b[i]) Add (i,n+1, (B[i]-a[i])/2);
    else if (A[i]>b[i])                Add (0,i, (A[i]-b[i])/2), ct+= (A[i]-b[i])/2;
        if (Ct!=dinic_flow (0,n+1)) flag=1;
        } if (flag) printf ("impossible\n");
    else printf ("possible\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.