#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 5;
const int INF = 1e9;
int n, k, tim;
int H[MAXN], d[MAXN], a[MAXN];
int up[20][MAXN], cnt[MAXN];
bool Vis[MAXN];
vector <int> Ans, V;
vector <int> NAdj[MAXN];
vector <int> Adj[MAXN];
void DFS(int node, int p = 1)
{
up[0][node] = p;
for(int i = 1; i <= 19; i++)
{
up[i][node] = up[i - 1][up[i - 1][node]];
}
tim++;
for(auto x : Adj[node])
{
if(x == p)
{
continue;
}
H[x] = H[node] + 1;
DFS(x, node);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
Adj[u].push_back(v);
Adj[v].push_back(u);
}
DFS(1);
for(int i = 1; i <= n; i++)
{
d[i] = -1;
}
queue <int> BFS;
for(int i = 1; i <= k; i++)
{
cin >> a[i];
d[a[i]] = 0;
BFS.push(a[i]);
V.push_back(a[i]);
}
while(BFS.empty() == false)
{
int node = BFS.front();
BFS.pop();
for(auto x : Adj[node])
{
if(d[x] == -1)
{
d[x] = d[node] + 1;
BFS.push(x);
}
if(d[x] == d[node] + 1)
{
NAdj[x].push_back(node);
}
}
}
sort(V.begin(), V.end(), [](int x, int y)
{
return H[x] > H[y];
});
for(auto x : V)
{
int cur = x;
for(int i = 19; i >= 0; i--)
{
int nx = up[i][cur];
if(H[x] - H[nx] <= d[nx])
{
cur = nx;
}
}
if(Vis[cur])
{
continue;
}
Ans.push_back(cur);
queue <int> BFS;
Vis[cur] = true;
BFS.push(cur);
while(BFS.empty() == false)
{
int node = BFS.front();
BFS.pop();
for(auto y : NAdj[node])
{
if(!Vis[y])
{
Vis[y] = true;
BFS.push(y);
}
}
}
}
cout << Ans.size() << '\n';
for(auto x : Ans)
{
cout << x << ' ';
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
136132 KB |
Output is correct |
2 |
Incorrect |
428 ms |
136584 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
24780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
24196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1031 ms |
106076 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |