首页 > 其他 > 详细

hdu 1269 迷宫城堡

时间:2014-07-23 20:38:55      阅读:415      评论:0      收藏:0      [点我收藏+]

迷宫城堡

Time Limit: 2000/1000 MS (Java/Others)    

Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6826    Accepted Submission(s): 3034


Problem Description
为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明可以通过这个通道由A房间到达B房间,但并不说明通过它可以由B房间到达A房间。Gardon需要请你写个程序确认一下是否任意两个房间都是相互连通的,即:对于任意的i和j,至少存在一条路径可以从房间i到房间j,也存在一条路径可以从房间j到房间i。
 

 

Input
输入包含多组数据,输入的第一行有两个数:N和M,接下来的M行每行有两个数a和b,表示了一条通道可以从A房间来到B房间。文件最后以两个0结束。
 

 

Output
对于输入的每组数据,如果任意两个房间都是相互连接的,输出"Yes",否则输出"No"。
 

 

Sample Input
3 3 1 2 2 3 3 1 3 3 1 2 2 3 3 2 0 0
 

 

Sample Output
Yes
No
 

 

Author
Gardon
 

 

Source
 
解题:强连通图判断,如果当前迷宫是一个强连通图,那么即Yes
 
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define INF 0x3f3f3f3f
15 using namespace std;
16 const int maxv = 10010;
17 const int maxe = 100010;
18 int dfn[maxv],low[maxv],n,m,index,scc;
19 vector<int>g[maxv];
20 bool instack[maxv];
21 stack<int>s;
22 void tarjan(int u){
23     s.push(u);
24     instack[u] = true;
25     dfn[u] = low[u] = ++index;
26     for(int i = 0; i < g[u].size(); i++){
27         int v = g[u][i];
28         if(!dfn[v]){
29             tarjan(v);
30             low[u] = min(low[u],low[v]);
31         }else if(instack[u] && low[u] > dfn[v]) low[u] = dfn[v];
32     }
33     if(dfn[u] == low[u]){
34         int v;
35         scc++;
36         do{
37             v = s.top();
38             s.pop();
39         }while(v != u);
40     }
41 }
42 int main(){
43     int i,j,u,v;
44     while(scanf("%d%d",&n,&m),n+m){
45         for(i = 1; i <= n; i++)
46             g[i].clear();
47         memset(dfn,0,sizeof(dfn));
48         memset(low,0,sizeof(low));
49         memset(instack,false,sizeof(instack));
50         for(i = 0; i < m; i++){
51             scanf("%d%d",&u,&v);
52             g[u].push_back(v);
53         }
54         while(!s.empty()) s.pop();
55         index = scc = 0;
56         for(i = 1; i <= n; i++)
57             if(!dfn[i]) tarjan(i);
58         scc==1?puts("Yes"):puts("No");
59     }
60     return 0;
61 }
View Code

 

上面的代码有点傻逼,冗余了一些计算。。。

 

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define INF 0x3f3f3f3f
15 using namespace std;
16 const int maxv = 10010;
17 const int maxe = 100010;
18 int dfn[maxv],low[maxv],n,m,index,scc;
19 vector<int>g[maxv];
20 bool instack[maxv],flag;
21 stack<int>s;
22 void tarjan(int u){
23     s.push(u);
24     instack[u] = true;
25     dfn[u] = low[u] = ++index;
26     for(int i = 0; i < g[u].size(); i++){
27         int v = g[u][i];
28         if(!dfn[v]){
29             tarjan(v);
30             low[u] = min(low[u],low[v]);
31         }else if(instack[u] && low[u] > dfn[v]) low[u] = dfn[v];
32     }
33     if(dfn[u] == low[u]){
34         int v,o = 0;
35         do{
36             v = s.top();
37             s.pop();
38             o++;
39         }while(v != u);
40         if(o == n) flag = true;
41     }
42 }
43 int main(){
44     int i,j,u,v;
45     while(scanf("%d%d",&n,&m),n+m){
46         for(i = 1; i <= n; i++){
47             g[i].clear();
48             dfn[i] = 0;
49             low[i] = 0;
50             instack[i] = false;
51         }
52         for(i = 0; i < m; i++){
53             scanf("%d%d",&u,&v);
54             g[u].push_back(v);
55         }
56         while(!s.empty()) s.pop();
57         flag = index = 0;
58         tarjan(1);
59         flag?puts("Yes"):puts("No");
60     }
61     return 0;
62 }
View Code

 

美观点

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define INF 0x3f3f3f3f
15 using namespace std;
16 const int maxv = 10010;
17 const int maxe = 100010;
18 int dfn[maxv],low[maxv],n,m,index,scc;
19 vector<int>g[maxv];
20 bool instack[maxv];
21 stack<int>s;
22 bool tarjan(int u) {
23     s.push(u);
24     instack[u] = true;
25     dfn[u] = low[u] = ++index;
26     for(int i = 0; i < g[u].size(); i++) {
27         int v = g[u][i];
28         if(!dfn[v]) {
29             if(tarjan(v)) return true;
30             low[u] = min(low[u],low[v]);
31         } else if(instack[u] && low[u] > dfn[v]) low[u] = dfn[v];
32     }
33     if(dfn[u] == low[u]) {
34         int v,o = 0;
35         do {
36             v = s.top();
37             s.pop();
38             o++;
39         } while(v != u);
40         if(o == n) return true;
41     }
42     return false;
43 }
44 int main() {
45     int i,j,u,v;
46     while(scanf("%d%d",&n,&m),n+m) {
47         for(i = 1; i <= n; i++) {
48             g[i].clear();
49             dfn[i] = 0;
50             low[i] = 0;
51             instack[i] = false;
52         }
53         for(i = 0; i < m; i++) {
54             scanf("%d%d",&u,&v);
55             g[u].push_back(v);
56         }
57         while(!s.empty()) s.pop();
58         index = 0;
59         tarjan(1)?puts("Yes"):puts("No");
60     }
61     return 0;
62 }
View Code

 

hdu 1269 迷宫城堡,布布扣,bubuko.com

hdu 1269 迷宫城堡

原文:http://www.cnblogs.com/crackpotisback/p/3863795.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!