Files
python/TangDou/AcWing/DCC/ZJOI2004.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

53 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 200010, M = N << 1;
int n;
bool st[N];
// 链式前向星
int e[M], h[N], idx, w[M], ne[M];
void add(int a, int b, int c = 0) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
int ans = INF;
int root, ed;
// Tarjan算法求割点
int dfn[N], low[N], ts;
void tarjan(int u, int fa) {
low[u] = dfn[u] = ++ts;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (v == fa) continue;
if (!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
// u是割点
if (u != root && low[v] >= dfn[u]) {
if (u == ed) continue; // 根据题意,u必须在root和ed之间
if (dfn[ed] >= dfn[v]) ans = min(ans, u);
}
} else
low[u] = min(low[u], dfn[v]);
}
}
int main() {
// 初始化链式前向星
memset(h, -1, sizeof h);
scanf("%d", &n);
int x, y;
while (scanf("%d %d", &x, &y), x || y)
if (x != y) add(x, y), add(y, x);
scanf("%d %d", &root, &ed);
tarjan(root, -1); // 从其中一个信息中心开始遍历
if (ans == INF)
printf("No solution");
else
printf("%d", ans);
return 0;
}