本文主要是介绍HDU 1269 迷宫城堡(强连通),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;
}
这篇关于HDU 1269 迷宫城堡(强连通)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!