#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 ddd[MAXK+10][MAXK+10];
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=0; i<K; i++) for(int j=0; j<K; j++) ddd[i][j]=dist(P[i], P[j]);
for(int i=1; i<(1<<K); i++)
{
vector<int> V, VV;
for(int j=0; j<K; j++)
{
if(i&(1<<j)) V.push_back(P[j]), VV.push_back(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=ddd[VV[p]][VV[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
pastiri.cpp: In function 'int main()':
pastiri.cpp:104:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int p=0; p<V.size(); p++)
| ~^~~~~~~~~
pastiri.cpp:106:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(int q=p+1; q<V.size(); q++)
| ~^~~~~~~~~
pastiri.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
71 | scanf("%d%d", &N, &K);
| ~~~~~^~~~~~~~~~~~~~~~
pastiri.cpp:75:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
75 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
pastiri.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
81 | scanf("%d", &P[i]);
| ~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
339 ms |
119916 KB |
Output is correct |
2 |
Runtime error |
534 ms |
242504 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
137 ms |
13292 KB |
Output is correct |
2 |
Correct |
48 ms |
13164 KB |
Output is correct |
3 |
Correct |
743 ms |
88868 KB |
Output is correct |
4 |
Correct |
777 ms |
91528 KB |
Output is correct |
5 |
Correct |
902 ms |
88940 KB |
Output is correct |
6 |
Correct |
901 ms |
98412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
12396 KB |
Integer 987654321 violates the range [1, 2000] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
782 ms |
180580 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |