# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1089397 | hihihah | Pastiri (COI20_pastiri) | C++17 | 225 ms | 34644 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define all(x) x.begin(), x.end();
using namespace std;
const int N = 1e9 + 7;
int n, k;
vector<int> ke[500009];
int c[500009];
int dd[500009];
int vis[500009];
int trc[500009];
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;
ke[u].push_back(v);
ke[v].push_back(u);
}
for(int i = 1; i <= k; i ++)
cin >> c[i];
if(k == 1){
cout << 1;
return 0;
}
queue<int> q;
for(int i = 1; i <= k; i ++){
q.push(c[i]);
vis[c[i]] = 1;
dd[c[i]] = 1;
trc[c[i]] = c[i];
}
ll res = 0;
while(q.size()){
int u = q.front();
q.pop();
for(int i : ke[u])
if(i != trc[u]){
if(!dd[i]){
vis[i] = vis[u];
dd[i] = dd[u] + 1;
q.push(i);
}
else if(dd[i] == dd[u] + 1)
vis[i] += vis[u];
}
}
vector<int> sav;
for(int i = 1; i <= n; i ++)
{
if(ke[i].size() == 1 && vis[i] == 1) continue;
int mx = 0;
for(int j : ke[i])
mx = max(mx, dd[j]);
if(dd[i] >= mx) sav.push_back(i);
}
cout << sav.size() << '\n';
for(int i : sav) cout << i << " ";
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |