답안 #114576

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
114576 2019-06-01T20:39:32 Z tincamatei Mergers (JOI19_mergers) C++14
70 / 100
349 ms 82648 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 500000;
const int MAX_K = MAX_N;

vector<int> graph[MAX_N];
int color[1+MAX_N], fr[1+MAX_K], frsub[1+MAX_K], subarb[1+MAX_K];
int criticalsubarb[1+MAX_N];
int nrleaves, criticalEdges;
bool intree[1+MAX_N];

void dfs(int nod, int papa = 0) {
	subarb[nod] = 1;
	for(auto it: graph[nod])
		if(it != papa) {
			dfs(it, nod);
			subarb[nod] += subarb[it];
		}
}

struct CriticalEdgeChecker {
	vector<int> nodehistory;
	int freq[1+MAX_K], fullsides, K;

	CriticalEdgeChecker(int _K):K(_K) {
		fullsides = K;
		for(int i = 0; i <= MAX_K; ++i)
			freq[i] = 0;
	}

	void insert(int nod) {
		if(freq[color[nod]] == 0)
			fullsides--;
		freq[color[nod]]++;
		if(freq[color[nod]] == fr[color[nod]])
			++fullsides;
		nodehistory.push_back(nod);
	}
	
	void erase(int nod) {
		if(freq[color[nod]] == fr[color[nod]])
			--fullsides;
		freq[color[nod]]--;
		if(freq[color[nod]] == 0)
			++fullsides;
	}

	void clear() {
		for(auto it: nodehistory)
			erase(it);
		nodehistory.clear();
	}

	bool isCritical() {
		return fullsides == K;
	}
} *criticalEdge;

void pourDfs(int nod, int papa = 0) {
	criticalEdge->insert(nod);
	for(auto it: graph[nod])
		if(it != papa)
			pourDfs(it, nod);
}

void heavyDfs(int nod, int papa = 0) {
	int heavySon = -1;
	for(auto it: graph[nod])
		if(it != papa && (heavySon == -1 || (heavySon != -1 && subarb[it] > subarb[heavySon])))
			heavySon = it;

	if(heavySon != -1) {
		for(auto it: graph[nod])
			if(it != papa && it != heavySon) {
				criticalEdge->clear();
				heavyDfs(it, nod);
				if(criticalEdge->isCritical()) {
					criticalsubarb[nod]++;
					criticalEdges++;
					intree[it] = true;
				}
			}

		criticalEdge->clear();
		heavyDfs(heavySon, nod);
		if(criticalEdge->isCritical()) {
			criticalsubarb[nod]++;
			criticalEdges++;
			intree[heavySon] = true;
		}
		
		for(auto it: graph[nod])
			if(it != papa && it != heavySon)
				pourDfs(it, nod);
	}
	criticalEdge->insert(nod);
}

int buildTree(int nod, int papa = 0) {
	int degree = (papa != 0); // degree from above
	for(auto it: graph[nod])
		if(it != papa)
			degree += buildTree(it, nod);

	if(intree[nod]) {
		if(degree == 1)
			++nrleaves;
		return 1;
	} else
		return degree - (papa != 0);
}

