HDU 1269 迷宫城堡
题意:中文题
思路:强连通模板题
代码:
#include <cstdio> #include <cstring> #include <vector> #include <stack> using namespace std; const int N = 10005; int n, m; vector<int> g[N], scc[N]; int pre[N], lowlink[N], sccno[N], dfs_clock, scc_cnt; stack<int> S; void dfs_scc(int u) { pre[u] = lowlink[u] = ++dfs_clock; S.push(u); for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (!pre[v]) { dfs_scc(v); lowlink[u] = min(lowlink[u], lowlink[v]); } else if (!sccno[v]) lowlink[u] = min(lowlink[u], pre[v]); } if (lowlink[u] == pre[u]) { scc_cnt++; int x = S.top(); while (x != u) { sccno[x] = scc_cnt; S.pop(); x = S.top(); } } } void find_scc(int n) { dfs_clock = scc_cnt = 0; memset(sccno, 0, sizeof(sccno)); memset(pre, 0, sizeof(pre)); for (int i = 1; i <= n; i++) if (!pre[i]) dfs_scc(i); } int main() { while (~scanf("%d%d", &n, &m) && n || m) { int u, v; for (int i = 1; i <= n; i++) g[i].clear(); while (m--) { scanf("%d%d", &u, &v); g[u].push_back(v); } find_scc(n); if (scc_cnt <= 1) printf("Yes\n"); else printf("No\n"); } return 0; }
时间: 2024-10-13 07:26:53