답안 #127677

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
127677 2019-07-09T22:34:13 Z wilwxk Mergers (JOI19_mergers) C++14
0 / 100
127 ms 45636 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN=5e5+5;
vector<int> g[MAXN];
map<int, int> mp[MAXN];
int cor[MAXN], contc[MAXN];
int n, x, respf;

int dfs(int cur, int p) {
	mp[cur][cor[cur]]++;
	if(mp[cur][cor[cur]]==contc[cor[cur]]) mp[cur].erase(cor[cur]);
	int cont=0;
	for(auto viz : g[cur]) {
		if(viz==p) continue;
		cont+=dfs(viz, cur);
		if(mp[viz].size()>mp[cur].size()) swap(mp[cur], mp[viz]);
		for(auto mit : mp[viz]) {
			mp[cur][mit.first]+=mit.second;
			if(mp[cur][mit.first]==contc[mit.first]) mp[cur].erase(mit.first);
		}
	}
	if(mp[cur].empty()) {
		// printf("%d >> %d\n", cur, cont);
		if(cur!=1) cont++;
		if(cont<=1) respf++;
		return cont>0;
	}
	return cont;
}

int main() {
	scanf("%d %d", &n, &x);
	for(int i=1; i<n; i++) {
		int a, b; scanf("%d %d", &a, &b);
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for(int i=1; i<=n; i++) {
		scanf("%d", &cor[i]);
		contc[cor[i]]++;
	}
	dfs(1, 1);

	if(n!=1) printf("%d\n", (respf+1)/2);
	else printf("0\n");
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:33:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &x);
  ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:35:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b; scanf("%d %d", &a, &b);
             ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &cor[i]);
   ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 35576 KB Output is correct
2 Correct 34 ms 35704 KB Output is correct
3 Correct 34 ms 35704 KB Output is correct
4 Correct 35 ms 35576 KB Output is correct
5 Incorrect 34 ms 35576 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 35576 KB Output is correct
2 Correct 34 ms 35704 KB Output is correct
3 Correct 34 ms 35704 KB Output is correct
4 Correct 35 ms 35576 KB Output is correct
5 Incorrect 34 ms 35576 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 35576 KB Output is correct
2 Correct 34 ms 35704 KB Output is correct
3 Correct 34 ms 35704 KB Output is correct
4 Correct 35 ms 35576 KB Output is correct
5 Incorrect 34 ms 35576 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 127 ms 45636 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 35576 KB Output is correct
2 Correct 34 ms 35704 KB Output is correct
3 Correct 34 ms 35704 KB Output is correct
4 Correct 35 ms 35576 KB Output is correct
5 Incorrect 34 ms 35576 KB Output isn't correct
6 Halted 0 ms 0 KB -