이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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; }
컴파일 시 표준 에러 (stderr) 메시지
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));
~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |