"Codeforces" 915 D. Almost acyclic Graph topological sort finding ring

Source: Internet
Author: User

"topic" D. Almost acyclic Graph

"Test Instructions" given n points of the direction of the graph (no heavy edges), asked whether the deletion of an edge so that the full picture is not ring. N<=500,m<=10^5.

"Algorithm" topology sequencing

"Solving" to find a simple ring, the edge you want to delete must pass through the ring. Try each edge on the ring (up to n edges) again to determine whether the full graph has a ring.

Topology sequencing to the simple ring: The remaining graph is the ring + ring inside the DAG,DFS process will walk into the dead end of the point mark-1, access to the superscript 1, find the point visited is a simple ring. Change the starting point until the ring is found.

Complexity of O (nm).

#include <cstdio>#include<queue>#include<algorithm>using namespacestd;Const intmaxn= -, maxm=100010;structedge{intV from;} E[MAXM];intMAP[MAXN][MAXN],TOT,CNT,N,M,FIRST[MAXN],P,VIS[MAXN],inch[Maxn],deg[maxn],suc[maxn];queue<int>Q;voidInsertintUintV) {Tot++;e[tot].v=v;e[tot]. from=first[u];first[u]=tot;inch[v]++;}voidDfsintXintFA) {    if(p| | vis[x]==-1)return; if(vis[x]==1) {p=x;suc[fa]=x;return;} VIS[X]=1;  for(intI=first[x];i;i=e[i]. from)if(deg[e[i].v]>0) {DFS (e[i].v,x); if(p) {if(Fa&&!suc[p]) suc[fa]=x; Break;} }    if(!p) vis[x]=-1;}BOOLSolveinto) {CNT=0;  for(intI=1; i<=n;i++) {deg[i]=inch[i];if(I==E[O].V) deg[i]--;if(deg[i]==0) Q.push (i), cnt++;}  while(!Q.empty ()) {        intx=Q.front ();        Q.pop ();  for(intI=first[x];i;i=e[i]. from)if(i!=o&&--deg[e[i].v]==0) Q.push (E[I].V), cnt++; }    if(cnt==n)return 1; return 0;} intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=m;i++){        intu,v; scanf ("%d%d",&u,&v); Insert (U,V); Map[u][v]=tot; } CNT=0;  for(intI=1; i<=n;i++) {deg[i]=inch[i];if(inch[i]==0) Q.push (i), cnt++;}  while(!Q.empty ()) {        intx=Q.front ();        Q.pop ();  for(intI=first[x];i;i=e[i]. from)if(--deg[e[i].v]==0) Q.push (E[I].V), cnt++; }    if(cnt==n) {printf ("YES");return 0;}  for(intI=1; i<=n;i++)if(deg[i]>0&AMP;&AMP;!P) DFS (i,0); intpp=p;  Do{        if(Solve (map[p][suc[p])) {printf ("YES");return 0;} P=Suc[p]; } while(p!=PP); printf ("NO"); return 0;}
View Code

Another solution: Enumeration point i,in[i]--, topological sorting to find the ring. This is equivalent to deleting an edge that points to n after the full-view look-up loop.

"Codeforces" 915 D. Almost acyclic Graph topological sort finding ring

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.