Submission #120910

# Submission time Handle Problem Language Result Execution time Memory
120910 2019-06-25T17:46:37 Z youngyojun Mergers (JOI19_mergers) C++11
100 / 100
1549 ms 161212 KB
#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
using namespace std;
typedef pair<int, int> pii;

const bool debug = 0;

const int MAXN = 500055;
const int MAXK = 500055;

struct DJF {
	DJF() { init(); }
	int ud[MAXN];

	void init() {
		iota(ud, ud+MAXN, 0);
	}

	int uf(int i) { return i == ud[i] ? i : (ud[i] = uf(ud[i])); }
	void uf(int a, int b) { ud[uf(b)] = uf(a); }
} djf;

vector<int> G[MAXN], TG[MAXN];
vector<int> CV[MAXK];
bitset<MAXN> chk;

int prt[19][MAXN], dep[MAXN], cnt[MAXN], dfo[MAXN];

int A[MAXN], B[MAXN], C[MAXN];

int N, K;

bool ison(int a, int b) {
	return dfo[a] <= dfo[b] && dfo[b] < dfo[a] + cnt[a];
}

int lca(int a, int b) {
	if(dep[a] > dep[b]) swap(a, b);
	const int dt = dep[b] - dep[a];
	for(int i = 19; i--;) if(dt & (1<<i))
		b = prt[i][b];
	if(a == b) return a;
	for(int i = 19; i--;) if(prt[i][a] != prt[i][b]) {
		a = prt[i][a];
		b = prt[i][b];
	}
	return prt[0][a];
}

void dfs(int i, int &c) {
	c++; dfo[i] = c; cnt[i] = 1;
	for(int e : G[i]) {
		int v = A[e] ^ B[e] ^ i;
		if(dep[v]) continue;
		dep[v] = dep[i] + 1;
		prt[0][v] = i;
		dfs(v, c);
		cnt[i] += cnt[v];
	}
}

void getTree(vector<int> &V, vector<pii> &RV) {
	int n = sz(V);
	if(n < 2) return;

	sort(allv(V), [&](int a, int b) { return dfo[a] < dfo[b]; });
	vector<int> TV = V;
	for(int i = 1; i < n; i++) TV.eb(lca(V[i-1], V[i]));
	sort(allv(TV), [&](int a, int b) { return dfo[a] < dfo[b]; });
	univ(TV);

	vector<int> PQ;
	for(int v : TV) {
		for(; !PQ.empty() && !ison(PQ.back(), v); PQ.pop_back());
		if(!PQ.empty()) RV.eb(PQ.back(), v);
		PQ.eb(v);
	}
}

void drawEdge(int p, int v) {
	for(;;) {
		v = djf.uf(v);
		if(dep[v] <= dep[p]) break;
		djf.uf(prt[0][v], v);
	}
}

int f(int i) {
	int ret = 0;
	chk[i] = true;
	for(int v : TG[i]) if(!chk[v]) {
		ret += f(v);
	}
	if(1 == sz(TG[i])) ret++;
	return ret;
}

