답안 #759588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
759588 2023-06-16T12:48:07 Z NintsiChkhaidze Pastiri (COI20_pastiri) C++17
0 / 100
121 ms 60080 KB
#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define ll long long
#define pii pair <int,int>
#define left (h<<1),l,((l + r)>>1)
#define right ((h<<1)|1),((l+r)>>1) + 1,r
using namespace std;

const int N = 3e5 + 5,inf = 1e9;

int dis[N],d[N],m[N];
bool fix[N],vis[N];
vector <int> v[N];

void dfs(int x,int par){

	d[x] = m[x] = -inf;
	if (fix[x]) {
		d[x] = 0;
	}
	
	for (int to: v[x]){
		if (to == par) continue;
		dfs(to,x);
		
		if (d[to] + 1 > dis[x]){
			//to-shi uechveli unda dasva cxvari
			d[to] = 0;
			m[to] = dis[to];
		}

		m[x] = max(m[x],m[to] - 1);
		d[x] = max(d[x],d[to] + 1);
	}

	if (m[x] >= d[x]) d[x] = -inf;
}
signed main (){
	ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);

	int n,m;
	cin>>n>>m;

	for (int i = 1; i < n; i++){
		int a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}
	
	for (int i = 1; i <= n; i++) 	
		dis[i] = inf;

	queue <int> q;
	for (int i = 1; i <= m; i++){
		int x;
		cin>>x;
		fix[x] = 1;
		dis[x] = 0;
		q.push(x);
	}

	while (q.size()){
		int x = q.front(); q.pop();
		vis[x] = 1;
		for (int to: v[x]){
			if (!vis[to]){
				vis[to] = 1;
				dis[to] = dis[x] + 1;
				q.push(to);
			}
		}
	}

	dfs(1,1);

	if (d[1] != -inf) d[1] = 0;
	vector <int> ans;
	for (int i = 1; i <= n; i++){
		if (dis[i] != 0 && d[i] == 0){
			ans.pb(i);
		}
	}

	cout<<ans.size()<<"\n";
	for (int x: ans) cout<<x<<" ";
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 121 ms 60080 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7636 KB Output is correct
2 Correct 4 ms 7640 KB Output is correct
3 Runtime error 11 ms 14760 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 7384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 11 ms 14832 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -