Submission #711483

# Submission time Handle Problem Language Result Execution time Memory
711483 2023-03-17T05:19:06 Z Foxyy Mergers (JOI19_mergers) C++17
100 / 100
959 ms 196524 KB
#include <bits/stdc++.h>
using namespace std;

#define Foxyy cin.tie(0); cout.sync_with_stdio(0);
#define ll long long

namespace HLD {
	int n;
	vector<vector<int>>& adj = *(new vector<vector<int>>());
	vector<int> chainRootOfNode{};
	vector<int> heavyChild{};
	vector<int> parent{};
	vector<int> height{};
	vector<int> depth{};
	
	void scanHeavy(int u, int p) {
		if (p != -1) {
			depth[u] = depth[p] + 1;
		}
		parent[u] = p;
		int currentHeavyChild = -1;
		for (int v : adj[u]) if (v != p) {
			scanHeavy(v, u);
			if (currentHeavyChild == -1 or height[v] + 1 > height[currentHeavyChild]) {
				currentHeavyChild = v;
			}
		}
		heavyChild[u] = currentHeavyChild;
		height[u] = height[heavyChild[u]] + 1;
	}
	
	void buildChains(int u, int root) {
		chainRootOfNode[u] = root;
		for (int v : adj[u]) if (v != parent[u]) {
			if (v == heavyChild[u]) {
				buildChains(v, root);
			} else {
				buildChains(v, v);
			}
		}
	}

	void initialize(vector<vector<int>>& _adj) {
		n = (int)_adj.size();
		adj = _adj;
		chainRootOfNode.resize(n, -1);
		parent.resize(n);
		heavyChild.resize(n, -1);
		height.resize(n);
		depth.resize(n);
		
		chainRootOfNode[0] = parent[0] = 0;
		depth[0] = 0;
		
		scanHeavy(0, 0);
		buildChains(0, 0);
	}
	
	int getLCAOf(int a, int b) {
		while (chainRootOfNode[a] != chainRootOfNode[b]) {
			if (depth[chainRootOfNode[a]] < depth[chainRootOfNode[b]]) {
				swap(a, b);
			}
			a = parent[chainRootOfNode[a]];
		}
		if (depth[a] > depth[b]) {
			return b;
		} else {
			return a;
		}
	}
	
	int getChainRootOfNode(int u) {
		return chainRootOfNode[u];
	}
}; // namespace HLD

struct Solver {
	int n;
	int k;
	vector<vector<int>>& adj;
	vector<int>& s;
	
	vector<int> f;
	
	int get(int x) {
		return x == f[x] ? x : f[x] = get(f[x]);
	}
	
	void solve() {
		HLD::initialize(adj);
//		cerr << "init\n";
		vector<vector<int>> stateCities(k);
		for (int i = 0; i < n; i++) {
			stateCities[s[i]].push_back(i);
		}
		
		f.resize(n);
		iota(f.begin(), f.end(), 0);
		
		for (int i = 0; i < k; i++) if (not stateCities[i].empty()) {
			int u = stateCities[i][0];
			for (int v : stateCities[i]) {
//				cerr << u << ' ' << v << '\n';
				u = get(u), v = get(v);
				int lca = get(HLD::getLCAOf(u, v));
//				cerr << i << ' ' << u << ' ' << v << ' ' << lca << '\n';
				while (u != lca) {
//					cerr << u << '\n';
					f[u] = lca;
					u = get(HLD::parent[u]);
				}
				while (v != lca) {
					f[v] = lca;
//					cerr << v << '\n';
					v = get(HLD::parent[v]);
				}
			}
		}
		
		vector<set<int>> s(n);
		for (int i = 0; i < n; i++) {
			for (int j : adj[i]) if (get(i) != get(j)) {
				s[get(i)].insert(get(j));
			}
		}
		
		int cnt = 0;
		for (int i = 0; i < n; i++) {
			if (s[i].size() == 1u) {
				cnt++;
			}
		} 	
		
//		cerr << cnt << '\n';
		
		cout << (cnt+1)/2 << '\n';
	}
};

