#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz size
const int mxN = 1e5;
int n,m;
vector<int> adj[mxN];
int p[2*mxN];
void dfs(int c, int depth, int par){
p[c] = depth;
p[c+n] = p[c];
int op = par;
for (int i : adj[c]){
if (i == par){par = -1; continue;}
if (p[i] != -1){ p[c+n] = min(p[c+n],p[i]); continue;}
dfs(i,depth+1,c);
p[c+n] = min(p[c+n], p[i+n]);
}
if (p[c+n] >= depth && op != -1) cout << c+1 << " " << op+1 << '\n';
}
int gpar(const int &c){
if (p[c] == c) return c;
p[c] = gpar(p[c]);
return p[c];
}
bool ss(const int &a, const int &b){
return gpar(a) == gpar(b);
}
void un(const int &a, const int &b){
p[gpar(a)] = gpar(b);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < 2* n; i++) p[i] = i;
int a,b;
for (int i = 0; i < m; i++){
cin >> a >> b;
a -= 1;
b -= 1;
if (!ss(a,b)) un(a,b);
else if (!ss(a+n,b+n)) un(a+n,b+n);
else continue;
adj[a].pb(b);
adj[b].pb(a);
}
for (int i = 0; i < n; i++) p[i] = -1;
for (int i = 0; i < n; i++) if (p[i] == -1) dfs(i,0,-1);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3164 KB |
Output is correct |
2 |
Correct |
4 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
2984 KB |
Output is correct |
2 |
Correct |
56 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
3664 KB |
Output is correct |
2 |
Correct |
115 ms |
3240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
5456 KB |
Output is correct |
2 |
Correct |
140 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
10648 KB |
Output is correct |
2 |
Correct |
229 ms |
6736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
395 ms |
12116 KB |
Output is correct |
2 |
Correct |
366 ms |
8672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
497 ms |
14224 KB |
Output is correct |
2 |
Correct |
458 ms |
8356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
603 ms |
14148 KB |
Output is correct |
2 |
Correct |
643 ms |
8436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
736 ms |
13648 KB |
Output is correct |
2 |
Correct |
675 ms |
10324 KB |
Output is correct |