forked from mrsac7/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
2177 - Strongly Connected Edges.cpp
61 lines (54 loc) · 1.38 KB
/
2177 - Strongly Connected Edges.cpp
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
/*
Problem Name: Strongly Connected Edges
Problem Link: https://cses.fi/problemset/task/2177
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int mxN = 1e5+5;
vector<pair<int,int>> ans;
int flag = 0, cnt = 0;
vector<int> adj[mxN];
int vis[mxN], low[mxN];
void dfs(int s, int p) {
vis[s] = ++cnt;
low[s] = vis[s];
for (auto i: adj[s]) {
if (i != p) {
if (vis[i]) {
low[s] = min(low[s], vis[i]);
if (vis[i] > vis[s])
ans.push_back({i, s});
}
else {
dfs(i, s);
ans.push_back({s, i});
low[s] = min(low[i], low[s]);
if (low[i] > vis[s])
flag = 1;
}
}
}
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m; cin>>n>>m;
for (int i = 0; i < m; i++) {
int x, y; cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 0);
if (flag == 1) return cout<<"IMPOSSIBLE", 0;
for (int i = 1; i <= n; i++)
if (!vis[i])
return cout<<"IMPOSSIBLE", 0;
for (auto [i, j]: ans)
cout<<i<<' '<<j<<endl;
}