signed main() {
	Foxyy
	
	int T = 1;
//	cin >> T;
	while (T--) {
		int n, k;
		cin >> n >> k;
		vector<vector<int>> adj(n);
		vector<int> s(n);
		for (int i = 0; i < n-1; i++) {
			int a, b;
			cin >> a >> b;
			a--, b--;
			adj[a].push_back(b);
			adj[b].push_back(a);
		}
		for (int i = 0; i < n; i++) {
			cin >> s[i];
			s[i]--;
		}
		
		Solver solver{n, k, adj, s};
		solver.solve();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 316 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 316 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 4 ms 1100 KB Output is correct
27 Correct 2 ms 904 KB Output is correct
28 Correct 3 ms 1364 KB Output is correct
29 Correct 3 ms 1364 KB Output is correct
30 Correct 2 ms 852 KB Output is correct
31 Correct 1 ms 320 KB Output is correct
32 Correct 3 ms 1260 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 2 ms 852 KB Output is correct
35 Correct 3 ms 1356 KB Output is correct
36 Correct 3 ms 852 KB Output is correct
37 Correct 3 ms 980 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 3 ms 980 KB Output is correct
40 Correct 2 ms 852 KB Output is correct
41 Correct 2 ms 840 KB Output is correct
42 Correct 2 ms 980 KB Output is correct
43 Correct 2 ms 1108 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 3 ms 1108 KB Output is correct
46 Correct 2 ms 980 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 2 ms 980 KB Output is correct
49 Correct 3 ms 1364 KB Output is correct
50 Correct 3 ms 1364 KB Output is correct
51 Correct 2 ms 980 KB Output is correct
52 Correct 2 ms 968 KB Output is correct
53 Correct 2 ms 980 KB Output is correct
54 Correct 2 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 316 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 60 ms 21500 KB Output is correct
27 Correct 81 ms 20804 KB Output is correct
28 Correct 2 ms 840 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 324 KB Output is correct
31 Correct 80 ms 20756 KB Output is correct
32 Correct 3 ms 852 KB Output is correct
33 Correct 112 ms 27832 KB Output is correct
34 Correct 84 ms 20736 KB Output is correct
35 Correct 2 ms 852 KB Output is correct
36 Correct 78 ms 21584 KB Output is correct
37 Correct 2 ms 848 KB Output is correct
38 Correct 2 ms 852 KB Output is correct
39 Correct 70 ms 21524 KB Output is correct
40 Correct 2 ms 1096 KB Output is correct
41 Correct 90 ms 20664 KB Output is correct
42 Correct 121 ms 23016 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 82 ms 28040 KB Output is correct
45 Correct 85 ms 24524 KB Output is correct
46 Correct 2 ms 980 KB Output is correct
47 Correct 3 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 20044 KB Output is correct
2 Correct 152 ms 36124 KB Output is correct
3 Correct 4 ms 1132 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 109 ms 24260 KB Output is correct
9 Correct 2 ms 864 KB Output is correct
10 Correct 74 ms 20996 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 105 ms 21000 KB Output is correct
13 Correct 104 ms 25040 KB Output is correct
14 Correct 101 ms 35412 KB Output is correct
15 Correct 73 ms 21432 KB Output is correct
16 Correct 2 ms 980 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 113 ms 33824 KB Output is correct
19 Correct 113 ms 39348 KB Output is correct
20 Correct 2 ms 980 KB Output is correct
21 Correct 1 ms 316 KB Output is correct
22 Correct 81 ms 25592 KB Output is correct
23 Correct 2 ms 980 KB Output is correct
24 Correct 113 ms 22348 KB Output is correct
25 Correct 107 ms 37892 KB Output is correct
26 Correct 3 ms 1364 KB Output is correct
27 Correct 3 ms 1360 KB Output is correct
28 Correct 4 ms 980 KB Output is correct
29 Correct 2 ms 980 KB Output is correct
30 Correct 2 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 316 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 4 ms 1100 KB Output is correct
27 Correct 2 ms 904 KB Output is correct
28 Correct 3 ms 1364 KB Output is correct
29 Correct 3 ms 1364 KB Output is correct
30 Correct 2 ms 852 KB Output is correct
31 Correct 1 ms 320 KB Output is correct
32 Correct 3 ms 1260 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 2 ms 852 KB Output is correct
35 Correct 3 ms 1356 KB Output is correct
36 Correct 3 ms 852 KB Output is correct
37 Correct 3 ms 980 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 3 ms 980 KB Output is correct
40 Correct 2 ms 852 KB Output is correct
41 Correct 2 ms 840 KB Output is correct
42 Correct 2 ms 980 KB Output is correct
43 Correct 2 ms 1108 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 3 ms 1108 KB Output is correct
46 Correct 2 ms 980 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 2 ms 980 KB Output is correct
49 Correct 3 ms 1364 KB Output is correct
50 Correct 3 ms 1364 KB Output is correct
51 Correct 2 ms 980 KB Output is correct
52 Correct 2 ms 968 KB Output is correct
53 Correct 2 ms 980 KB Output is correct
54 Correct 2 ms 852 KB Output is correct
55 Correct 1 ms 320 KB Output is correct
56 Correct 60 ms 21500 KB Output is correct
57 Correct 81 ms 20804 KB Output is correct
58 Correct 2 ms 840 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 324 KB Output is correct
61 Correct 80 ms 20756 KB Output is correct
62 Correct 3 ms 852 KB Output is correct
63 Correct 112 ms 27832 KB Output is correct
64 Correct 84 ms 20736 KB Output is correct
65 Correct 2 ms 852 KB Output is correct
66 Correct 78 ms 21584 KB Output is correct
67 Correct 2 ms 848 KB Output is correct
68 Correct 2 ms 852 KB Output is correct
69 Correct 70 ms 21524 KB Output is correct
70 Correct 2 ms 1096 KB Output is correct
71 Correct 90 ms 20664 KB Output is correct
72 Correct 121 ms 23016 KB Output is correct
73 Correct 1 ms 340 KB Output is correct
74 Correct 82 ms 28040 KB Output is correct
75 Correct 85 ms 24524 KB Output is correct
76 Correct 2 ms 980 KB Output is correct
77 Correct 3 ms 852 KB Output is correct
78 Correct 52 ms 20044 KB Output is correct
79 Correct 152 ms 36124 KB Output is correct
80 Correct 4 ms 1132 KB Output is correct
81 Correct 2 ms 852 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 2 ms 852 KB Output is correct
85 Correct 109 ms 24260 KB Output is correct
86 Correct 2 ms 864 KB Output is correct
87 Correct 74 ms 20996 KB Output is correct
88 Correct 1 ms 340 KB Output is correct
89 Correct 105 ms 21000 KB Output is correct
90 Correct 104 ms 25040 KB Output is correct
91 Correct 101 ms 35412 KB Output is correct
92 Correct 73 ms 21432 KB Output is correct
93 Correct 2 ms 980 KB Output is correct
94 Correct 1 ms 340 KB Output is correct
95 Correct 113 ms 33824 KB Output is correct
96 Correct 113 ms 39348 KB Output is correct
97 Correct 2 ms 980 KB Output is correct
98 Correct 1 ms 316 KB Output is correct
99 Correct 81 ms 25592 KB Output is correct
100 Correct 2 ms 980 KB Output is correct
101 Correct 113 ms 22348 KB Output is correct
102 Correct 107 ms 37892 KB Output is correct
103 Correct 3 ms 1364 KB Output is correct
104 Correct 3 ms 1360 KB Output is correct
105 Correct 4 ms 980 KB Output is correct
106 Correct 2 ms 980 KB Output is correct
107 Correct 2 ms 980 KB Output is correct
108 Correct 660 ms 121404 KB Output is correct
109 Correct 789 ms 120496 KB Output is correct
110 Correct 765 ms 131732 KB Output is correct
111 Correct 911 ms 196524 KB Output is correct
112 Correct 959 ms 168720 KB Output is correct
113 Correct 920 ms 168804 KB Output is correct
114 Correct 643 ms 102764 KB Output is correct
115 Correct 652 ms 102800 KB Output is correct
116 Correct 701 ms 109664 KB Output is correct
117 Correct 849 ms 177264 KB Output is correct
118 Correct 664 ms 102564 KB Output is correct
119 Correct 849 ms 177364 KB Output is correct
120 Correct 863 ms 191064 KB Output is correct
121 Correct 835 ms 177120 KB Output is correct
122 Correct 853 ms 127452 KB Output is correct
123 Correct 904 ms 180668 KB Output is correct
124 Correct 893 ms 157428 KB Output is correct