Submission #399345

#TimeUsernameProblemLanguageResultExecution timeMemory
399345cpp219Network (BOI15_net)C++14
0 / 100
8 ms12016 KiB
#pragma GCC optimization "O2" #pragma GCC optimization "unroll-loop" #pragma GCC target ("avx2") #include<bits/stdc++.h> #define ll int #define ld long double #define fs first #define sc second using namespace std; typedef pair<ll,ll> LL; const ll N = 5e5 + 9; const ll inf = 1e9 + 7; const ll Log2 = 21; vector<ll> g[N]; deque<ll> leaf; vector<LL> ans; ll n,x,y,root,pos[N],d[N],par[N][Log2]; ll now; void DFS(ll u,ll p){ pos[u] = now; now++; for (ll i = 1;i < Log2;i++) if ((1 << u) <= d[u]) par[u][i] = par[par[u][i - 1]][i - 1]; for (auto i : g[u]){ if (i != p){ d[i] = d[u] + 1; par[i][0] = u; DFS(i,u); } } } bool lf(ll x,ll y){ return pos[x] < pos[y]; } ll LCA(ll u,ll v){ if (d[u] > d[v]) swap(u,v); for (ll i = Log2 - 1;i >= 0;i--) if ((1 << i) <= d[v] && d[par[v][i]] >= d[u]) v = par[v][i]; if (u == v) return u; for (ll i = Log2 - 1;i >= 0;i--) if (par[u][i] != par[v][i]) v = par[v][i],u = par[u][i]; return par[v][0]; } ll nearest(ll target){ ll scr = leaf.front(); ll l = 0,h = leaf.size() - 1,m; while(l <= h){ m = (l + h)/2; //cout<<scr<<" "<<leaf[m]<<" "<<LCA(scr,leaf[m])<<"\n"; //exit(0); if (LCA(scr,leaf[m]) == target) h = m - 1; else l = m + 1; } //exit(0); //cout<<leaf[l]<<" "<<LCA(scr,leaf[l]); exit(0); return l; } void out(){ for (auto i : leaf) cout<<i<<" "; exit(0); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define task "tst" if (fopen(task".inp","r")){ freopen(task".inp","r",stdin); //freopen(task".out","w",stdout); } cin>>n; for (ll i = 1;i < n;i++){ cin>>x>>y; g[x].push_back(y); g[y].push_back(x); } for (ll i = 1;i <= n;i++){ if (g[i].size() > 1) root = i; else leaf.push_back(i); } DFS(root,0); sort(leaf.begin(),leaf.end(),lf); while(!leaf.empty()){ ll sz = leaf.size(); if (sz == 1) break; ll lca = LCA(leaf.front(),leaf.back()); ll nxt = nearest(lca); if (nxt <= sz/2){ for (ll i = 0;i < nxt;i++) ans.push_back({leaf[i],leaf[nxt + i]}); for (ll i = 0;i < 2*nxt;i++) leaf.pop_front(); } else{ for (ll i = sz - 1;i >= nxt;i--) ans.push_back({leaf[i],leaf[nxt - sz + i]}); for (ll i = 0;i < 2*(sz - nxt);i++) leaf.pop_back(); } //out(); } if (!leaf.empty()) ans.push_back({leaf.back(),root}); cout<<ans.size()<<"\n"; for (auto i : ans) cout<<i.fs<<" "<<i.sc<<"\n"; }

Compilation message (stderr)

net.cpp:1: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    1 | #pragma GCC optimization "O2"
      | 
net.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    2 | #pragma GCC optimization "unroll-loop"
      | 
net.cpp: In function 'void out()':
net.cpp:61:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   61 |     for (auto i : leaf) cout<<i<<" "; exit(0);
      |     ^~~
net.cpp:61:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   61 |     for (auto i : leaf) cout<<i<<" "; exit(0);
      |                                       ^~~~
net.cpp: In function 'int main()':
net.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   68 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...