POJ 2762 going from U-V or from V to u? (Strongly connected components + topological ordering)

Source: Internet
Author: User

Title Address: POJ 2762
First, then determine whether the number of layers per layer in the topology network is 1 (I admit that if I don't know the problem in advance, I can't think of a topological sort.) )。 Because if there is a layer of 2, then the two fork points will not be from one point to another.
The code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace STD;#define LL __int64#define PI ACOs ( -1.0)Const intMod=1e9+7;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-9;Const intmaxn= ++Ten;intHEAD[MAXN], ecnt, SCC, top, indx;intLOW[MAXN], DFN[MAXN], BELONG[MAXN], STK[MAXN], INSTACK[MAXN];structnode{intU, V, next;} edge[7000];voidAddintUintV) {edge[ecnt].v=v;        Edge[ecnt].next=head[u]; head[u]=ecnt++;}voidTarjan (intu) {low[u]=dfn[u]=++indx; instack[u]=1; Stk[++top]=u; for(inti=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(!dfn[v])                        {Tarjan (v);                Low[u]=min (Low[u],low[v]); }Else if(Instack[v])                {low[u]=min (low[u],dfn[v]); }        }if(Low[u]==dfn[u]) {scc++; while(1){intv=stk[top--];                        BELONG[V]=SCC; instack[v]=0;if(U==V) Break; }        }}voidInit () {memset(head,-1,sizeof(head));memset(DFN,0,sizeof(DFN));memset(Instack,0,sizeof(Instack)); Ecnt=top=indx=scc=0;}intHEAD1[MAXN], Ecnt1, DEG[MAXN];structnode1{intU, V, next;} edge1[7000];voidADD1 (intUintV) {edge1[ecnt1].v=v;        Edge1[ecnt1].next=head1[u]; head1[u]=ecnt1++;}BOOLTopointSCC) {intI, u, tot, M=SCC; while(m--) {tot=0; for(i=1; i<=scc;i++) {if(!deg[i])                                {tot++;                                U=i;                        deg[i]--; }                }if(tot>1)return false; for(i=head1[u];i!=-1; i=edge1[i].next) {deg[edge1[i].v]--; }        }return true;}voidInit1 () {memset(DEG,0,sizeof(deg));memset(head1,-1,sizeof(HEAD1)); ecnt1=0;}intMain () {intN, M, I, J, u, V, T, F;scanf("%d", &t); while(t--) {scanf("%d%d", &n,&m); Init (); while(m--) {scanf("%d%d", &u,&v);                Add (U,V); } for(i=1; i<=n;i++) {if(!dfn[i]) Tarjan (i); } init1 (); for(i=1; i<=n;i++) { for(j=head[i];j!=-1; j=edge[j].next) {v=edge[j].v;if(Belong[i]!=belong[v])                                        {add1 (belong[i],belong[v]); deg[belong[v]]++;//printf ("%d%d\n", Belong[i],belong[v]);}                        }                }if(Topo (SCC))puts("Yes");Else puts("No"); }return 0;}

POJ 2762 going from u to V or from V to u? (strongly connected component + topological sort)

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.