POJ 1438 One-way traffic

Source: Internet
Author: User

Serie A champions:

Unlike the http://blog.csdn.net/houserabbit/article/details/38958891 category, it is not possible to change the original graph into a mixed graph.

Ideas:

In the previous article I also wrote the http://blog.csdn.net/houserabbit/article/details/38958891 first is to find a bridge then you need to first turn the mixed diagram into an image of the problem because the answer exists so the bridge must be the non-edge of the mixed graph

And then the work within the block is divided into two kinds of side discussion is only to determine whether the side is left to see if it is not the original image of the non-aligned side because there is no moving side of the last slightly change the output is basically consistent with the previous article

So we can come up with a way of thinking. A method for constructing strong connected graphs based on censored edges--finding bridge + block DFS in 2 kinds of side discussions I used to do it before. The approximate method of constructing strong connected graph based on edge addition--the discussion of strong connected indentation

Code:

#include <cstdio> #include <iostream> #include <string> #include <cstring> #include < algorithm> #include <cmath> #include <map> #include <set> #include <vector>using namespace Std;typedef long long LL; #define N 2010#define M 4000010#define inf 2147483647int n,m,t=1,tot,idx;int head[n],dfn[n],low[    n];struct edge{int u,v,next; BOOL Vis,cut,left,dir,exit;}    ed[m];void Add (int u,int v,bool Dir,bool exit) {ed[tot].u=u;    Ed[tot].v=v;    Ed[tot].next=head[u];    Ed[tot].vis=ed[tot].cut=ed[tot].left=false;    Ed[tot].dir=dir;    Ed[tot].exit=exit; head[u]=tot++;}    void Tarjan (int u) {int i,v;    Dfn[u]=low[u]=++idx;        for (I=head[u];~i;i=ed[i].next) {v=ed[i].v;        if (Ed[i].vis) continue;ed[i].vis=ed[i^1].vis=true;            if (dfn[v]==-1) {Tarjan (v);            Low[u]=min (Low[u],low[v]);                if (Dfn[u]<low[v]) {ed[i].cut=ed[i^1].cut=true; Ed[i].left=ed[i^1].left=tRue    }} else Low[u]=min (Low[u],dfn[v]);    }}void dfs (int u) {int i,v;    Dfn[u]=low[u]=++idx; for (I=head[u];~i;i=ed[i].next) {if (ed[i].cut| |!        Ed[i].exit) continue;        V=ED[I].V;            if (dfn[v]==-1) {ed[i].vis=ed[i^1].vis=true;            DFS (v);            Low[u]=min (Low[u],low[v]);                if (!ed[i].dir) {if (low[v]>dfn[u]) ed[i^1].left=true;            else ed[i].left=true;            }} else {low[u]=min (low[u],dfn[v]);            if (!ed[i].vis&&!ed[i].dir) ed[i].left=true;        Ed[i].vis=ed[i^1].vis=true; }}}void Solve () {int I;memset (dfn,-1,sizeof (DFN)); Idx=0;tarjan (1); Memset (Dfn,-1,sizeof (DFN)); Idx=0;for (i=0;i<    tot;i++) ed[i].vis=false;for (i=1;i<=n;i++) {if (dfn[i]==-1) DFS (i);    }}int Main () {int i,u,v,k;        while (~SCANF ("%d%d", &n,&m)) {tot=0;    memset (head,-1,sizeof (head));    for (i=1;i<=m;i++) {scanf ("%d%d%d", &u,&v,&k);                if (k&1) {Add (u,v,true,true);            Add (V,u,true,false);                } else {Add (u,v,false,true);            Add (v,u,false,true);        }} solve ();            for (i=0;i<tot;i+=2) {if (ed[i].dir) continue;            if (ed[i].left&&ed[i^1].left) printf ("%d%d 2\n", ED[I].U,ED[I].V);            else if (ed[i].left) printf ("%d%d 1\n", ED[I].U,ED[I].V);        else printf ("%d%d 1\n", ed[i].v,ed[i].u); }}return 0;}


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

POJ 1438 One-way traffic

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.