题面

题目链接

大秦 为你打开 题目传送门

备用题面

求割点。

思路

那么这道题既然是求割点的模板题,我们就来浅浅回顾一下 Tarjan 求割点是怎么回事。

对于 Tarjan 最最基础的一些变量定义,不知道建议直接 出门右转看我 Tarjan 博文

那么言归正传,其实说割点就是 low 值大于 dfn 值,因为如果我最小的点就是到达我的子孙方向的,那么我就是必经之路,所以这个点就是割点。另外这个点如果是根要特判,如果这个根有大于等于 2 的儿子,那么它还是割点。

代码

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#pragma GCC optimize(2)
#pragma GCC optimize(3, "Ofast", "inline")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef __int128 int128;

namespace FastIO
{
char buf[1 << 20], *p1, *p2;
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)
template<typename T> inline T read(T& x) {
x = 0;
int f = 1;
char ch;
while (!isdigit(ch = getchar())) if (ch == '-') f = -1;
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
x *= f;
return x;
}
template<typename T, typename... Args> inline void read(T& x, Args &...x_) {
read(x);
read(x_...);
return;
}
inline ll read() {
ll x;
read(x);
return x;
}
};
using namespace FastIO;

const int N = 1010;
const int M = 2010;

class Graph {
private :
struct Edge {
int to, nt, wt;
Edge() {}
Edge(int to, int nt, int wt) : to(to), nt(nt), wt(wt) {}
}e[M];
int hd[N], cnte;
public :
inline void AddEdge(int u, int v, int w = 0) {
e[++cnte] = Edge(v, hd[u], w);
hd[u] = cnte;
}
inline int head(int u) { return hd[u]; }
inline int nt(int u) { return e[u].nt; }
inline int to(int u) { return e[u].to; }
inline int wt(int u) { return e[u].wt; }
};

int n;
Graph e;

char s[1010];

inline int Getnumber(char* s, int &pos) {
int x = 0;
while(s[pos] && isdigit(s[pos])) x = x * 10 + s[pos++] - '0';
while(s[pos] && !isdigit(s[pos])) pos++;
return x;
}

inline void Input() {
read(n);
while(fgets(s + 1, 1000, stdin)) {
int pos = 1;
int u = Getnumber(s, pos);
// cout << u << ' ';
while(s[pos]) {
int v = Getnumber(s, pos);
e.AddEdge(u, v);
e.AddEdge(v, u);
// cout << u << ' ' << v << endl;
// cout << 1 << endl;
// cout << v << ' ';
}
// cout << endl;
}
}

int low[N], dfn[N], cntd;
int ans;

inline void Tarjan(int u, int fa) {
low[u] = dfn[u] = ++cntd;
int flag = 0;
for(int i = e.head(u); i ; i = e.nt(i)) {
int v = e.to(i);
// if(v == fa) continue;
if(!dfn[v]) {
Tarjan(v, u);
low[u] = min(low[v], low[u]);
if(u != 1 && low[v] >= dfn[u]) {
flag = 1;
}
}
else if(v != fa){
low[u] = min(low[u], dfn[v]);
}
}
ans += flag;
}

inline void Work() {
Tarjan(1, 1);
printf("%d", ans);
}

int main() {
int T = 1;
while(T--) {
Input();
Work();
}
return 0;
}