int main() {
	int N, K;
	scanf("%d%d", &N, &K);
	criticalEdge = new CriticalEdgeChecker(K);

	for(int i = 0; i < N - 1; ++i) {
		int a, b;
		scanf("%d%d", &a, &b);
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	for(int i = 1; i <= N; ++i) {
		scanf("%d", &color[i]);
		fr[color[i]]++;
	}

	dfs(1);
	heavyDfs(1);
	
	intree[1] = true;
	buildTree(1);

	printf("%d", (nrleaves + 1) / 2);

	return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:117: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:122: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:128:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &color[i]);
   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 14080 KB Output is correct
2 Correct 15 ms 14080 KB Output is correct
3 Correct 13 ms 14080 KB Output is correct
4 Correct 14 ms 14080 KB Output is correct
5 Correct 13 ms 14080 KB Output is correct
6 Correct 13 ms 14080 KB Output is correct
7 Correct 13 ms 14080 KB Output is correct
8 Correct 14 ms 14080 KB Output is correct
9 Correct 14 ms 14080 KB Output is correct
10 Correct 14 ms 14080 KB Output is correct
11 Correct 15 ms 14208 KB Output is correct
12 Correct 14 ms 14036 KB Output is correct
13 Correct 14 ms 14208 KB Output is correct
14 Correct 15 ms 14080 KB Output is correct
15 Correct 13 ms 14080 KB Output is correct
16 Correct 14 ms 14084 KB Output is correct
17 Correct 13 ms 14208 KB Output is correct
18 Correct 13 ms 14080 KB Output is correct
19 Correct 13 ms 14080 KB Output is correct
20 Correct 16 ms 14208 KB Output is correct
21 Correct 14 ms 14052 KB Output is correct
22 Correct 14 ms 14080 KB Output is correct
23 Correct 13 ms 14084 KB Output is correct
24 Correct 13 ms 14080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 14080 KB Output is correct
2 Correct 15 ms 14080 KB Output is correct
3 Correct 13 ms 14080 KB Output is correct
4 Correct 14 ms 14080 KB Output is correct
5 Correct 13 ms 14080 KB Output is correct
6 Correct 13 ms 14080 KB Output is correct
7 Correct 13 ms 14080 KB Output is correct
8 Correct 14 ms 14080 KB Output is correct
9 Correct 14 ms 14080 KB Output is correct
10 Correct 14 ms 14080 KB Output is correct
11 Correct 15 ms 14208 KB Output is correct
12 Correct 14 ms 14036 KB Output is correct
13 Correct 14 ms 14208 KB Output is correct
14 Correct 15 ms 14080 KB Output is correct
15 Correct 13 ms 14080 KB Output is correct
16 Correct 14 ms 14084 KB Output is correct
17 Correct 13 ms 14208 KB Output is correct
18 Correct 13 ms 14080 KB Output is correct
19 Correct 13 ms 14080 KB Output is correct
20 Correct 16 ms 14208 KB Output is correct
21 Correct 14 ms 14052 KB Output is correct
22 Correct 14 ms 14080 KB Output is correct
23 Correct 13 ms 14084 KB Output is correct
24 Correct 13 ms 14080 KB Output is correct
25 Correct 12 ms 14080 KB Output is correct
26 Correct 15 ms 14324 KB Output is correct
27 Correct 16 ms 14336 KB Output is correct
28 Correct 17 ms 14464 KB Output is correct
29 Correct 15 ms 14336 KB Output is correct
30 Correct 15 ms 14336 KB Output is correct
31 Correct 14 ms 14080 KB Output is correct
32 Correct 15 ms 14464 KB Output is correct
33 Correct 13 ms 14080 KB Output is correct
34 Correct 15 ms 14208 KB Output is correct
35 Correct 15 ms 14336 KB Output is correct
36 Correct 14 ms 14208 KB Output is correct
37 Correct 15 ms 14336 KB Output is correct
38 Correct 14 ms 14080 KB Output is correct
39 Correct 15 ms 14336 KB Output is correct
40 Correct 15 ms 14208 KB Output is correct
41 Correct 15 ms 14336 KB Output is correct
42 Correct 15 ms 14208 KB Output is correct
43 Correct 15 ms 14592 KB Output is correct
44 Correct 14 ms 14064 KB Output is correct
45 Correct 16 ms 14336 KB Output is correct
46 Correct 16 ms 14336 KB Output is correct
47 Correct 13 ms 14080 KB Output is correct
48 Correct 16 ms 14336 KB Output is correct
49 Correct 15 ms 14336 KB Output is correct
50 Correct 16 ms 14336 KB Output is correct
51 Correct 15 ms 14328 KB Output is correct
52 Correct 16 ms 14336 KB Output is correct
53 Correct 14 ms 14208 KB Output is correct
54 Correct 15 ms 14208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 14080 KB Output is correct
2 Correct 15 ms 14080 KB Output is correct
3 Correct 13 ms 14080 KB Output is correct
4 Correct 14 ms 14080 KB Output is correct
5 Correct 13 ms 14080 KB Output is correct
6 Correct 13 ms 14080 KB Output is correct
7 Correct 13 ms 14080 KB Output is correct
8 Correct 14 ms 14080 KB Output is correct
9 Correct 14 ms 14080 KB Output is correct
10 Correct 14 ms 14080 KB Output is correct
11 Correct 15 ms 14208 KB Output is correct
12 Correct 14 ms 14036 KB Output is correct
13 Correct 14 ms 14208 KB Output is correct
14 Correct 15 ms 14080 KB Output is correct
15 Correct 13 ms 14080 KB Output is correct
16 Correct 14 ms 14084 KB Output is correct
17 Correct 13 ms 14208 KB Output is correct
18 Correct 13 ms 14080 KB Output is correct
19 Correct 13 ms 14080 KB Output is correct
20 Correct 16 ms 14208 KB Output is correct
21 Correct 14 ms 14052 KB Output is correct
22 Correct 14 ms 14080 KB Output is correct
23 Correct 13 ms 14084 KB Output is correct
24 Correct 13 ms 14080 KB Output is correct
25 Correct 14 ms 14080 KB Output is correct
26 Correct 75 ms 20556 KB Output is correct
27 Correct 115 ms 20216 KB Output is correct
28 Correct 16 ms 14336 KB Output is correct
29 Correct 80 ms 14072 KB Output is correct
30 Correct 13 ms 14080 KB Output is correct
31 Correct 119 ms 20344 KB Output is correct
32 Correct 14 ms 14208 KB Output is correct
33 Correct 146 ms 30328 KB Output is correct
34 Correct 136 ms 20240 KB Output is correct
35 Correct 15 ms 14208 KB Output is correct
36 Correct 147 ms 21444 KB Output is correct
37 Correct 15 ms 14464 KB Output is correct
38 Correct 15 ms 14336 KB Output is correct
39 Correct 73 ms 20464 KB Output is correct
40 Correct 15 ms 14592 KB Output is correct
41 Correct 172 ms 20216 KB Output is correct
42 Correct 183 ms 23516 KB Output is correct
43 Correct 14 ms 14080 KB Output is correct
44 Correct 187 ms 30604 KB Output is correct
45 Correct 139 ms 25436 KB Output is correct
46 Correct 14 ms 14336 KB Output is correct
47 Correct 16 ms 14208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 19184 KB Output is correct
2 Correct 89 ms 19576 KB Output is correct
3 Correct 15 ms 14208 KB Output is correct
4 Correct 15 ms 14336 KB Output is correct
5 Correct 13 ms 14080 KB Output is correct
6 Correct 13 ms 14080 KB Output is correct
7 Correct 15 ms 14180 KB Output is correct
8 Correct 151 ms 19552 KB Output is correct
9 Correct 15 ms 14208 KB Output is correct
10 Correct 154 ms 19100 KB Output is correct
11 Correct 13 ms 14080 KB Output is correct
12 Correct 172 ms 20728 KB Output is correct
13 Correct 163 ms 21224 KB Output is correct
14 Correct 148 ms 21372 KB Output is correct
15 Correct 70 ms 20340 KB Output is correct
16 Correct 14 ms 14336 KB Output is correct
17 Correct 13 ms 14080 KB Output is correct
18 Correct 90 ms 21232 KB Output is correct
19 Correct 199 ms 26968 KB Output is correct
20 Correct 16 ms 14336 KB Output is correct
21 Correct 14 ms 14080 KB Output is correct
22 Correct 88 ms 21104 KB Output is correct
23 Correct 16 ms 14208 KB Output is correct
24 Correct 173 ms 20816 KB Output is correct
25 Correct 141 ms 24984 KB Output is correct
26 Correct 15 ms 14336 KB Output is correct
27 Correct 16 ms 14336 KB Output is correct
28 Correct 16 ms 14208 KB Output is correct
29 Correct 16 ms 14336 KB Output is correct
30 Correct 16 ms 14336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 14080 KB Output is correct
2 Correct 15 ms 14080 KB Output is correct
3 Correct 13 ms 14080 KB Output is correct
4 Correct 14 ms 14080 KB Output is correct
5 Correct 13 ms 14080 KB Output is correct
6 Correct 13 ms 14080 KB Output is correct
7 Correct 13 ms 14080 KB Output is correct
8 Correct 14 ms 14080 KB Output is correct
9 Correct 14 ms 14080 KB Output is correct
10 Correct 14 ms 14080 KB Output is correct
11 Correct 15 ms 14208 KB Output is correct
12 Correct 14 ms 14036 KB Output is correct
13 Correct 14 ms 14208 KB Output is correct
14 Correct 15 ms 14080 KB Output is correct
15 Correct 13 ms 14080 KB Output is correct
16 Correct 14 ms 14084 KB Output is correct
17 Correct 13 ms 14208 KB Output is correct
18 Correct 13 ms 14080 KB Output is correct
19 Correct 13 ms 14080 KB Output is correct
20 Correct 16 ms 14208 KB Output is correct
21 Correct 14 ms 14052 KB Output is correct
22 Correct 14 ms 14080 KB Output is correct
23 Correct 13 ms 14084 KB Output is correct
24 Correct 13 ms 14080 KB Output is correct
25 Correct 12 ms 14080 KB Output is correct
26 Correct 15 ms 14324 KB Output is correct
27 Correct 16 ms 14336 KB Output is correct
28 Correct 17 ms 14464 KB Output is correct
29 Correct 15 ms 14336 KB Output is correct
30 Correct 15 ms 14336 KB Output is correct
31 Correct 14 ms 14080 KB Output is correct
32 Correct 15 ms 14464 KB Output is correct
33 Correct 13 ms 14080 KB Output is correct
34 Correct 15 ms 14208 KB Output is correct
35 Correct 15 ms 14336 KB Output is correct
36 Correct 14 ms 14208 KB Output is correct
37 Correct 15 ms 14336 KB Output is correct
38 Correct 14 ms 14080 KB Output is correct
39 Correct 15 ms 14336 KB Output is correct
40 Correct 15 ms 14208 KB Output is correct
41 Correct 15 ms 14336 KB Output is correct
42 Correct 15 ms 14208 KB Output is correct
43 Correct 15 ms 14592 KB Output is correct
44 Correct 14 ms 14064 KB Output is correct
45 Correct 16 ms 14336 KB Output is correct
46 Correct 16 ms 14336 KB Output is correct
47 Correct 13 ms 14080 KB Output is correct
48 Correct 16 ms 14336 KB Output is correct
49 Correct 15 ms 14336 KB Output is correct
50 Correct 16 ms 14336 KB Output is correct
51 Correct 15 ms 14328 KB Output is correct
52 Correct 16 ms 14336 KB Output is correct
53 Correct 14 ms 14208 KB Output is correct
54 Correct 15 ms 14208 KB Output is correct
55 Correct 14 ms 14080 KB Output is correct
56 Correct 75 ms 20556 KB Output is correct
57 Correct 115 ms 20216 KB Output is correct
58 Correct 16 ms 14336 KB Output is correct
59 Correct 80 ms 14072 KB Output is correct
60 Correct 13 ms 14080 KB Output is correct
61 Correct 119 ms 20344 KB Output is correct
62 Correct 14 ms 14208 KB Output is correct
63 Correct 146 ms 30328 KB Output is correct
64 Correct 136 ms 20240 KB Output is correct
65 Correct 15 ms 14208 KB Output is correct
66 Correct 147 ms 21444 KB Output is correct
67 Correct 15 ms 14464 KB Output is correct
68 Correct 15 ms 14336 KB Output is correct
69 Correct 73 ms 20464 KB Output is correct
70 Correct 15 ms 14592 KB Output is correct
71 Correct 172 ms 20216 KB Output is correct
72 Correct 183 ms 23516 KB Output is correct
73 Correct 14 ms 14080 KB Output is correct
74 Correct 187 ms 30604 KB Output is correct
75 Correct 139 ms 25436 KB Output is correct
76 Correct 14 ms 14336 KB Output is correct
77 Correct 16 ms 14208 KB Output is correct
78 Correct 75 ms 19184 KB Output is correct
79 Correct 89 ms 19576 KB Output is correct
80 Correct 15 ms 14208 KB Output is correct
81 Correct 15 ms 14336 KB Output is correct
82 Correct 13 ms 14080 KB Output is correct
83 Correct 13 ms 14080 KB Output is correct
84 Correct 15 ms 14180 KB Output is correct
85 Correct 151 ms 19552 KB Output is correct
86 Correct 15 ms 14208 KB Output is correct
87 Correct 154 ms 19100 KB Output is correct
88 Correct 13 ms 14080 KB Output is correct
89 Correct 172 ms 20728 KB Output is correct
90 Correct 163 ms 21224 KB Output is correct
91 Correct 148 ms 21372 KB Output is correct
92 Correct 70 ms 20340 KB Output is correct
93 Correct 14 ms 14336 KB Output is correct
94 Correct 13 ms 14080 KB Output is correct
95 Correct 90 ms 21232 KB Output is correct
96 Correct 199 ms 26968 KB Output is correct
97 Correct 16 ms 14336 KB Output is correct
98 Correct 14 ms 14080 KB Output is correct
99 Correct 88 ms 21104 KB Output is correct
100 Correct 16 ms 14208 KB Output is correct
101 Correct 173 ms 20816 KB Output is correct
102 Correct 141 ms 24984 KB Output is correct
103 Correct 15 ms 14336 KB Output is correct
104 Correct 16 ms 14336 KB Output is correct
105 Correct 16 ms 14208 KB Output is correct
106 Correct 16 ms 14336 KB Output is correct
107 Correct 16 ms 14336 KB Output is correct
108 Runtime error 349 ms 82648 KB Execution killed with signal 11 (could be triggered by violating memory limits)
109 Halted 0 ms 0 KB -