Submission #334294

# Submission time Handle Problem Language Result Execution time Memory
334294 2020-12-09T00:50:11 Z limabeans Pastiri (COI20_pastiri) C++17
100 / 100
795 ms 90292 KB
#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;


const int maxn = 1e6 + 5;




int n,k;
vector<int> g[maxn];
vector<int> a;

int par[maxn];
int dep[maxn];

void dfs(int at, int p) {
    for (int to: g[at]) {
	if (to == p) continue;
	par[to]=at;
	dep[to]=1+dep[at];
	dfs(to,at);
    }
}

int rad[maxn];

void gen_rad() {
    for (int i=1; i<=n; i++) {
	rad[i]=-1;
    }
    queue<int> qq;
    for (int x: a) {
	rad[x]=0;
	qq.push(x);
    }
    while (qq.size()) {
	int at = qq.front();
	qq.pop();
	for (int to: g[at]) {
	    if (rad[to]==-1) {
		rad[to]=1+rad[at];
		qq.push(to);
	    }
	}
    }
}


bool done[maxn];

void mark_done(int x) {
    queue<int> qq;
    qq.push(x);
    while (qq.size()) {
	int at = qq.front();
	qq.pop();
	done[at] = true;
	for (int to: g[at]) {
	    if (done[to]) continue;
	    if (rad[to] == rad[at]-1) {
		qq.push(to);
	    }
	}
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

    cin>>n>>k;
    for (int i=0; i<n-1; i++) {
	int u,v; cin>>u>>v;
	g[u].push_back(v);
	g[v].push_back(u);
    }

    a.resize(k);
    for (int i=0; i<k; i++) {
	cin>>a[i];
    }

    dfs(1,0);
    gen_rad();

    sort(a.begin(), a.end(), [&](int x, int y) {
	    return dep[x]<dep[y];
	});
    reverse(a.begin(),a.end());


    vector<int> ans;

    for (int x: a) {
	if (done[x]) continue;
	int at = x;
	while (rad[par[at]] == 1+rad[at]) {
	    at = par[at];
	}
	ans.push_back(at);
	mark_done(at);
    }


    cout<<ans.size()<<"\n";
    sort(ans.begin(),ans.end());
    for (int x: ans) cout<<x<<" ";
    cout<<endl;
    
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 202 ms 83436 KB Output is correct
2 Correct 257 ms 83840 KB Output is correct
3 Correct 249 ms 83820 KB Output is correct
4 Correct 359 ms 90292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 24172 KB Output is correct
2 Correct 18 ms 24172 KB Output is correct
3 Correct 514 ms 52724 KB Output is correct
4 Correct 499 ms 55420 KB Output is correct
5 Correct 632 ms 52588 KB Output is correct
6 Correct 734 ms 55404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 16 ms 23936 KB Output is correct
3 Correct 17 ms 23916 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 17 ms 24044 KB Output is correct
6 Correct 17 ms 24044 KB Output is correct
7 Correct 16 ms 23916 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 21 ms 23916 KB Output is correct
10 Correct 16 ms 23916 KB Output is correct
11 Correct 17 ms 23916 KB Output is correct
12 Correct 17 ms 23916 KB Output is correct
13 Correct 19 ms 23916 KB Output is correct
14 Correct 17 ms 24044 KB Output is correct
15 Correct 17 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 598 ms 53612 KB Output is correct
2 Correct 597 ms 53484 KB Output is correct
3 Correct 703 ms 56708 KB Output is correct
4 Correct 640 ms 52588 KB Output is correct
5 Correct 549 ms 51428 KB Output is correct
6 Correct 664 ms 61036 KB Output is correct
7 Correct 743 ms 59276 KB Output is correct
8 Correct 764 ms 58920 KB Output is correct
9 Correct 795 ms 52588 KB Output is correct
10 Correct 653 ms 52716 KB Output is correct
11 Correct 476 ms 54656 KB Output is correct
12 Correct 437 ms 58588 KB Output is correct
13 Correct 504 ms 60616 KB Output is correct
14 Correct 400 ms 56112 KB Output is correct
15 Correct 80 ms 28908 KB Output is correct
16 Correct 673 ms 50764 KB Output is correct
17 Correct 515 ms 51552 KB Output is correct
18 Correct 592 ms 47484 KB Output is correct
19 Correct 644 ms 58436 KB Output is correct
20 Correct 647 ms 62116 KB Output is correct
21 Correct 577 ms 52756 KB Output is correct
22 Correct 582 ms 53484 KB Output is correct