本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/15621588.html

题目链接

首先如果一个点满足答案,则这个点一定是割点。

然后我们可以从 aa 点开始搜,对于每一个点,如果 bb 点在它的儿子内,说明这个点分离了 aabb

如何判断 bb 是否在它的儿子内,只需要在搜索这个儿子前后判断一下即可。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Problem: P5058 [ZJOI2004]嗅探器
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5058
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define M 500010
//#define mo
#define N 200010
struct node
{
int x, y, n;
}d[M*2];
int n, m, i, j, k;
int dep[N], f[N], h[N], zi[N];
int u, v, ans, a, b;

void cun(int x, int y)
{
d[++k].x=x; d[k].y=y;
d[k].n=h[x]; h[x]=k;
}

void dfs(int x)
{
int flag=0;
for(int g=h[x]; g; g=d[g].n)
{
int y=d[g].y;
if(!dep[y])
{
int flg=(dep[b]!=0 ? 0 : 1);
dep[y]=dep[x]+1;
dfs(y);
if(dep[f[y]]>=dep[x])
{
++zi[x];
if(dep[b]) flag+=flg;
}
if(dep[f[y]]<dep[f[x]]) f[x]=f[y];
}
else if(dep[y]<dep[f[x]]) f[x]=y;
}
// printf("%lld: %lld %lld %lld\n", x, f[x], zi[x], flag);
if(x==b||x==a) flag=0;
if(!(zi[x]>1||(x!=a&&zi[x]))) flag=0;
if(flag)
{
ans=min(ans, x);
// printf("%lld\n", x);
}

}

signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
n=read(); ans=0x3f3f3f3f;
for(i=1; i<=n; ++i) f[i]=i;
u=read(); v=read();
while(u) cun(u, v), cun(v, u), u=read(), v=read();
a=read(); b=read();
dep[a]=1; dfs(a);
// for(i=1; i<=n; ++i) printf("%lld ", f[i]);
if(ans==0x3f3f3f3f) printf("No solution");
else printf("%lld", ans);
return 0;
}