Submission #367739

#TimeUsernameProblemLanguageResultExecution timeMemory
367739arnold518Pastiri (COI20_pastiri)C++14
0 / 100
1104 ms119904 KiB
#pragma GCC optimize ("O3") #pragma GCC optimize ("Ofast") #pragma GCC optimize ("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAXN = 5e5; const int MAXK = 15; const int INF = 987654321; int N, K; vector<int> adj[MAXN+10]; int par[MAXN+10][30], dep[MAXN+10]; int P[MAXK+10]; void dfs(int now, int bef, int d) { par[now][0]=bef; dep[now]=d; for(int nxt : adj[now]) { if(nxt==bef) continue; dfs(nxt, now, d+1); } } int lca(int u, int v) { if(dep[u]>dep[v]) swap(u, v); for(int i=20; i>=0; i--) if(dep[par[v][i]]>=dep[u]) v=par[v][i]; if(u==v) return u; for(int i=20; i>=0; i--) if(par[u][i]!=par[v][i]) u=par[u][i], v=par[v][i]; return par[u][0]; } int dist(int u, int v) { int w=lca(u, v); return dep[u]+dep[v]-dep[w]-dep[w]; } int half(int u, int v) { int w=lca(u, v); int d=dep[u]+dep[v]-dep[w]-dep[w]; if(d%2) return -1; d/=2; if(dep[u]-dep[w]>=d) { for(int i=20; i>=0; i--) if(d&(1<<i)) u=par[u][i]; return u; } else { for(int i=20; i>=0; i--) if(d&(1<<i)) v=par[v][i]; return v; } } int chk[(1<<MAXK)+10], dp[(1<<MAXK)+10], memo[(1<<MAXK)+10], val[(1<<MAXK)+10]; int main() { scanf("%d%d", &N, &K); for(int i=1; i<N; i++) { int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } for(int i=0; i<K; i++) { scanf("%d", &P[i]); } dfs(1, 1, 1); for(int i=1; i<=20; i++) for(int j=1; j<=N; j++) par[j][i]=par[par[j][i-1]][i-1]; for(int i=1; i<(1<<K); i++) { vector<int> V; for(int j=0; j<K; j++) { if(i&(1<<j)) V.push_back(P[j]); } if(V.size()==1) { chk[i]=1; val[i]=V[0]; continue; } int u=V[0], v=V[0], dd=0; bool flag=true; for(int p=0; p<V.size(); p++) { for(int q=p+1; q<V.size(); q++) { int d=dist(V[p], V[q]); if(d%2) flag=false; if(d>dd) { u=V[p]; v=V[q]; dd=d; } } } if(!flag) { chk[i]=0; continue; } int t=half(u, v); int d=dist(t, V[0]); chk[i]=1; for(int j=0; j<K; j++) { if(i&(1<<j)) { if(d!=dist(P[j], t)) chk[i]=0; } else { if(d>dist(P[j], t)) chk[i]=0; } } if(chk[i]) val[i]=t; } for(int i=0; i<(1<<K); i++) { if(i==0) continue; dp[i]=INF; for(int j=i; j; j=i&(j-1)) { if(chk[j]) { if(dp[i]>dp[i^j]+1) { dp[i]=dp[i^j]+1; memo[i]=i^j; } } } } int ans=dp[(1<<K)-1]; printf("%d\n", ans); int t=(1<<K)-1; while(t) { int x=(t^memo[t]); printf("%d ", val[x]); t=memo[t]; } printf("\n"); }

Compilation message (stderr)

pastiri.cpp: In function 'int main()':
pastiri.cpp:102:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |   for(int p=0; p<V.size(); p++)
      |                ~^~~~~~~~~
pastiri.cpp:104:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |    for(int q=p+1; q<V.size(); q++)
      |                   ~^~~~~~~~~
pastiri.cpp:70:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   70 |  scanf("%d%d", &N, &K);
      |  ~~~~~^~~~~~~~~~~~~~~~
pastiri.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   74 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
pastiri.cpp:80:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   80 |   scanf("%d", &P[i]);
      |   ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...