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 f first
#define s second
#define pii pair<int,int>
using namespace std;
const int N = 5e5 + 5, mod = 1e9 + 7; // !
int n, d[N];
vector<int> V[N], A;
struct node {
int A, B, ans;
} x[N];
void dfs(int u, int p) {
for(int i = 0; i < V[u].size(); i++) {
int v = V[u][i];
if(v == p) continue;
dfs(v, u);
if(x[v].A + 1 > d[u]) {
x[v].ans++; A.push_back(v);
// cout << "++" << v << endl;
x[v].A = -n;
x[v].B = d[v];
}
x[u].A = max(x[u].A, x[v].A + 1);
x[u].B = max(x[u].B, x[v].B - 1);
x[u].ans += x[v].ans;
}
if(x[u].A <= x[u].B) x[u].A = -n - 5;
if(d[u] == 0 && x[u].B < 0) x[u].A = max(x[u].A, 0);
// cout << u <<" " << x[u].A << " " << x[u].B << " " << x[u].ans << endl;
}
main(){
int k;
cin >> n >> k;
for(int i = 2; i <= n; i++) {
int u, v;
cin >> u >> v;
V[u].push_back(v);
V[v].push_back(u);
}
for(int u = 1; u <= n; u++) d[u] = n, x[u].A = -n - 5, x[u].B = -n-5, x[u].ans = 0;
queue<int> q;
while(k--) {
int x;
cin >> x;
d[x] = 0;
q.push(x);
}
while(q.size()) {
int u = q.front();
q.pop();
for(int i = 0; i < V[u].size(); i++) {
if(d[V[u][i]] > d[u] + 1) d[V[u][i]] = d[u] + 1, q.push(V[u][i]);
}
}
dfs(1, 0);
if(x[1].A > -1) ++x[1].ans, A.push_back(1);
cout << A.size() << "\n";
sort(A.begin(), A.end());
for(int i = 0; i < A.size(); i++) cout << A[i] << " "; cout << endl;
}
Compilation message (stderr)
pastiri.cpp: In function 'void dfs(int, int)':
pastiri.cpp:14:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
pastiri.cpp: At global scope:
pastiri.cpp:32:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
32 | main(){
| ^~~~
pastiri.cpp: In function 'int main()':
pastiri.cpp:52:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
pastiri.cpp:60:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int i = 0; i < A.size(); i++) cout << A[i] << " "; cout << endl;
| ~~^~~~~~~~~~
pastiri.cpp:60:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
60 | for(int i = 0; i < A.size(); i++) cout << A[i] << " "; cout << endl;
| ^~~
pastiri.cpp:60:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
60 | for(int i = 0; i < A.size(); i++) cout << A[i] << " "; cout << endl;
| ^~~~
# | 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... |