Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7a80b36

Browse files
Add articulation points
1 parent 9a2a833 commit 7a80b36

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

‎articultion_points.cpp‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define INF 1e18
4+
using namespace std;
5+
6+
vector<vector<ll>> adj;
7+
vector<ll> low, discover;
8+
vector<bool> visited;
9+
set<ll> articulation_pts;
10+
ll timer = 0;
11+
12+
13+
void explore(ll u, ll p = 0) {
14+
visited[u] = true;
15+
discover[u] = low[u] = ++timer;
16+
17+
ll children = 0;
18+
for (ll v : adj[u]) {
19+
if (visited[v] == false) {
20+
children++;
21+
explore(v,u);
22+
low[u] = min(low[u], low[v]);
23+
if (p == 0 and children > 1) {
24+
articulation_pts.insert(u);
25+
}
26+
if (p != 0 and low[v] >= discover[u]) {
27+
articulation_pts.insert(u);
28+
}
29+
}
30+
else if (v != p) {
31+
low[u] = min(low[u], discover[v]);
32+
}
33+
}
34+
}
35+
36+
void dfs(ll n) {
37+
ll i;
38+
for (i = 1; i <= n; i++) {
39+
if (visited[i] == false) {
40+
explore(i);
41+
}
42+
}
43+
}
44+
45+
int main() {
46+
ll n, m, i, u, v;
47+
cin >> n >> m;
48+
adj.resize(n+1);
49+
discover.resize(n+1);
50+
low.resize(n+1);
51+
visited.assign(n+1,false);
52+
53+
for (i = 0; i < m; i++) {
54+
cin >> u >> v;
55+
adj[u].push_back(v);
56+
adj[v].push_back(u);
57+
}
58+
dfs(n);
59+
for (ll pt : articulation_pts) {
60+
cout << pt << endl;
61+
}
62+
return 0;
63+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /