#include <iostream>
#include <fstream>
#include <bitset>
#include <cassert>
#include <vector>
#define rep(i, n) for (int i=0; i<(n); i++)
#define pb push_back
using namespace std;
struct UnionFind {
int U[100000];
UnionFind(int N) {
rep(i, N) U[i] = i;
}
int find(int x) {
if (U[x] == x) return x;
return U[x] = find(U[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return;
U[x] = y;
}
bool same(int x, int y) {
return find(x) == find(y);
}
};
int N, M;
vector<int> G[100000];
int R[100000];
int U[17][100000];
void dfs(int x, int p, int r) {
R[x] = r;
U[0][x] = p;
for (int t : G[x]) {
if (t == p) continue;
dfs(t, x, r+1);
}
}
int T[100000];
int lca(int x, int y) {
if (R[x] < R[y]) swap(x, y);
for (int b=16; b>=0; b--) if ((R[x]-R[y])>>b) x = U[b][x];
if (x == y) return x;
for (int b=16; b>=0; b--) if (U[b][x] != U[b][y]) x = U[b][x], y = U[b][y];
return U[0][x];
}
bool used[100000];
void dfs2(int x, int p) {
used[x] = true;
for (int t : G[x]) {
if (t == p) continue;
dfs2(t, x);
T[x] += T[t];
}
if (p != -1 && T[x] == 0) cout << x+1 << " " << p+1 << "\n";
}
signed main() {
ifstream cin("/dev/stdin");
cin >> N >> M;
auto uf = new UnionFind(N);
auto backward = new bitset<6000000>();
rep(i, M) {
int u, v;
cin >> u >> v;
u--, v--;
if (uf->same(u, v)) {
backward->set(i);
}
else {
uf->unite(u, v);
G[u].pb(v);
G[v].pb(u);
}
}
delete uf;
rep(i, N) R[i] = -1;
rep(i, N) if (R[i] == -1) dfs(i, -1, 0);
rep(k, 16) rep(i, N) {
if (U[k][i] == -1) U[k+1][i] = -1;
U[k+1][i] = U[k][U[k][i]];
}
cin.seekg(0);
int nn, nm;
cin >> nn >> nm;
assert(N == nn && M == nm);
rep(i, M) {
int u, v;
u = 0, v = 0;
cin >> u >> v;
u--, v--;
if (backward->test(i)) {
if (R[u] > R[v]) swap(u, v);
T[u]++;
T[v]++;
T[lca(u, v)]-=2;
}
}
delete backward;
rep(i, N) if (!used[i]) dfs2(i, -1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3552 KB |
Output is correct |
2 |
Correct |
4 ms |
3456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
4020 KB |
Output is correct |
2 |
Correct |
10 ms |
3948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
274 ms |
3816 KB |
Output is correct |
2 |
Correct |
251 ms |
3816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
523 ms |
4536 KB |
Output is correct |
2 |
Correct |
610 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
901 ms |
6044 KB |
Output is correct |
2 |
Correct |
762 ms |
6660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1372 ms |
10956 KB |
Output is correct |
2 |
Correct |
1204 ms |
10856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2356 ms |
12088 KB |
Output is correct |
2 |
Correct |
2050 ms |
12012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3481 ms |
13904 KB |
Output is correct |
2 |
Correct |
3008 ms |
14148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4186 ms |
13924 KB |
Output is correct |
2 |
Correct |
3831 ms |
14036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4967 ms |
13360 KB |
Output is correct |
2 |
Correct |
4267 ms |
14184 KB |
Output is correct |