首页 技术 正文
技术 2022年11月9日
0 收藏 921 点赞 2,684 浏览 1604 个字

迷宫城堡
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21627    Accepted Submission(s): 9417Problem 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

C/C++:

 #include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int my_max = ; int n, m, a, b, scc_cnt, my_dfn[my_max], my_low[my_max], my_belong[my_max],
my_stack[my_max], my_top, my_index, is_stack[my_max];
vector <int> my_map[my_max]; void my_tarjan(int u)
{
my_stack[my_top ++] = u;
my_dfn[u] = my_low[u] = my_index ++;
is_stack[u] = ;
for (int i = ; i < my_map[u].size(); ++ i)
{
int temp = my_map[u][i];
if (!my_dfn[temp])
{
my_tarjan(temp);
my_low[u] = min(my_low[u], my_low[temp]);
}
else if (is_stack[temp])
my_low[u] = min(my_low[u], my_dfn[temp]);
}
if (my_dfn[u] == my_low[u])
{
scc_cnt ++;
int temp;
do{
temp = my_stack[-- my_top];
is_stack[temp] = ;
my_belong[temp] = scc_cnt;
} while (temp != u);
}
} int main()
{
while (scanf("%d%d", &n, &m), n || m)
{
for (int i = ; i <= n; ++ i) my_map[i].clear();
scc_cnt = my_top = my_index = ;
for (int i = ; i <= n; ++ i)
my_dfn[i] = my_low[i] = is_stack[i] = ;
while (m --)
{
scanf("%d%d", &a, &b);
my_map[a].push_back(b);
} for (int i = ; i <= n; ++ i)
if (!my_dfn[i])
my_tarjan(i);
printf("%s\n", scc_cnt == ? "Yes" : "No");
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290