#include <iostream>
#include <fstream>
#include <bitset>
#include <cassert>
#include <vector>
#define rep(i, n) for (int i=0; i<(n); i++)
#define INF 1145141919
#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);
}
}
inline 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];
}
int T[100000];
int dfs2(int x, int p) {
for (int t : G[x]) {
if (t == p) continue;
T[x] += dfs2(t, x);
}
if (p != -1 && T[x] == 0) cout << x+1 << " " << p+1 << "\n";
int ret = T[x];
T[x] = INF;
return ret;
}
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;
cin >> u >> v;
u--, v--;
if (backward->test(i)) {
T[u]++;
T[v]++;
T[lca(u, v)]-=2;
}
}
delete backward;
rep(i, N) if (T[i] != INF) dfs2(i, -1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3580 KB |
Output is correct |
2 |
Correct |
5 ms |
3580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
3948 KB |
Output is correct |
2 |
Correct |
10 ms |
3948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
3828 KB |
Output is correct |
2 |
Correct |
251 ms |
3816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
508 ms |
4540 KB |
Output is correct |
2 |
Correct |
600 ms |
4436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
904 ms |
6000 KB |
Output is correct |
2 |
Correct |
764 ms |
6660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1254 ms |
10912 KB |
Output is correct |
2 |
Correct |
1191 ms |
10788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2200 ms |
12016 KB |
Output is correct |
2 |
Correct |
1949 ms |
11964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3088 ms |
13884 KB |
Output is correct |
2 |
Correct |
2799 ms |
14080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3838 ms |
13948 KB |
Output is correct |
2 |
Correct |
3827 ms |
14032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5059 ms |
13576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |