#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
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)
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
141300 KB |
Output is correct |
2 |
Correct |
82 ms |
34388 KB |
Output is correct |
3 |
Correct |
78 ms |
34388 KB |
Output is correct |
4 |
Correct |
133 ms |
46416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
13148 KB |
Output is correct |
2 |
Correct |
94 ms |
13148 KB |
Output is correct |
3 |
Execution timed out |
1043 ms |
127012 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1048 ms |
12640 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
163 ms |
30544 KB |
Sheep 54 not protected |
2 |
Halted |
0 ms |
0 KB |
- |