Submission #1003796

# Submission time Handle Problem Language Result Execution time Memory
1003796 2024-06-20T17:56:12 Z vjudge1 Pastiri (COI20_pastiri) C++17
0 / 100
14 ms 35676 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;
}

Compilation message

pastiri.cpp: In function 'int32_t main()':
pastiri.cpp:56:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |  freopen("in.in","r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~
pastiri.cpp:57:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |  freopen("out.out","w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 35672 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 35672 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 35676 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 35672 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -