-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtarjan.cpp
More file actions
56 lines (48 loc) · 1.04 KB
/
tarjan.cpp
File metadata and controls
56 lines (48 loc) · 1.04 KB
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
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int n,m;
vector<int> adj[N];
int disc[N],low[N];
bool visited[N];
int idx;
set<int> ap;
set<pair<int,int> > b;
void dfs(int u,int p)
{
visited[u] = true;
low[u] = disc[u] = idx++;
int ch = 0;
for(int v : adj[u])
{
if(!visited[v])
{
ch++;
dfs(v,u);
low[u] = min(low[u],low[v]);
if((p!=-1 and low[v]>=disc[u])or(p==-1 and ch>1)) ap.insert(u);
if(low[v]>disc[u]) b.insert({u,v});
}
else if(v!=p)
{
low[u] = min(low[u],disc[v]);
}
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for(int i = 0;i < m;i++)
{
int a,b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(0,-1);
cout << ap.size() << '\n';
for(int x : ap) cout << x << ' ';
cout << '\n' << b.size() << '\n';
for(pair<int,int> x : b) cout << x.first << ' ' << x.second << '\n';
}