int main() {
	ios::sync_with_stdio(false);

	cin >> N >> K;
	for(int i = 1; i < N; i++) {
		cin >> A[i] >> B[i];
		if(A[i] > B[i]) swap(A[i], B[i]);
		G[A[i]].eb(i);
		G[B[i]].eb(i);
	}
	for(int i = 1; i <= N; i++) {
		cin >> C[i];
		CV[C[i]].eb(i);
	}

	dep[1] = 1; { int c = 0; dfs(1, c); }
	for(int j = 1; j < 19; j++) for(int i = 1; i <= N; i++)
		prt[j][i] = prt[j-1][prt[j-1][i]];

	for(int i = 1; i <= K; i++) {
		vector<pii> TV;
		getTree(CV[i], TV);
		for(auto &e : TV) drawEdge(e.first, e.second);

		if(debug) {
			printf("color %d\n", i);
			for(auto &e : TV) printf("(%d,%d) ", e.first, e.second);
			puts("");
		}
	}

	for(int i = 1, a, b; i < N; i++) {
		a = djf.uf(A[i]); b = djf.uf(B[i]);
		if(a == b) continue;
		TG[a].eb(b);
		TG[b].eb(a);
	}
	cout << ((f(1) + 1) >> 1) << endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 34 ms 37888 KB Output is correct
2 Correct 34 ms 37752 KB Output is correct
3 Correct 35 ms 37752 KB Output is correct
4 Correct 34 ms 37776 KB Output is correct
5 Correct 33 ms 37752 KB Output is correct
6 Correct 36 ms 37752 KB Output is correct
7 Correct 35 ms 37760 KB Output is correct
8 Correct 36 ms 37752 KB Output is correct
9 Correct 33 ms 37760 KB Output is correct
10 Correct 33 ms 37724 KB Output is correct
11 Correct 34 ms 37724 KB Output is correct
12 Correct 35 ms 37744 KB Output is correct
13 Correct 33 ms 37752 KB Output is correct
14 Correct 36 ms 37760 KB Output is correct
15 Correct 35 ms 37756 KB Output is correct
16 Correct 33 ms 37752 KB Output is correct
17 Correct 33 ms 37752 KB Output is correct
18 Correct 33 ms 37752 KB Output is correct
19 Correct 33 ms 37752 KB Output is correct
20 Correct 34 ms 37760 KB Output is correct
21 Correct 34 ms 37880 KB Output is correct
22 Correct 35 ms 37760 KB Output is correct
23 Correct 33 ms 37752 KB Output is correct
24 Correct 35 ms 37664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 37888 KB Output is correct
2 Correct 34 ms 37752 KB Output is correct
3 Correct 35 ms 37752 KB Output is correct
4 Correct 34 ms 37776 KB Output is correct
5 Correct 33 ms 37752 KB Output is correct
6 Correct 36 ms 37752 KB Output is correct
7 Correct 35 ms 37760 KB Output is correct
8 Correct 36 ms 37752 KB Output is correct
9 Correct 33 ms 37760 KB Output is correct
10 Correct 33 ms 37724 KB Output is correct
11 Correct 34 ms 37724 KB Output is correct
12 Correct 35 ms 37744 KB Output is correct
13 Correct 33 ms 37752 KB Output is correct
14 Correct 36 ms 37760 KB Output is correct
15 Correct 35 ms 37756 KB Output is correct
16 Correct 33 ms 37752 KB Output is correct
17 Correct 33 ms 37752 KB Output is correct
18 Correct 33 ms 37752 KB Output is correct
19 Correct 33 ms 37752 KB Output is correct
20 Correct 34 ms 37760 KB Output is correct
21 Correct 34 ms 37880 KB Output is correct
22 Correct 35 ms 37760 KB Output is correct
23 Correct 33 ms 37752 KB Output is correct
24 Correct 35 ms 37664 KB Output is correct
25 Correct 35 ms 37760 KB Output is correct
26 Correct 36 ms 38260 KB Output is correct
27 Correct 37 ms 38136 KB Output is correct
28 Correct 36 ms 38364 KB Output is correct
29 Correct 36 ms 38392 KB Output is correct
30 Correct 37 ms 38136 KB Output is correct
31 Correct 35 ms 37760 KB Output is correct
32 Correct 36 ms 38400 KB Output is correct
33 Correct 36 ms 37752 KB Output is correct
34 Correct 37 ms 38136 KB Output is correct
35 Correct 36 ms 38392 KB Output is correct
36 Correct 37 ms 38136 KB Output is correct
37 Correct 39 ms 38136 KB Output is correct
38 Correct 37 ms 37752 KB Output is correct
39 Correct 39 ms 38136 KB Output is correct
40 Correct 46 ms 38264 KB Output is correct
41 Correct 38 ms 38136 KB Output is correct
42 Correct 38 ms 38264 KB Output is correct
43 Correct 38 ms 38392 KB Output is correct
44 Correct 34 ms 37752 KB Output is correct
45 Correct 37 ms 38264 KB Output is correct
46 Correct 36 ms 38136 KB Output is correct
47 Correct 34 ms 37724 KB Output is correct
48 Correct 37 ms 38136 KB Output is correct
49 Correct 37 ms 38392 KB Output is correct
50 Correct 37 ms 38392 KB Output is correct
51 Correct 36 ms 38272 KB Output is correct
52 Correct 36 ms 38264 KB Output is correct
53 Correct 35 ms 38264 KB Output is correct
54 Correct 38 ms 38140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 37888 KB Output is correct
2 Correct 34 ms 37752 KB Output is correct
3 Correct 35 ms 37752 KB Output is correct
4 Correct 34 ms 37776 KB Output is correct
5 Correct 33 ms 37752 KB Output is correct
6 Correct 36 ms 37752 KB Output is correct
7 Correct 35 ms 37760 KB Output is correct
8 Correct 36 ms 37752 KB Output is correct
9 Correct 33 ms 37760 KB Output is correct
10 Correct 33 ms 37724 KB Output is correct
11 Correct 34 ms 37724 KB Output is correct
12 Correct 35 ms 37744 KB Output is correct
13 Correct 33 ms 37752 KB Output is correct
14 Correct 36 ms 37760 KB Output is correct
15 Correct 35 ms 37756 KB Output is correct
16 Correct 33 ms 37752 KB Output is correct
17 Correct 33 ms 37752 KB Output is correct
18 Correct 33 ms 37752 KB Output is correct
19 Correct 33 ms 37752 KB Output is correct
20 Correct 34 ms 37760 KB Output is correct
21 Correct 34 ms 37880 KB Output is correct
22 Correct 35 ms 37760 KB Output is correct
23 Correct 33 ms 37752 KB Output is correct
24 Correct 35 ms 37664 KB Output is correct
25 Correct 33 ms 37752 KB Output is correct
26 Correct 143 ms 53060 KB Output is correct
27 Correct 183 ms 52728 KB Output is correct
28 Correct 38 ms 38236 KB Output is correct
29 Correct 35 ms 37752 KB Output is correct
30 Correct 35 ms 37724 KB Output is correct
31 Correct 188 ms 52856 KB Output is correct
32 Correct 37 ms 38264 KB Output is correct
33 Correct 212 ms 58468 KB Output is correct
34 Correct 208 ms 52728 KB Output is correct
35 Correct 39 ms 38136 KB Output is correct
36 Correct 170 ms 53240 KB Output is correct
37 Correct 36 ms 38264 KB Output is correct
38 Correct 36 ms 38188 KB Output is correct
39 Correct 152 ms 53104 KB Output is correct
40 Correct 37 ms 38392 KB Output is correct
41 Correct 183 ms 54928 KB Output is correct
42 Correct 215 ms 54520 KB Output is correct
43 Correct 38 ms 37704 KB Output is correct
44 Correct 197 ms 60900 KB Output is correct
45 Correct 223 ms 55756 KB Output is correct
46 Correct 37 ms 38132 KB Output is correct
47 Correct 38 ms 38208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 141 ms 53100 KB Output is correct
2 Correct 146 ms 59476 KB Output is correct
3 Correct 39 ms 38236 KB Output is correct
4 Correct 37 ms 38200 KB Output is correct
5 Correct 36 ms 37724 KB Output is correct
6 Correct 34 ms 37724 KB Output is correct
7 Correct 40 ms 38136 KB Output is correct
8 Correct 202 ms 54360 KB Output is correct
9 Correct 37 ms 38212 KB Output is correct
10 Correct 178 ms 52852 KB Output is correct
11 Correct 35 ms 37752 KB Output is correct
12 Correct 157 ms 52748 KB Output is correct
13 Correct 190 ms 54652 KB Output is correct
14 Correct 169 ms 58800 KB Output is correct
15 Correct 145 ms 53000 KB Output is correct
16 Correct 37 ms 38264 KB Output is correct
17 Correct 35 ms 37724 KB Output is correct
18 Correct 144 ms 58444 KB Output is correct
19 Correct 207 ms 61816 KB Output is correct
20 Correct 37 ms 38172 KB Output is correct
21 Correct 36 ms 37752 KB Output is correct
22 Correct 170 ms 54920 KB Output is correct
23 Correct 36 ms 38228 KB Output is correct
24 Correct 156 ms 53372 KB Output is correct
25 Correct 173 ms 60856 KB Output is correct
26 Correct 37 ms 38392 KB Output is correct
27 Correct 38 ms 38364 KB Output is correct
28 Correct 36 ms 38136 KB Output is correct
29 Correct 36 ms 38236 KB Output is correct
30 Correct 37 ms 38268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 37888 KB Output is correct
2 Correct 34 ms 37752 KB Output is correct
3 Correct 35 ms 37752 KB Output is correct
4 Correct 34 ms 37776 KB Output is correct
5 Correct 33 ms 37752 KB Output is correct
6 Correct 36 ms 37752 KB Output is correct
7 Correct 35 ms 37760 KB Output is correct
8 Correct 36 ms 37752 KB Output is correct
9 Correct 33 ms 37760 KB Output is correct
10 Correct 33 ms 37724 KB Output is correct
11 Correct 34 ms 37724 KB Output is correct
12 Correct 35 ms 37744 KB Output is correct
13 Correct 33 ms 37752 KB Output is correct
14 Correct 36 ms 37760 KB Output is correct
15 Correct 35 ms 37756 KB Output is correct
16 Correct 33 ms 37752 KB Output is correct
17 Correct 33 ms 37752 KB Output is correct
18 Correct 33 ms 37752 KB Output is correct
19 Correct 33 ms 37752 KB Output is correct
20 Correct 34 ms 37760 KB Output is correct
21 Correct 34 ms 37880 KB Output is correct
22 Correct 35 ms 37760 KB Output is correct
23 Correct 33 ms 37752 KB Output is correct
24 Correct 35 ms 37664 KB Output is correct
25 Correct 35 ms 37760 KB Output is correct
26 Correct 36 ms 38260 KB Output is correct
27 Correct 37 ms 38136 KB Output is correct
28 Correct 36 ms 38364 KB Output is correct
29 Correct 36 ms 38392 KB Output is correct
30 Correct 37 ms 38136 KB Output is correct
31 Correct 35 ms 37760 KB Output is correct
32 Correct 36 ms 38400 KB Output is correct
33 Correct 36 ms 37752 KB Output is correct
34 Correct 37 ms 38136 KB Output is correct
35 Correct 36 ms 38392 KB Output is correct
36 Correct 37 ms 38136 KB Output is correct
37 Correct 39 ms 38136 KB Output is correct
38 Correct 37 ms 37752 KB Output is correct
39 Correct 39 ms 38136 KB Output is correct
40 Correct 46 ms 38264 KB Output is correct
41 Correct 38 ms 38136 KB Output is correct
42 Correct 38 ms 38264 KB Output is correct
43 Correct 38 ms 38392 KB Output is correct
44 Correct 34 ms 37752 KB Output is correct
45 Correct 37 ms 38264 KB Output is correct
46 Correct 36 ms 38136 KB Output is correct
47 Correct 34 ms 37724 KB Output is correct
48 Correct 37 ms 38136 KB Output is correct
49 Correct 37 ms 38392 KB Output is correct
50 Correct 37 ms 38392 KB Output is correct
51 Correct 36 ms 38272 KB Output is correct
52 Correct 36 ms 38264 KB Output is correct
53 Correct 35 ms 38264 KB Output is correct
54 Correct 38 ms 38140 KB Output is correct
55 Correct 33 ms 37752 KB Output is correct
56 Correct 143 ms 53060 KB Output is correct
57 Correct 183 ms 52728 KB Output is correct
58 Correct 38 ms 38236 KB Output is correct
59 Correct 35 ms 37752 KB Output is correct
60 Correct 35 ms 37724 KB Output is correct
61 Correct 188 ms 52856 KB Output is correct
62 Correct 37 ms 38264 KB Output is correct
63 Correct 212 ms 58468 KB Output is correct
64 Correct 208 ms 52728 KB Output is correct
65 Correct 39 ms 38136 KB Output is correct
66 Correct 170 ms 53240 KB Output is correct
67 Correct 36 ms 38264 KB Output is correct
68 Correct 36 ms 38188 KB Output is correct
69 Correct 152 ms 53104 KB Output is correct
70 Correct 37 ms 38392 KB Output is correct
71 Correct 183 ms 54928 KB Output is correct
72 Correct 215 ms 54520 KB Output is correct
73 Correct 38 ms 37704 KB Output is correct
74 Correct 197 ms 60900 KB Output is correct
75 Correct 223 ms 55756 KB Output is correct
76 Correct 37 ms 38132 KB Output is correct
77 Correct 38 ms 38208 KB Output is correct
78 Correct 141 ms 53100 KB Output is correct
79 Correct 146 ms 59476 KB Output is correct
80 Correct 39 ms 38236 KB Output is correct
81 Correct 37 ms 38200 KB Output is correct
82 Correct 36 ms 37724 KB Output is correct
83 Correct 34 ms 37724 KB Output is correct
84 Correct 40 ms 38136 KB Output is correct
85 Correct 202 ms 54360 KB Output is correct
86 Correct 37 ms 38212 KB Output is correct
87 Correct 178 ms 52852 KB Output is correct
88 Correct 35 ms 37752 KB Output is correct
89 Correct 157 ms 52748 KB Output is correct
90 Correct 190 ms 54652 KB Output is correct
91 Correct 169 ms 58800 KB Output is correct
92 Correct 145 ms 53000 KB Output is correct
93 Correct 37 ms 38264 KB Output is correct
94 Correct 35 ms 37724 KB Output is correct
95 Correct 144 ms 58444 KB Output is correct
96 Correct 207 ms 61816 KB Output is correct
97 Correct 37 ms 38172 KB Output is correct
98 Correct 36 ms 37752 KB Output is correct
99 Correct 170 ms 54920 KB Output is correct
100 Correct 36 ms 38228 KB Output is correct
101 Correct 156 ms 53372 KB Output is correct
102 Correct 173 ms 60856 KB Output is correct
103 Correct 37 ms 38392 KB Output is correct
104 Correct 38 ms 38364 KB Output is correct
105 Correct 36 ms 38136 KB Output is correct
106 Correct 36 ms 38236 KB Output is correct
107 Correct 37 ms 38268 KB Output is correct
108 Correct 1039 ms 122076 KB Output is correct
109 Correct 1169 ms 132976 KB Output is correct
110 Correct 1267 ms 146448 KB Output is correct
111 Correct 1534 ms 161212 KB Output is correct
112 Correct 1179 ms 148272 KB Output is correct
113 Correct 990 ms 142584 KB Output is correct
114 Correct 1348 ms 118868 KB Output is correct
115 Correct 1333 ms 122460 KB Output is correct
116 Correct 1549 ms 117100 KB Output is correct
117 Correct 1356 ms 144768 KB Output is correct
118 Correct 1121 ms 113016 KB Output is correct
119 Correct 1372 ms 144656 KB Output is correct
120 Correct 1528 ms 155000 KB Output is correct
121 Correct 1357 ms 144600 KB Output is correct
122 Correct 1306 ms 125100 KB Output is correct
123 Correct 871 ms 147640 KB Output is correct
124 Correct 1353 ms 136496 KB Output is correct