답안 #412056

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
412056 2021-05-26T12:49:14 Z 송준혁(#7508) Pastiri (COI20_pastiri) C++17
0 / 100
719 ms 36164 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define MOD 1000000007
#define INF (1ll<<62)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

int N, K;
int A[505050], D[505050];
bool chk[505050], vis[505050];
queue<int> Q;
vector<int> adj[505050], ans;

void del(int u){
	vis[u] = true;
	for (int v : adj[u]) if (A[v] == A[u]-1) del(v);
}

int main(){
	scanf("%d %d", &N, &K);
	if (N==1){
		puts("1\n1");
		return 0;
	}
	for (int i=1; i<N; i++){
		int u, v;
		scanf("%d %d", &u, &v);
		adj[u].pb(v), adj[v].pb(u);
		D[u]++, D[v]++;
	}
	memset(A, -1, sizeof A);
	for (int i=1; i<=K; i++){
		int x;
		scanf("%d", &x);
		chk[x] = true, Q.push(x), A[x] = 0;
	}
	while (Q.size()){
		int u = Q.front(); Q.pop();
		for (int v : adj[u]){
			if (A[v]>=0) continue;
			A[v] = A[u]+1;
			Q.push(v);
		}
	}
	for (int i=1; i<=N; i++) if (D[i] == 1) Q.push(i);
	while (Q.size()){
		int u = Q.front(); Q.pop();
		for (int v : adj[u]){
			D[v]--;
			if (D[v] == 1) Q.push(v);
		}
		if (vis[u] || !chk[u]) continue;
		bool tf = true;
		while (tf){
			tf = false;
			for (int v : adj[u]){
				if (!vis[v] && A[v] == A[u]+1){
					u = v, tf = true;
					break;
				}
			}
		}
		ans.pb(u);
		del(u);
	}
	printf("%d\n", ans.size());
	for (int u : ans) printf("%d ", u);
	printf("\n");
	return 0;
}

Compilation message

pastiri.cpp: In function 'int main()':
pastiri.cpp:70:11: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wformat=]
   70 |  printf("%d\n", ans.size());
      |          ~^     ~~~~~~~~~~
      |           |             |
      |           int           std::vector<int>::size_type {aka long unsigned int}
      |          %ld
pastiri.cpp:24:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |  scanf("%d %d", &N, &K);
      |  ~~~~~^~~~~~~~~~~~~~~~~
pastiri.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
pastiri.cpp:38:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |   scanf("%d", &x);
      |   ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 214 ms 36164 KB Output is correct
2 Incorrect 244 ms 32708 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 14348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 14188 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 719 ms 33800 KB Output isn't correct
2 Halted 0 ms 0 KB -