#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <array>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <numeric>
#include <cassert>
#include <iomanip>
using namespace std;
#define pi pair<int, int>
#define f first
#define s second
vector <vector <int> > edges (1e5 + 5);
vector <bool> visited (1e5 + 5, false);
vector <int> tin (1e5 + 5, -1), low (1e5 + 5, -1);
int timer = 0;
void dfs(int v, int prev){
visited[v] = true;
tin[v] = low[v] = timer++;
bool ok = false;
for(int u : edges[v]){
if(u == prev && !ok){
ok = true;
continue;
}
if(visited[u]){
low[v] = min(low[v], tin[u]);
}
else{
dfs(u, v);
low[v] = min(low[v], low[u]);
}
}
if(prev != -1 && tin[v] == low[v]){
cout << prev << " " << v << endl;
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n, m;
cin >> n >> m;
vector <int> links1 (n + 1), links2 (n + 1), sz1 (n + 1, 1), sz2 (n + 1, 1);
iota(links1.begin(), links1.end(), 0);
iota(links2.begin(), links2.end(), 0);
while(m--){
int a, b, a1, b1, a2, b2;
cin >> a >> b;
a1 = a2 = a;
b1 = b2 = b;
while(a1 != links1[a1]){
a1 = links1[a1];
}
while(b1 != links1[b1]){
b1 = links1[b1];
}
while(a2 != links2[a2]){
a2 = links2[a2];
}
while(b2 != links2[b2]){
b2 = links2[b2];
}
if(a1 != b1){
edges[a].push_back(b);
edges[b].push_back(a);
if(sz1[a1] < sz1[b1]){
swap(a1, b1);
}
links1[b1] = a1;
sz1[a1] += sz1[b1];
}
else if(a2 != b2){
edges[a].push_back(b);
edges[b].push_back(a);
if(sz2[a2] < sz2[b2]){
swap(a2, b2);
}
links2[b2] = a2;
sz2[a2] += sz2[b2];
}
}
for(int i = 1; i <= n; i++){
if(!visited[i]){
dfs(i, -1);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3456 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4008 KB |
Output is correct |
2 |
Correct |
7 ms |
3748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
8248 KB |
Output is correct |
2 |
Correct |
109 ms |
8096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
209 ms |
8856 KB |
Output is correct |
2 |
Correct |
264 ms |
8452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
307 ms |
9956 KB |
Output is correct |
2 |
Correct |
275 ms |
9696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
406 ms |
10956 KB |
Output is correct |
2 |
Correct |
382 ms |
9112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
661 ms |
13708 KB |
Output is correct |
2 |
Correct |
615 ms |
11312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
926 ms |
16868 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1165 ms |
18232 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1453 ms |
17792 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |