HDU 5154 Harry and magical computer to the graph ring

Source: Internet
Author: User
Tags cmath

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=5154

Exercises

There is a circular to the graph.

1, with DFS, the node being accessed is labeled-1, the node that has been visited is marked as 1, the node that has not been visited is marked as 0, and if access to-1 node description is said to have a ring.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5 using namespacestd;6typedefLong LongLL;7 8 intn,m;9 Ten Const intmaxm=10000+Ten; One Const intmaxn=111; A  - structedge{ -     intV,ne; theEdge (intVintne): V (v), NE (NE) {} - Edge () {} - }EGS[MAXM]; -  + intHead[maxn],tot; - intVIS[MAXN]; +  A voidAddedge (intUintv) { ategs[tot]=Edge (V,head[u]); -head[u]=tot++;  - } -  - BOOLDfsintcur) { -vis[cur]=-1; in     intp=Head[cur]; -      while(p!=-1){ toEdge &e=Egs[p]; +         if(vis[e.v]==0){ -             if(Dfs (E.V))return true; the         } *         Else if(vis[e.v]==-1){ $             return true;Panax Notoginseng         }  -p=e.ne; the     } +vis[cur]=1; A     return false; the } +  - voidinit () { $memset (head,-1,sizeof(head)); $memset (Vis,0,sizeof(Vis)); -tot=0; - } the  - intMain () {Wuyi      while(SCANF ("%d%d", &n,&m) = =2&&N) { the init (); -          while(m--){ Wu             intu,v; -scanf"%d%d",&u,&v); About Addedge (u,v); $         }  -         intsu=1; -          for(intI=1; i<=n;i++){ -             if(vis[i]==0){ A                 if(Dfs (i)) { +su=0; Break; the                 } -             }  $         } the         if(SU) puts ("YES"); the         ElsePuts"NO");  the     } the     return 0; -}

2, topological sorting.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <queue>6 using namespacestd;7typedefLong LongLL;8 9 intN, M;Ten  One Const intMAXM =10000+Ten; A Const intMAXN =111; -  - structEdge { the     intV, NE; -Edge (intVintne): V (v), NE (NE) {} - Edge () {} - }EGS[MAXM]; +  - intHEAD[MAXN], tot; + intIND[MAXN]; A  at voidAddedge (intUintv) { -Egs[tot] =Edge (V, Head[u]); -Head[u] = tot++; - } -  - voidinit () { inMemset (Head,-1,sizeof(head)); -memset (Ind,0,sizeof(Ind)); totot =0; + } -  the intMain () { *      while(SCANF ("%d%d", &n, &m) = =2&&N) { $ init ();Panax Notoginseng          while(m--) { -             intu, v; thescanf"%d%d", &u, &v); + Addedge (U, v); Aind[v]++; the         } +queue<int>Q; -          for(inti =1; I <= N; i++) { $             if(Ind[i] = =0) Q.push (i); $         } -          while(!Q.empty ()) { -             intU =Q.front (); Q.pop (); the             //printf ("v:%d\n", v); -             intp =Head[u];Wuyi              while(P! =-1) { theedge& e =Egs[p]; -ind[e.v]--; Wu                 if(IND[E.V] = =0) Q.push (E.V); -p =e.ne; About             } $         } -         intSu =1; -          for(inti =1; I <= N; i++) { -             if(Ind[i]) {su =0; Break; } A         } +         if(SU) puts ("YES"); the         ElsePuts"NO"); -     } $     return 0; the}

HDU 5154 Harry and magical computer to the graph ring

Related Article

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.