Submission #1003967

#TimeUsernameProblemLanguageResultExecution timeMemory
1003967vjudge1Pastiri (COI20_pastiri)C++17
8 / 100
1048 ms141300 KiB
#include<bits/stdc++.h> using namespace std; #define N 500010 #define M 50 #define LOG 22 #define INFLL 2000000000000000020 #define pb push_back typedef long long ll; typedef pair<ll,ll> pll; vector<ll>resp; ll vet[N]; ll pai[N][LOG]; vector<ll>grafo[N]; ll n,k; vector<ll>ativos; vector<ll>aux; map<ll,ll>dist; ll lca; ll lvl[N]; void DFS(ll u) { for(auto v : grafo[u]) { if(lvl[v]) continue; pai[v][0]=u; lvl[v]=lvl[u]+1; DFS(v); } return; } void build() { ll i,j; lvl[1]=1; pai[1][0]=1; DFS(1); for(i=1;i<LOG;i++) { for(j=1;j<=n;j++) { pai[j][i]=pai[pai[j][i-1]][i-1]; } } return; } ll LCA(ll u,ll v) { ll i,diff=abs(lvl[u]-lvl[v]); if(lvl[u]<lvl[v]) swap(u,v); for(i=LOG-1;i>=0;i--) { if((diff&(1<<i))) { u=pai[u][i]; } } if(u==v) return u; for(i=LOG-1;i>=0;i--) { if(pai[u][i]!=pai[v][i]) { u=pai[u][i]; v=pai[v][i]; } } return pai[u][0]; } ll DIST(ll u,ll v) { lca=LCA(u,v); return lvl[u]+lvl[v]-2*lvl[lca]; } void test() { ll i,ret,mx=0,who,md,maior=0,d,j; for(i=1;i<=n;i++) { d=INFLL; for(j=0;j<k;j++) { d=min(d,DIST(i,vet[j])); } mx=0; for(auto x : ativos) { ret=DIST(i,x); mx+=(bool)(ret==d); } if(maior<mx) { md=d; who=i; maior=mx; } } d=md; for(auto x : ativos) { if(DIST(who,x)!=d) aux.pb(x); } ativos.clear(); for(auto x : aux) { ativos.pb(x); } aux.clear(); resp.pb(who); return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll u,v,i=0,ans,sz; cin >> n >> k; while(i<n-1) { cin >> u >> v; grafo[u].pb(v); grafo[v].pb(u); i++; } i=0; while(i<k) { cin >> vet[i]; ativos.pb(vet[i]); i++; } if(k<=15 || n<=2000) { build(); while(!ativos.empty()) { test(); } sz=(ll)(resp.size()); cout << sz << endl; for(i=0;i<sz;i++) { cout << resp[i] << ((i==sz-1) ? '\n' : ' '); } return 0; } sort(vet,vet+k); vet[k]=vet[k-1]; for(i=0;i<k;i++) { if((vet[i+1]-vet[i])%2) { resp.pb(vet[i]); }else { resp.pb((vet[i+1]+vet[i])/2); i++; } } sz=(ll)(resp.size()); cout << sz << endl; for(i=0;i<sz;i++) { cout << resp[i] << ((i==sz-1) ? '\n' : ' '); } return 0; }

Compilation message (stderr)

pastiri.cpp: In function 'int main()':
pastiri.cpp:129:16: warning: unused variable 'ans' [-Wunused-variable]
  129 |     ll u,v,i=0,ans,sz;
      |                ^~~
pastiri.cpp: In function 'void test()':
pastiri.cpp:110:9: warning: 'md' may be used uninitialized in this function [-Wmaybe-uninitialized]
  110 |         if(DIST(who,x)!=d)
      |         ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...