이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 5e5 + 5, lg = 22, lng = 26, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
int seg[maxn<<2];
int upd(int i, int k, int x = 1, int lx = 0, int rx = maxn){
if(lx + 1 == rx) return seg[x] = k, 0;
int mid = (lx + rx)>>1;
if(i < mid) upd(i, k, x<<1, lx, mid);
else upd(i, k, x<<1|1, mid, rx);
return seg[x] = min(seg[x<<1], seg[x<<1|1]), 0;
}
int get(int l, int r, int x = 1, int lx = 0, int rx = maxn){
if(l <= lx && r >= rx) return seg[x];
if(l >= rx || r <= lx) return inf;
int mid = (lx + rx)>>1;
return min(get(l, r, x<<1, lx, mid), get(l, r, x<<1|1, mid, rx));
}
int st[maxn], en[maxn], eu[maxn], t, par[maxn][lg], h[maxn], a[maxn], b[maxn], mtch[maxn];
vector<int> adj[maxn], leaf;
void dfs(int r, int p){
if(sz(adj[r]) == 1) leaf.pb(t);
eu[t] = r, st[r] = t++, par[r][0] = p;
rep(i,1,lg) par[r][i] = par[par[r][i-1]][i-1];
for(int c: adj[r]) if(c - p) h[c] = h[r] + 1, dfs(c, r);
en[r] = t;
}
int lca(int u, int v){
if(h[u] > h[v]) swap(u, v);
per(i,lg-1,0) if(h[par[v][i]] >= h[u]) v = par[v][i];
if(u == v) return u;
per(i,lg-1,0) if(par[u][i] - par[v][i]) u = par[u][i], v = par[v][i];
return par[u][0];
}
void add(int u, int v){
mtch[u] = v, mtch[v] = u;
int l = lca(u, v);
upd(st[u], h[l]), upd(st[v], h[l]);
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
fill(seg, seg+(maxn<<2), inf);
fill(mtch, mtch + maxn, -1);
int n; cin >> n;
rep(i,1,n){
int u, v; cin >> u >> v; u--, v--;
adj[u].pb(v);
adj[v].pb(u);
}
int root;
rep(i,0,n) if(sz(adj[i]) > 1) root = i;
dfs(root, root);
// er(root+1);
// for(int c: leaf) er(eu[c]+1);
rep(i,0,n) if(i - root){
a[i] = leaf.back();
// er(a[i], st[i], en[i]);
if(st[i] < a[i] && en[i] > a[i]) a[i] = leaf[0];
b[i] = *lower_bound(all(leaf), st[i]), a[i] = eu[a[i]], b[i] = eu[b[i]];
// er(i+1, a[i]+1, b[i]+1);
}
rep(i,0,sz(leaf)>>1) add(eu[leaf[i<<1]], eu[leaf[i<<1|1]]);
if(sz(leaf)&1) add(eu[leaf.back()], root);
rep(i,0,n) if(i - root){
if(get(st[i], en[i]) < h[i]) continue;
int x = mtch[a[i]], y = mtch[b[i]];
add(a[i], b[i]), add(x, y);
}
cout << ((sz(leaf)+1)>>1) << '\n';
vector<bool> is(n);
rep(i,0,n){
if(mtch[i]+1 && !is[i]){
cout << i + 1 << ' ' << mtch[i] + 1 << '\n';
is[mtch[i]] = true;
}
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int main()':
net.cpp:89:17: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
89 | rep(i,0,n) if(i - root){
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |