#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 6e5 + 5, LG = 18;
vector<int> g[N];
int dsu[N], lvl[N], pos[N], accm[N], rmq[LG][N * 2], lg2[N * 2];
bitset<M> edgs(0);
int n, m, lptr;
static int get_dsu(int nod) {
return (dsu[nod] ? (dsu[nod] = get_dsu(dsu[nod])) : nod); }
static bool join(int a, int b) {
a = get_dsu(a);
b = get_dsu(b);
if (a == b)
return false;
if (rand() % 2)
swap(a, b);
dsu[a] = b;
return true; }
static void dfs(int u, int pap = 0) {
rmq[0][++lptr] = u;
lvl[u] = lvl[pap] + 1;
pos[u] = lptr;
for (auto v: g[u]) if (v != pap) {
dfs(v, u);
rmq[0][++lptr] = u; } }
static void build_rmq() {
for (int i = 2; i < 2 * N; ++i)
lg2[i] = 1 + lg2[i / 2];
for (int lg = 1; lg < LG; ++lg)
for (int i = 1; i <= 2 * (n - 1); ++i) {
int l = i, r = min(2 * (n - 1), i + (1 << lg - 1));
rmq[lg][i] = min(make_pair(lvl[rmq[lg - 1][l]], rmq[lg - 1][l]), make_pair(lvl[rmq[lg - 1][r]], rmq[lg - 1][r])).second; } }
static int lca(int a, int b) {
int l;
a = pos[a];
b = pos[b];
if (a > b)
swap(a, b);
l = lg2[b - a];
return min(make_pair(lvl[rmq[l][a]], rmq[l][a]), make_pair(lvl[rmq[l][b - (1 << l) + 1]], rmq[l][b - (1 << l) + 1])).second; }
static void answer(int nod, int pap = 0) {
for (auto v: g[nod]) if (v != pap) {
answer(v, nod);
accm[nod]+= accm[v]; }
if (!accm[nod] && pap != 0)
cout << nod << ' ' << pap << '\n'; }
int main() {
#ifdef HOME
freopen("pipes.in", "r", stdin);
freopen("pipes.out", "w", stdout);
#else
srand(time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int a, b, i = 0; i < m; ++i) {
cin >> a >> b;
if (join(a, b)) {
edgs[i] = true;
g[a].push_back(b);
g[b].push_back(a); } }
rewind(stdin);
for (int i = 1; i <= n; ++i)
if (!lvl[i])
dfs(i);
build_rmq();
cin >> n >> m;
for (int a, b, i = 0; i < m; ++i) {
cin >> a >> b;
if (!edgs[i]) {
accm[a]+= 1, accm[b]+= 1;
accm[lca(a, b)]-= 2; } }
for (int i = 1; i <= n; ++i)
if (lvl[i] == 1)
answer(i);
return 0; }
Compilation message
pipes.cpp: In function 'void build_rmq()':
pipes.cpp:39:48: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
int l = i, r = min(2 * (n - 1), i + (1 << lg - 1));
~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3576 KB |
Output is correct |
2 |
Correct |
5 ms |
3580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4600 KB |
Output is correct |
2 |
Correct |
13 ms |
4728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
9776 KB |
Output is correct |
2 |
Correct |
257 ms |
9720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
520 ms |
15224 KB |
Output is correct |
2 |
Runtime error |
526 ms |
16772 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
826 ms |
24332 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1204 ms |
37456 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1820 ms |
31836 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2457 ms |
37928 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3079 ms |
45708 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3727 ms |
58188 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |