Submission #114577

# Submission time Handle Problem Language Result Execution time Memory
114577 2019-06-01T20:40:52 Z tincamatei Mergers (JOI19_mergers) C++14
100 / 100
1620 ms 85228 KB
#include <bits/stdc++.h>

using namespace std;

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

vector<int> graph[1+MAX_N];
int color[1+MAX_N], fr[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]);
   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14080 KB Output is correct
2 Correct 13 ms 14080 KB Output is correct
3 Correct 13 ms 14124 KB Output is correct
4 Correct 13 ms 14052 KB Output is correct
5 Correct 13 ms 14080 KB Output is correct
6 Correct 14 ms 14080 KB Output is correct
7 Correct 16 ms 14080 KB Output is correct
8 Correct 13 ms 14080 KB Output is correct
9 Correct 13 ms 14080 KB Output is correct
10 Correct 13 ms 14080 KB Output is correct
11 Correct 16 ms 14080 KB Output is correct
12 Correct 13 ms 14080 KB Output is correct
13 Correct 13 ms 14080 KB Output is correct
14 Correct 13 ms 14052 KB Output is correct
15 Correct 14 ms 14080 KB Output is correct
16 Correct 13 ms 14080 KB Output is correct
17 Correct 12 ms 14052 KB Output is correct
18 Correct 12 ms 14080 KB Output is correct
19 Correct 12 ms 14080 KB Output is correct
20 Correct 14 ms 14080 KB Output is correct
21 Correct 14 ms 14080 KB Output is correct
22 Correct 13 ms 14080 KB Output is correct
23 Correct 13 ms 14080 KB Output is correct
24 Correct 13 ms 14080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14080 KB Output is correct
2 Correct 13 ms 14080 KB Output is correct
3 Correct 13 ms 14124 KB Output is correct
4 Correct 13 ms 14052 KB Output is correct
5 Correct 13 ms 14080 KB Output is correct
6 Correct 14 ms 14080 KB Output is correct
7 Correct 16 ms 14080 KB Output is correct
8 Correct 13 ms 14080 KB Output is correct
9 Correct 13 ms 14080 KB Output is correct
10 Correct 13 ms 14080 KB Output is correct
11 Correct 16 ms 14080 KB Output is correct
12 Correct 13 ms 14080 KB Output is correct
13 Correct 13 ms 14080 KB Output is correct
14 Correct 13 ms 14052 KB Output is correct
15 Correct 14 ms 14080 KB Output is correct
16 Correct 13 ms 14080 KB Output is correct
17 Correct 12 ms 14052 KB Output is correct
18 Correct 12 ms 14080 KB Output is correct
19 Correct 12 ms 14080 KB Output is correct
20 Correct 14 ms 14080 KB Output is correct
21 Correct 14 ms 14080 KB Output is correct
22 Correct 13 ms 14080 KB Output is correct
23 Correct 13 ms 14080 KB Output is correct
24 Correct 13 ms 14080 KB Output is correct
25 Correct 13 ms 14080 KB Output is correct
26 Correct 16 ms 14324 KB Output is correct
27 Correct 15 ms 14336 KB Output is correct
28 Correct 16 ms 14336 KB Output is correct
29 Correct 15 ms 14208 KB Output is correct
30 Correct 15 ms 14208 KB Output is correct
31 Correct 13 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 14 ms 14208 KB Output is correct
35 Correct 15 ms 14208 KB Output is correct
36 Correct 15 ms 14252 KB Output is correct
37 Correct 16 ms 14208 KB Output is correct
38 Correct 14 ms 14076 KB Output is correct
39 Correct 15 ms 14208 KB Output is correct
40 Correct 14 ms 14208 KB Output is correct
41 Correct 15 ms 14208 KB Output is correct
42 Correct 15 ms 14208 KB Output is correct
43 Correct 15 ms 14464 KB Output is correct
44 Correct 13 ms 14080 KB Output is correct
45 Correct 14 ms 14336 KB Output is correct
46 Correct 15 ms 14208 KB Output is correct
47 Correct 13 ms 14080 KB Output is correct
48 Correct 15 ms 14208 KB Output is correct
49 Correct 15 ms 14208 KB Output is correct
50 Correct 15 ms 14336 KB Output is correct
51 Correct 16 ms 14208 KB Output is correct
52 Correct 14 ms 14208 KB Output is correct
53 Correct 15 ms 14208 KB Output is correct
54 Correct 15 ms 14208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14080 KB Output is correct
2 Correct 13 ms 14080 KB Output is correct
3 Correct 13 ms 14124 KB Output is correct
4 Correct 13 ms 14052 KB Output is correct
5 Correct 13 ms 14080 KB Output is correct
6 Correct 14 ms 14080 KB Output is correct
7 Correct 16 ms 14080 KB Output is correct
8 Correct 13 ms 14080 KB Output is correct
9 Correct 13 ms 14080 KB Output is correct
10 Correct 13 ms 14080 KB Output is correct
11 Correct 16 ms 14080 KB Output is correct
12 Correct 13 ms 14080 KB Output is correct
13 Correct 13 ms 14080 KB Output is correct
14 Correct 13 ms 14052 KB Output is correct
15 Correct 14 ms 14080 KB Output is correct
16 Correct 13 ms 14080 KB Output is correct
17 Correct 12 ms 14052 KB Output is correct
18 Correct 12 ms 14080 KB Output is correct
19 Correct 12 ms 14080 KB Output is correct
20 Correct 14 ms 14080 KB Output is correct
21 Correct 14 ms 14080 KB Output is correct
22 Correct 13 ms 14080 KB Output is correct
23 Correct 13 ms 14080 KB Output is correct
24 Correct 13 ms 14080 KB Output is correct
25 Correct 12 ms 14080 KB Output is correct
26 Correct 81 ms 19184 KB Output is correct
27 Correct 235 ms 18736 KB Output is correct
28 Correct 15 ms 14336 KB Output is correct
29 Correct 15 ms 14080 KB Output is correct
30 Correct 14 ms 14052 KB Output is correct
31 Correct 152 ms 18936 KB Output is correct
32 Correct 15 ms 14208 KB Output is correct
33 Correct 180 ms 28756 KB Output is correct
34 Correct 163 ms 18680 KB Output is correct
35 Correct 15 ms 14208 KB Output is correct
36 Correct 186 ms 19960 KB Output is correct
37 Correct 18 ms 14208 KB Output is correct
38 Correct 15 ms 14208 KB Output is correct
39 Correct 75 ms 18936 KB Output is correct
40 Correct 41 ms 14588 KB Output is correct
41 Correct 141 ms 18776 KB Output is correct
42 Correct 164 ms 22072 KB Output is correct
43 Correct 13 ms 14208 KB Output is correct
44 Correct 176 ms 29304 KB Output is correct
45 Correct 135 ms 23928 KB Output is correct
46 Correct 14 ms 14208 KB Output is correct
47 Correct 21 ms 14208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 19200 KB Output is correct
2 Correct 84 ms 19668 KB Output is correct
3 Correct 15 ms 14208 KB Output is correct
4 Correct 15 ms 14208 KB Output is correct
5 Correct 13 ms 14080 KB Output is correct
6 Correct 13 ms 14080 KB Output is correct
7 Correct 14 ms 14208 KB Output is correct
8 Correct 141 ms 19528 KB Output is correct
9 Correct 15 ms 14208 KB Output is correct
10 Correct 129 ms 19224 KB Output is correct
11 Correct 13 ms 14080 KB Output is correct
12 Correct 182 ms 19192 KB Output is correct
13 Correct 164 ms 19444 KB Output is correct
14 Correct 144 ms 19704 KB Output is correct
15 Correct 68 ms 18968 KB Output is correct
16 Correct 15 ms 14208 KB Output is correct
17 Correct 13 ms 14080 KB Output is correct
18 Correct 76 ms 19440 KB Output is correct
19 Correct 141 ms 25084 KB Output is correct
20 Correct 15 ms 14252 KB Output is correct
21 Correct 12 ms 14080 KB Output is correct
22 Correct 77 ms 19412 KB Output is correct
23 Correct 14 ms 14208 KB Output is correct
24 Correct 144 ms 19192 KB Output is correct
25 Correct 129 ms 23288 KB Output is correct
26 Correct 15 ms 14208 KB Output is correct
27 Correct 19 ms 14336 KB Output is correct
28 Correct 15 ms 14248 KB Output is correct
29 Correct 14 ms 14208 KB Output is correct
30 Correct 15 ms 14208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14080 KB Output is correct
2 Correct 13 ms 14080 KB Output is correct
3 Correct 13 ms 14124 KB Output is correct
4 Correct 13 ms 14052 KB Output is correct
5 Correct 13 ms 14080 KB Output is correct
6 Correct 14 ms 14080 KB Output is correct
7 Correct 16 ms 14080 KB Output is correct
8 Correct 13 ms 14080 KB Output is correct
9 Correct 13 ms 14080 KB Output is correct
10 Correct 13 ms 14080 KB Output is correct
11 Correct 16 ms 14080 KB Output is correct
12 Correct 13 ms 14080 KB Output is correct
13 Correct 13 ms 14080 KB Output is correct
14 Correct 13 ms 14052 KB Output is correct
15 Correct 14 ms 14080 KB Output is correct
16 Correct 13 ms 14080 KB Output is correct
17 Correct 12 ms 14052 KB Output is correct
18 Correct 12 ms 14080 KB Output is correct
19 Correct 12 ms 14080 KB Output is correct
20 Correct 14 ms 14080 KB Output is correct
21 Correct 14 ms 14080 KB Output is correct
22 Correct 13 ms 14080 KB Output is correct
23 Correct 13 ms 14080 KB Output is correct
24 Correct 13 ms 14080 KB Output is correct
25 Correct 13 ms 14080 KB Output is correct
26 Correct 16 ms 14324 KB Output is correct
27 Correct 15 ms 14336 KB Output is correct
28 Correct 16 ms 14336 KB Output is correct
29 Correct 15 ms 14208 KB Output is correct
30 Correct 15 ms 14208 KB Output is correct
31 Correct 13 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 14 ms 14208 KB Output is correct
35 Correct 15 ms 14208 KB Output is correct
36 Correct 15 ms 14252 KB Output is correct
37 Correct 16 ms 14208 KB Output is correct
38 Correct 14 ms 14076 KB Output is correct
39 Correct 15 ms 14208 KB Output is correct
40 Correct 14 ms 14208 KB Output is correct
41 Correct 15 ms 14208 KB Output is correct
42 Correct 15 ms 14208 KB Output is correct
43 Correct 15 ms 14464 KB Output is correct
44 Correct 13 ms 14080 KB Output is correct
45 Correct 14 ms 14336 KB Output is correct
46 Correct 15 ms 14208 KB Output is correct
47 Correct 13 ms 14080 KB Output is correct
48 Correct 15 ms 14208 KB Output is correct
49 Correct 15 ms 14208 KB Output is correct
50 Correct 15 ms 14336 KB Output is correct
51 Correct 16 ms 14208 KB Output is correct
52 Correct 14 ms 14208 KB Output is correct
53 Correct 15 ms 14208 KB Output is correct
54 Correct 15 ms 14208 KB Output is correct
55 Correct 12 ms 14080 KB Output is correct
56 Correct 81 ms 19184 KB Output is correct
57 Correct 235 ms 18736 KB Output is correct
58 Correct 15 ms 14336 KB Output is correct
59 Correct 15 ms 14080 KB Output is correct
60 Correct 14 ms 14052 KB Output is correct
61 Correct 152 ms 18936 KB Output is correct
62 Correct 15 ms 14208 KB Output is correct
63 Correct 180 ms 28756 KB Output is correct
64 Correct 163 ms 18680 KB Output is correct
65 Correct 15 ms 14208 KB Output is correct
66 Correct 186 ms 19960 KB Output is correct
67 Correct 18 ms 14208 KB Output is correct
68 Correct 15 ms 14208 KB Output is correct
69 Correct 75 ms 18936 KB Output is correct
70 Correct 41 ms 14588 KB Output is correct
71 Correct 141 ms 18776 KB Output is correct
72 Correct 164 ms 22072 KB Output is correct
73 Correct 13 ms 14208 KB Output is correct
74 Correct 176 ms 29304 KB Output is correct
75 Correct 135 ms 23928 KB Output is correct
76 Correct 14 ms 14208 KB Output is correct
77 Correct 21 ms 14208 KB Output is correct
78 Correct 76 ms 19200 KB Output is correct
79 Correct 84 ms 19668 KB Output is correct
80 Correct 15 ms 14208 KB Output is correct
81 Correct 15 ms 14208 KB Output is correct
82 Correct 13 ms 14080 KB Output is correct
83 Correct 13 ms 14080 KB Output is correct
84 Correct 14 ms 14208 KB Output is correct
85 Correct 141 ms 19528 KB Output is correct
86 Correct 15 ms 14208 KB Output is correct
87 Correct 129 ms 19224 KB Output is correct
88 Correct 13 ms 14080 KB Output is correct
89 Correct 182 ms 19192 KB Output is correct
90 Correct 164 ms 19444 KB Output is correct
91 Correct 144 ms 19704 KB Output is correct
92 Correct 68 ms 18968 KB Output is correct
93 Correct 15 ms 14208 KB Output is correct
94 Correct 13 ms 14080 KB Output is correct
95 Correct 76 ms 19440 KB Output is correct
96 Correct 141 ms 25084 KB Output is correct
97 Correct 15 ms 14252 KB Output is correct
98 Correct 12 ms 14080 KB Output is correct
99 Correct 77 ms 19412 KB Output is correct
100 Correct 14 ms 14208 KB Output is correct
101 Correct 144 ms 19192 KB Output is correct
102 Correct 129 ms 23288 KB Output is correct
103 Correct 15 ms 14208 KB Output is correct
104 Correct 19 ms 14336 KB Output is correct
105 Correct 15 ms 14248 KB Output is correct
106 Correct 14 ms 14208 KB Output is correct
107 Correct 15 ms 14208 KB Output is correct
108 Correct 591 ms 39888 KB Output is correct
109 Correct 1182 ms 69392 KB Output is correct
110 Correct 1127 ms 85228 KB Output is correct
111 Correct 1464 ms 83272 KB Output is correct
112 Correct 1289 ms 69560 KB Output is correct
113 Correct 719 ms 50156 KB Output is correct
114 Correct 1162 ms 44160 KB Output is correct
115 Correct 1069 ms 44112 KB Output is correct
116 Correct 1154 ms 48840 KB Output is correct
117 Correct 1461 ms 50984 KB Output is correct
118 Correct 1080 ms 46312 KB Output is correct
119 Correct 1372 ms 50956 KB Output is correct
120 Correct 1277 ms 70508 KB Output is correct
121 Correct 1284 ms 50872 KB Output is correct
122 Correct 1219 ms 50072 KB Output is correct
123 Correct 616 ms 51152 KB Output is correct
124 Correct 1620 ms 50232 KB Output is correct