#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define eb emplace_back
#define em emplace
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const int MAX = 101010;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
struct UFT {
vector <int> p;
UFT(int n) {
p.resize(n+1);
for(int i = 1; i <= n; i++) p[i] = i;
}
int find(int x) {
return p[x] == x ? p[x] : p[x] = find(p[x]);
}
void uni(int x, int y) {
x = find(x), y = find(y);
p[max(x, y)] = min(x, y);
}
};
vector <int> g[MAX];
int low[MAX], ord[MAX], cnt = 0;
void dfs(int x, int pa) {
ord[x] = low[x] = ++cnt;
bool was = false;
for(auto i : g[x]) {
if(i == pa && !was) {
was = true;
continue;
}
if(ord[i] == 0) {
dfs(i, x);
low[x] = min(low[x], low[i]);
}
else if(ord[i] < ord[x]) {
low[x] = min(low[x], ord[i]);
}
}
for(auto i : g[x]) {
if(i == pa) continue;
if(low[i] > ord[x]) cout << x << ' ' << i << '\n';
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n, m;
cin >> n >> m;
UFT t1(n), t2(n);
for(int i = 1; i <= m; i++) {
int u, v; cin >> u >> v;
if(t1.find(u) != t1.find(v)) {
t1.uni(u, v);
g[u].eb(v), g[v].eb(u);
}
else if(t2.find(u) != t2.find(v)) {
t2.uni(u, v);
g[u].eb(v), g[v].eb(u);
}
}
for(int i = 1; i <= n; i++) {
if(ord[i] == 0) dfs(i, 0);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3192 KB |
Output is correct |
2 |
Correct |
10 ms |
3152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
3224 KB |
Output is correct |
2 |
Correct |
128 ms |
2936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
217 ms |
3876 KB |
Output is correct |
2 |
Correct |
249 ms |
3292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
352 ms |
5624 KB |
Output is correct |
2 |
Correct |
319 ms |
5280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
524 ms |
11576 KB |
Output is correct |
2 |
Correct |
459 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
826 ms |
12680 KB |
Output is correct |
2 |
Correct |
744 ms |
9628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1014 ms |
15048 KB |
Output is correct |
2 |
Correct |
955 ms |
9336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1257 ms |
14968 KB |
Output is correct |
2 |
Correct |
1226 ms |
9336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1515 ms |
14640 KB |
Output is correct |
2 |
Correct |
1442 ms |
11360 KB |
Output is correct |