首页 > 代码库 > POJ2762-Going from u to v or from v to u?(强连通缩点+DP)
POJ2762-Going from u to v or from v to u?(强连通缩点+DP)
题目链接
题意:给出一张有向图,判断图上的任意两个点是否存在一条路可达(单向可达即可)。
思路:有向图找出强连通分量,然后缩点,因为题目要求任意两点存在可达的路,所以缩点之后的点,要形成一条单链,才能符合可达的要求,在这里用DP求最长路来判断是否能形成一条单链。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int MAXN = 2010; const int MAXM = 50010; 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]; int n, m; int d[MAXN]; vector<int> g[MAXN]; void init() { tot = 0; memset(head, -1, sizeof(head)); } 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() { memset(Low, 0, sizeof(Low)); memset(DFN, 0, sizeof(DFN)); memset(num, 0, sizeof(num)); memset(Stack, 0, sizeof(Stack)); memset(Instack, false, sizeof(Instack)); Index = scc = top = 0; for (int i = 1; i <= n; i++) if (!DFN[i]) Tarjan(i); } int dp(int i) { int& ans = d[i]; if (ans > 0) return ans; ans = 1; for (int j = 0; j < g[i].size(); j++) { int v = g[i][j]; ans = max(ans, dp(v) + 1); } return ans; } int main() { int cas; scanf("%d", &cas); while (cas--) { init(); scanf("%d%d", &n, &m); int u, v; for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); addedge(u, v); } solve(); if (scc == 1) { printf("Yes\n"); continue; } else { for (int i = 0; i <= scc; i++) g[i].clear(); for (int u = 1; u <= n; u++) { for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (Belong[u] != Belong[v]) g[Belong[u]].push_back(Belong[v]); } } memset(d, -1, sizeof(d)); int ans = 0; for (int i = 1; i <= scc; i++) ans = max(ans, dp(i)); if (ans == scc) printf("Yes\n"); else printf("No\n"); } } return 0; }
POJ2762-Going from u to v or from v to u?(强连通缩点+DP)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。