답안 #1003805

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1003805 2024-06-20T18:02:10 Z vjudge1 Pastiri (COI20_pastiri) C++17
0 / 100
1000 ms 524288 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
const int maxn = 5e5+10;
const int inf = 1e18+10;

int n, k, down[maxn], dist[maxn];
vector<int> g[maxn];
set<int> st[maxn];
int ans;
vector<int> ansv;

void dfs(int u, int ant) {
	down[u] = -1;
	if(dist[u] == 0) down[u] = 0;
	vector<pair<int,int>> ord;
	for(auto v : g[u]) if(v != ant) {
		dfs(v,u);
		ord.pb(mp(down[v],v));
		for(auto x : st[v]) st[u].insert(x-1);
	}

	sort(all(ord),greater<pair<int,int>>());


	for(auto V : ord) {
		int v = V.sc;
		if(down[v] != -1 && st[u].count(down[v]+1) == 0) {
			if(dist[u] != down[v]+1) {
				// cout << " coloca em " << v << endl;
				ansv.pb(v);
				ans++;
				st[u].insert(dist[v]-1);
				down[v] = -1;
			}
			else {
				down[u] = down[v]+1;
			}
		}
	}
	if(down[u] != -1) {
		if(st[u].count(0)) {
			down[u] = -1;
		}
	}
	// cout << u << " " << down[u] << " " << dist[u] << " " << ans << " " << st[u].count(1) << endl;
}

int32_t main() {
	// #ifndef ONLINE_JUDGE
	// freopen("in.in","r",stdin);
	// freopen("out.out","w",stdout);
	// #endif

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

	queue<int> q;
	for(int i = 1; i <= n; i++) {
		dist[i] = inf;
	}

	for(int i = 0; i < k; i++) {
		int x;
		cin >> x;
		dist[x] = 0;
		q.push(x);
	}
	while(q.size()) {
		int u = q.front();
		q.pop();
		for(auto v : g[u]) {
			if(dist[v] == inf) {
				q.push(v);
				dist[v] = dist[u]+1;
			}
		}
	}

	dfs(1,0);
	if(down[1] != -1) ansv.pb(1);
	cout << ansv.size() << endl;
	for(auto x : ansv) cout << x << " ";
	cout << endl;
	// cout << ans + (down[1] != -1) << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 259 ms 137300 KB Output is correct
2 Runtime error 629 ms 524288 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 35932 KB Output is correct
2 Correct 16 ms 35908 KB Output is correct
3 Correct 434 ms 69356 KB Output is correct
4 Incorrect 431 ms 74000 KB Sheep 99854 not protected
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 35676 KB Sheep 208 not protected
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1082 ms 524288 KB Time limit exceeded
2 Halted 0 ms 0 KB -