#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
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 |
1 |
Correct |
324 ms |
89012 KB |
Output is correct |
2 |
Correct |
350 ms |
89244 KB |
Output is correct |
3 |
Correct |
353 ms |
89164 KB |
Output is correct |
4 |
Correct |
486 ms |
94744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12244 KB |
Output is correct |
2 |
Correct |
9 ms |
12244 KB |
Output is correct |
3 |
Correct |
530 ms |
42652 KB |
Output is correct |
4 |
Correct |
536 ms |
45048 KB |
Output is correct |
5 |
Correct |
585 ms |
42176 KB |
Output is correct |
6 |
Correct |
601 ms |
46380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12116 KB |
Output is correct |
2 |
Correct |
7 ms |
12124 KB |
Output is correct |
3 |
Correct |
8 ms |
12064 KB |
Output is correct |
4 |
Correct |
7 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12160 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
7 ms |
12068 KB |
Output is correct |
8 |
Correct |
6 ms |
12116 KB |
Output is correct |
9 |
Correct |
6 ms |
12116 KB |
Output is correct |
10 |
Correct |
7 ms |
12116 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12116 KB |
Output is correct |
13 |
Correct |
8 ms |
12064 KB |
Output is correct |
14 |
Correct |
7 ms |
12244 KB |
Output is correct |
15 |
Correct |
7 ms |
12116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
555 ms |
43484 KB |
Output is correct |
2 |
Correct |
563 ms |
43144 KB |
Output is correct |
3 |
Correct |
617 ms |
45620 KB |
Output is correct |
4 |
Correct |
601 ms |
42100 KB |
Output is correct |
5 |
Correct |
492 ms |
39912 KB |
Output is correct |
6 |
Correct |
612 ms |
51348 KB |
Output is correct |
7 |
Correct |
601 ms |
49104 KB |
Output is correct |
8 |
Correct |
620 ms |
48752 KB |
Output is correct |
9 |
Correct |
617 ms |
42236 KB |
Output is correct |
10 |
Correct |
596 ms |
42264 KB |
Output is correct |
11 |
Correct |
504 ms |
44076 KB |
Output is correct |
12 |
Correct |
473 ms |
47312 KB |
Output is correct |
13 |
Correct |
500 ms |
48796 KB |
Output is correct |
14 |
Correct |
437 ms |
45900 KB |
Output is correct |
15 |
Correct |
61 ms |
17112 KB |
Output is correct |
16 |
Correct |
521 ms |
40052 KB |
Output is correct |
17 |
Correct |
431 ms |
40112 KB |
Output is correct |
18 |
Correct |
449 ms |
36832 KB |
Output is correct |
19 |
Correct |
616 ms |
49872 KB |
Output is correct |
20 |
Correct |
601 ms |
55644 KB |
Output is correct |
21 |
Correct |
586 ms |
42432 KB |
Output is correct |
22 |
Correct |
586 ms |
43312 KB |
Output is correct |