Skip to content

Commit dc340fc

Browse files
authored
Create Tarjan's algorithm for finding articulation points.cpp
1 parent 93e9976 commit dc340fc

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <bits/stdc++.h>
2+
#include <ext/rope>
3+
using namespace std;
4+
using namespace __gnu_cxx;
5+
6+
using ld = long double;
7+
using llint = long long;
8+
using ullint = unsigned long long;
9+
using pii = pair <int,int>;
10+
using pcc = pair <char,char>;
11+
using pss = pair <string,string>;
12+
using vi = vector <int>;
13+
using vb = vector <bool>;
14+
using vii = vi::iterator;
15+
16+
#define INF (1<<30)
17+
#define MOD 1000000007
18+
#define mp make_pair
19+
#define mt make_tuple
20+
#define all(c) c.begin(), c.end()
21+
#define ms(name,val) memset(name, val, sizeof name)
22+
#define np nullptr
23+
24+
25+
int n, m;
26+
vb ap;
27+
vi disc, low;
28+
vector <vi> g;
29+
30+
void DFS(int node, int p)
31+
{
32+
static int time = 0;
33+
disc[node] = low[node] = ++time;
34+
int children = 0;
35+
36+
for (int &x: g[node])
37+
{
38+
if (!disc[x])
39+
{
40+
DFS(x, node);
41+
++children;
42+
low[node] = min(low[node], low[x]);
43+
44+
if (p != -1 && low[x] >= disc[node])
45+
ap[node] = 1;
46+
}
47+
else if (x != p)
48+
low[node] = min(low[node], disc[x]);
49+
}
50+
51+
if (p == -1 && children > 1)
52+
ap[node] = 1;
53+
}
54+
55+
int main()
56+
{
57+
ios_base::sync_with_stdio(0);
58+
//cin.tie(0);
59+
60+
cin >> n >> m;
61+
g.resize(n);
62+
disc.resize(n);
63+
low.resize(n);
64+
ap.resize(n);
65+
66+
while (m)
67+
{
68+
--m;
69+
int a, b;
70+
cin >> a >> b;
71+
g[a].push_back(b);
72+
g[b].push_back(a);
73+
}
74+
75+
for (int t1 = 0; t1 < n; ++t1)
76+
if (!disc[t1]) DFS(t1, -1);
77+
78+
for (int t1 = 0; t1 < n; ++t1)
79+
if (ap[t1]) cout << t1 << ' ';
80+
81+
cout << '\n';
82+
83+
84+
return 0;
85+
}

0 commit comments

Comments
 (0)