HDU 5154 Harry and Magical Computer 有向圖判環

來源:互聯網
上載者:User

標籤:

題目連結:

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

題解:

有向圖判環。

1、用dfs,正在訪問的節點標記為-1,已經訪問過的節點標記為1,沒有訪問過的節點標記為0,如果訪問到-1的節點說明說有環。

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 typedef long long LL; 7  8 int n,m;  9 10 const int maxm=10000+10;11 const int maxn=111;12 13 struct Edge{14     int v,ne;15     Edge(int v,int ne):v(v),ne(ne){}16     Edge(){}17 }egs[maxm];18 19 int head[maxn],tot;20 int vis[maxn];21 22 void addEdge(int u,int v){23     egs[tot]=Edge(v,head[u]);24     head[u]=tot++; 25 }26 27 bool dfs(int cur){28     vis[cur]=-1;29     int p=head[cur];30     while(p!=-1){31         Edge &e=egs[p];32         if(vis[e.v]==0){33             if(dfs(e.v)) return true;34         }35         else if(vis[e.v]==-1){36             return true;37         } 38         p=e.ne;39     }40     vis[cur]=1;41     return false;42 }43 44 void init(){45     memset(head,-1,sizeof(head));46     memset(vis,0,sizeof(vis));47     tot=0;48 }49 50 int main(){51     while(scanf("%d%d",&n,&m)==2&&n){52         init();53         while(m--){54             int u,v;55             scanf("%d%d",&u,&v);56             addEdge(u,v);57         } 58         int su=1;59         for(int i=1;i<=n;i++){60             if(vis[i]==0){61                 if(dfs(i)){62                     su=0; break;63                 }64             } 65         }66         if(su) puts("YES");67         else puts("NO"); 68     }69     return 0;70 }

2、拓撲排序。

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 using namespace std; 7 typedef long long LL; 8  9 int n, m;10 11 const int maxm = 10000 + 10;12 const int maxn = 111;13 14 struct Edge {15     int v, ne;16     Edge(int v, int ne) :v(v), ne(ne) {}17     Edge() {}18 }egs[maxm];19 20 int head[maxn], tot;21 int ind[maxn];22 23 void addEdge(int u, int v) {24     egs[tot] = Edge(v, head[u]);25     head[u] = tot++;26 }27 28 void init() {29     memset(head, -1, sizeof(head));30     memset(ind, 0, sizeof(ind));31     tot = 0;32 }33 34 int main() {35     while (scanf("%d%d", &n, &m) == 2 && n) {36         init();37         while (m--) {38             int u, v;39             scanf("%d%d", &u, &v);40             addEdge(u, v);41             ind[v]++;42         }43         queue<int> q;44         for (int i = 1; i <= n; i++) {45             if (ind[i] == 0) q.push(i);46         }47         while (!q.empty()) {48             int u = q.front(); q.pop();49             //printf("v:%d\n", v);50             int p = head[u];51             while (p != -1) {52                 Edge& e = egs[p];53                 ind[e.v]--;54                 if (ind[e.v] == 0) q.push(e.v);55                 p = e.ne;56             }57         }58         int su = 1;59         for (int i = 1; i <= n; i++) {60             if (ind[i]) { su = 0; break; }61         }62         if (su) puts("YES");63         else puts("NO");64     }65     return 0;66 }

 

HDU 5154 Harry and Magical Computer 有向圖判環

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.