hdu 1269 (强联通分量Tarjan入门)
2015-09-04 17:43
411 查看
迷宫城堡
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10075 Accepted Submission(s): 4529
[align=left]Problem Description[/align]
为
了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单
向的,就是说若称某通道连通了A房间和B房间,只说明可以通过这个通道由A房间到达B房间,但并不说明通过它可以由B房间到达A房间。Gardon需要请
你写个程序确认一下是否任意两个房间都是相互连通的,即:对于任意的i和j,至少存在一条路径可以从房间i到房间j,也存在一条路径可以从房间j到房间
i。
[align=left]Input[/align]
输入包含多组数据,输入的第一行有两个数:N和M,接下来的M行每行有两个数a和b,表示了一条通道可以从A房间来到B房间。文件最后以两个0结束。
[align=left]Output[/align]
对于输入的每组数据,如果任意两个房间都是相互连接的,输出"Yes",否则输出"No"。
[align=left]Sample Input[/align]
3 3
1 2
2 3
3 1
3 3
1 2
2 3
3 2
0 0
[align=left]Sample Output[/align]
Yes
No
[align=left]Author[/align]
Gardon
[align=left]Source[/align]
HDU 2006-4 Programming Contest
模板~
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #define ll long long using namespace std; const int MAXN = 12000; const int MAXM = 120000; struct Edge { int to,next; }edge[MAXM]; int head[MAXN],tot; int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN]; int Index,top; int scc; bool Instack[MAXN]; int num[MAXN]; void addedge(int u,int v) { edge[tot].to = v;edge[tot].next = head[u];head[u] = tot++; } void Tarjan(int u) { int v; Low[u] = DFN[u] = ++Index; Stack[top++] = u; Instack[u] = true; for(int i = head[u]; i != -1; i = edge[i].next) { v = edge[i].to; if( !DFN[v] ) { Tarjan(v); if(Low[u] > Low[v]) Low[u] = Low[v]; } else if(Instack[v] && Low[u] > DFN[v]) Low[u] = DFN[v]; } if(Low[u] == DFN[u]) { scc++; do { v = Stack[--top]; Instack[v] = false; Belong[v] = scc; num[scc]++; } while(v != u); } } void solve(int N) { memset(DFN,0,sizeof(DFN)); memset(Instack,false,sizeof(Instack)); memset(num,0,sizeof(num)); Index = scc = top = 0; for(int i = 1; i <= N; i++) if( !DFN[i]) Tarjan(i); } void init() { tot = 0; memset(head,-1,sizeof(head)); } int main(void) { int n,m,a,b; while(scanf("%d %d",&n,&m) ,n != 0 || m != 0) { init(); for(int i = 0; i < m; i++) { scanf("%d %d",&a,&b); addedge(a,b); } solve(n); if(scc == 1) printf("Yes\n"); else printf("No\n"); } return 0; }
相关文章推荐
- 获取系统任务栏高度
- 条款09 绝不在构造和析构过程中调用 virtual 函数
- 平衡二叉树
- iOS 通过数字拼音快速搜索股票
- JAVA建立泛型数组
- android support Percent支持库开发
- Maven下的SpringMVC4.2+hibernate4.3整合
- hdu 1540(区间合并)
- UVa 12627 Erratic Expansion 奇怪的气球膨胀 (分治_递归) 白书P245
- 有关eclipse的快捷键
- linux ip命令代替ifconfig命令
- 096 Unique Binary Search Trees [Leetcode]
- poj 1144 Network(割点 入门)
- javascript学习笔记(四):事件处理函数和动态创建html标记。
- Gulp折腾记 - (1)简易入门篇
- 关于editplus语法高亮和自动补全
- Gulp折腾记 - (1)简易入门篇
- libvirt、kvm、qumu等之间的关系
- LA -3644 X-Plosives -并查集
- 总结Unity 初学者容易犯的编译与运行时错误(第二部分)