题目连接

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

迷宫城堡

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

强联通分量裸题。。

Tarjan求强联通分量参见:https://www.byvoid.com/blog/scc-tarjan/

#include<bits/stdc++.h>
using namespace std;
const int N = 10100;
struct Tarjan_Scc {
stack<int>s;
bool instack[N];
struct edge { int to, next; }G[N * 10];
int scc, idx, tot, dfn[N], low[N], head[N];
inline void init(int n) {
scc = tot = idx = 0;
while (!s.empty()) s.pop();
for (int i = 0; i < n + 2; i++) {
head[i] = -1;
instack[i] = false;
dfn[i] = low[i] = 0;
}
}
inline void add_edge(int u, int v) {
G[tot].to = v, G[tot].next = head[u], head[u] = tot++;
}
inline void built(int m) {
int u, v;
while (m--) {
scanf("%d %d", &u, &v);
add_edge(u, v);
}
}
inline void tarjan(int u) {
dfn[u] = low[u] = ++idx;
s.push(u);
instack[u] = true;
for (int i = head[u]; ~i; i = G[i].next) {
int &v = G[i].to;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (instack[v] && dfn[v] < low[u]) {
low[u] = dfn[v];
}
}
if (low[u] == dfn[u]) {
scc++;
int v = 0;
do {
v = s.top(); s.pop();
instack[v] = false;
} while (v != u);
}
}
inline void solve(int n, int m) {
init(n);
built(m);
for (int i = 1; i <= n; i++) {
if (!dfn[i]) tarjan(i);
}
puts(scc == 1 ? "Yes" : "No");
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, m;
while (~scanf("%d %d", &n, &m), m + n) {
go.solve(n, m);
}
return 0;
}

hdu 1269 迷宫城堡的更多相关文章

  1. HDU 1269 迷宫城堡(强连通)

    HDU 1269 迷宫城堡 pid=1269" target="_blank" style="">题目链接 题意:中文题 思路:强连通模板题 代 ...

  2. hdu 1269 迷宫城堡 强连通分量

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. HDU 1269 迷宫城堡(判断有向图强连通分量的个数,tarjan算法)

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. hdu 1269 迷宫城堡 最简单的联通图题 kosaraju缩点算法

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  5. hdu 1269 迷宫城堡(Targin算法)

    ---恢复内容开始--- 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. HDU 1269.迷宫城堡-Tarjan or 双向DFS

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. HDU 1269 迷宫城堡 (Kosaraju)

    题目链接:HDU 1269 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000), ...

  8. HDU 1269 迷宫城堡(DFS)

    迷宫城堡 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的 ...

  9. HDU 1269 迷宫城堡(向量)(Tarjan模版题)

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  2. NTP服务器地址及IP

    yum install ntp */20 * * * * /usr/sbin/ntpdate 61.172.254.29 210.72.145.44 (国家授时中心服务器IP地址)133.100.11 ...

  3. epoll在socket通信中的应用

    当服务器需要服务多个客户时,需要使用并发通信,实现并发通信有以下几种方法: 1.在服务器中fork子进程来为每个客户服务  具体可参考http://www.cnblogs.com/ggjucheng/ ...

  4. Git 图解剖析

    git中文件内容并没有真正存储在索引(.git/index)或者提交对象中,而是以blob的形式分别存储在数据库中(.git/objects),并用SHA-1值来校验. 索引文件用识别码列出相关的bl ...

  5. 设置lable文本内容的行间距

    NSMutableParagraphStyle *paragraphStyle =[ [NSMutableParagraphStyle alloc] init]; paragraphStyle.lin ...

  6. Duilib扩展《01》— 双击、右键消息扩展

    用过duilib的可能会发现,duilib中有些控件没能很好的区分左键.右键等消息.所以根据实际需要,我们需要进行相关区分处理,或者自行扩展. 一. 左键.右键消息区分 我们以CListUI控件来分析 ...

  7. javascript 同步加载与异步加载

    HTML 4.01 的script属性 charset: 可选.指定src引入代码的字符集,大多数浏览器忽略该值. defer: boolean, 可选.延迟脚本执行,相当于将script标签放入页面 ...

  8. C# 多线程传参

    using System; using System.Threading; //多线程调试: 2013.10.08 namespace ThreadExample { class App { publ ...

  9. 简短总结一下C#里跨线程更新UI

    摘自: http://my.oschina.net/sdqxcxh/blog/53707 跨线程更新UI是写多线程程序尤其是通信类的程序经常遇到的问题,这里面主要的问题是冲突,比如数据线程想要更新UI ...

  10. nginx之keepalive

    一:设置 keepalive_timeout  0; 发curl: [xxx ~]$ curl -H "Keep-Alive: 60" -H "Connection: k ...