#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
struct DSU {
vector <int> par;
int n;
inline void init(int _n) {
n = _n;
par.resize(n + 1);
}
int root(int x) {
if(par[x] == 0)
return x;
return par[x] = root(par[x]);
}
inline void join(int x, int y) {
x = root(x), y = root(y);
if(x != y) {
par[x] = y;
}
}
};
const int MAXN = (int) 1e5;
vector <int> g[MAXN + 1];
stack <int> stk;
inline void addEdge(int x, int y) {
g[x].push_back(y);
g[y].push_back(x);
}
int lvl[MAXN + 1], low[MAXN + 1];
void dfs(int nod, int par) {
stk.push(nod);
lvl[nod] = lvl[par] + 1;
low[nod] = lvl[nod];
int cnt = 0;
for(auto it : g[nod]) {
if(lvl[it] == 0) {
dfs(it, nod);
low[nod] = min(low[nod], low[it]);
if(low[it] == lvl[it]) {
while(stk.top() != it) {
stk.pop();
}
stk.pop();
cout << nod << " " << it << "\n";
}
}
else {
if(it == par) {
if(cnt == 1) {
low[nod] = min(low[nod], lvl[it]);
}
cnt++;
}
else {
low[nod] = min(low[nod], lvl[it]);
}
}
}
}
int main() {
//ifstream cin("B.in");
//ofstream cout("B.out");
int i, n, m;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
DSU dsu1, dsu2;
dsu1.init(n);
dsu2.init(n);
for(i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
if(dsu1.root(x) == dsu1.root(y)) {
if(dsu2.root(x) != dsu2.root(y)) {
dsu2.join(x, y);
addEdge(x, y);
}
}
else {
dsu1.join(x, y);
addEdge(x, y);
}
}
for(i = 1; i <= n; i++) {
if(lvl[i] == 0) {
dfs(i, 0);
}
}
//cin.close();
//cout.close();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
3116 KB |
Output is correct |
2 |
Correct |
8 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
3036 KB |
Output is correct |
2 |
Correct |
97 ms |
2936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
175 ms |
3728 KB |
Output is correct |
2 |
Correct |
235 ms |
3324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
290 ms |
5292 KB |
Output is correct |
2 |
Correct |
246 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
427 ms |
10336 KB |
Output is correct |
2 |
Correct |
378 ms |
7388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
638 ms |
11376 KB |
Output is correct |
2 |
Correct |
633 ms |
8956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
865 ms |
13416 KB |
Output is correct |
2 |
Correct |
844 ms |
9344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1057 ms |
13416 KB |
Output is correct |
2 |
Correct |
990 ms |
9340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1261 ms |
12908 KB |
Output is correct |
2 |
Correct |
1184 ms |
10464 KB |
Output is correct |