답안 #106109

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
106109 2019-04-16T14:54:17 Z mzhao Mergers (JOI19_mergers) C++11
0 / 100
27 ms 23996 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#define D(x...) printf(x)
#else
#define D(x...)
#endif

#define MN 500100

int N, K, S[MN];
vector<int> adj[MN];

int tot[MN], cnt[MN], ans, num[MN];
bool bad[MN], sub[MN];

void dfs(int n, int p) {
	int cur[8];
	for (int i = 1; i <= K; i++) cur[i] = cnt[i];
	cnt[S[n]]++;
	for (int i : adj[n]) if (i != p) {
		dfs(i, n);
		sub[n] |= sub[i] || bad[i];
	}
	for (int i = 1; i <= K; i++) cur[i] = cnt[i]-cur[i];
	if (n == 1) return;
	bool f = 1;
	for (int i = 1; i <= K; i++) if (cur[i] && cur[i] != tot[i]) {
		f = 0;
		break;
	}
	bad[n] = f;
}

void dfs2(int n, int p, bool b) {
	b |= bad[n];
	if (!sub[n]) {
		ans += b;
		num[n] = 1;
		return;
	}
	for (int i : adj[n]) if (i != p) dfs2(i, n, b);
}

bool extra;

void dfs3(int n, int p) {
	for (int i : adj[n]) if (i != p) {
		dfs3(i, n);
		num[n] += num[i];
	}
	if (num[n] == ans && bad[n]) extra = 1;
}

int main() {
	scanf("%d%d", &N, &K);
	assert(N <= 100);
	for (int i = 1, A, B; i < N; i++) {
		scanf("%d%d", &A, &B);
		adj[A].push_back(B);
		adj[B].push_back(A);
	}
	for (int i = 1; i <= N; i++) {
		scanf("%d", &S[i]);
		tot[S[i]]++;
	}
	dfs(1, 0);
	for (int i = 1; i <= N; i++) D("%d", bad[i]);
	D("\n");
	dfs2(1, 0, 0);
	printf("%d\n", (ans+1)/2);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:57:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &K);
  ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &A, &B);
   ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &S[i]);
   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12160 KB Output is correct
2 Correct 14 ms 12160 KB Output is correct
3 Incorrect 12 ms 12032 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12160 KB Output is correct
2 Correct 14 ms 12160 KB Output is correct
3 Incorrect 12 ms 12032 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12160 KB Output is correct
2 Correct 14 ms 12160 KB Output is correct
3 Incorrect 12 ms 12032 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 27 ms 23996 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12160 KB Output is correct
2 Correct 14 ms 12160 KB Output is correct
3 Incorrect 12 ms 12032 KB Output isn't correct
4 Halted 0 ms 0 KB -