답안 #120621

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
120621 2019-06-25T05:58:10 Z 윤교준(#2960) Mergers (JOI19_mergers) C++14
100 / 100
1537 ms 151588 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 37760 KB Output is correct
2 Correct 34 ms 37760 KB Output is correct
3 Correct 34 ms 37796 KB Output is correct
4 Correct 35 ms 37752 KB Output is correct
5 Correct 33 ms 37752 KB Output is correct
6 Correct 34 ms 37760 KB Output is correct
7 Correct 33 ms 37624 KB Output is correct
8 Correct 34 ms 37752 KB Output is correct
9 Correct 34 ms 37752 KB Output is correct
10 Correct 33 ms 37760 KB Output is correct
11 Correct 33 ms 37732 KB Output is correct
12 Correct 34 ms 37760 KB Output is correct
13 Correct 35 ms 37752 KB Output is correct
14 Correct 36 ms 37760 KB Output is correct
15 Correct 36 ms 37760 KB Output is correct
16 Correct 37 ms 37752 KB Output is correct
17 Correct 36 ms 37760 KB Output is correct
18 Correct 36 ms 37760 KB Output is correct
19 Correct 34 ms 37752 KB Output is correct
20 Correct 33 ms 37760 KB Output is correct
21 Correct 33 ms 37760 KB Output is correct
22 Correct 33 ms 37752 KB Output is correct
23 Correct 33 ms 37760 KB Output is correct
24 Correct 34 ms 37760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 37760 KB Output is correct
2 Correct 34 ms 37760 KB Output is correct
3 Correct 34 ms 37796 KB Output is correct
4 Correct 35 ms 37752 KB Output is correct
5 Correct 33 ms 37752 KB Output is correct
6 Correct 34 ms 37760 KB Output is correct
7 Correct 33 ms 37624 KB Output is correct
8 Correct 34 ms 37752 KB Output is correct
9 Correct 34 ms 37752 KB Output is correct
10 Correct 33 ms 37760 KB Output is correct
11 Correct 33 ms 37732 KB Output is correct
12 Correct 34 ms 37760 KB Output is correct
13 Correct 35 ms 37752 KB Output is correct
14 Correct 36 ms 37760 KB Output is correct
15 Correct 36 ms 37760 KB Output is correct
16 Correct 37 ms 37752 KB Output is correct
17 Correct 36 ms 37760 KB Output is correct
18 Correct 36 ms 37760 KB Output is correct
19 Correct 34 ms 37752 KB Output is correct
20 Correct 33 ms 37760 KB Output is correct
21 Correct 33 ms 37760 KB Output is correct
22 Correct 33 ms 37752 KB Output is correct
23 Correct 33 ms 37760 KB Output is correct
24 Correct 34 ms 37760 KB Output is correct
25 Correct 35 ms 37752 KB Output is correct
26 Correct 38 ms 38264 KB Output is correct
27 Correct 36 ms 38144 KB Output is correct
28 Correct 37 ms 38392 KB Output is correct
29 Correct 36 ms 38400 KB Output is correct
30 Correct 37 ms 38136 KB Output is correct
31 Correct 35 ms 37752 KB Output is correct
32 Correct 37 ms 38500 KB Output is correct
33 Correct 34 ms 37752 KB Output is correct
34 Correct 36 ms 38264 KB Output is correct
35 Correct 37 ms 38352 KB Output is correct
36 Correct 39 ms 38140 KB Output is correct
37 Correct 38 ms 38264 KB Output is correct
38 Correct 36 ms 37676 KB Output is correct
39 Correct 38 ms 38392 KB Output is correct
40 Correct 36 ms 38264 KB Output is correct
41 Correct 38 ms 38136 KB Output is correct
42 Correct 39 ms 38268 KB Output is correct
43 Correct 39 ms 38392 KB Output is correct
44 Correct 35 ms 37752 KB Output is correct
45 Correct 42 ms 38264 KB Output is correct
46 Correct 50 ms 38264 KB Output is correct
47 Correct 37 ms 37760 KB Output is correct
48 Correct 36 ms 38272 KB Output is correct
49 Correct 35 ms 38400 KB Output is correct
50 Correct 37 ms 38392 KB Output is correct
51 Correct 38 ms 38272 KB Output is correct
52 Correct 35 ms 38264 KB Output is correct
53 Correct 36 ms 38272 KB Output is correct
54 Correct 38 ms 38136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 37760 KB Output is correct
2 Correct 34 ms 37760 KB Output is correct
3 Correct 34 ms 37796 KB Output is correct
4 Correct 35 ms 37752 KB Output is correct
5 Correct 33 ms 37752 KB Output is correct
6 Correct 34 ms 37760 KB Output is correct
7 Correct 33 ms 37624 KB Output is correct
8 Correct 34 ms 37752 KB Output is correct
9 Correct 34 ms 37752 KB Output is correct
10 Correct 33 ms 37760 KB Output is correct
11 Correct 33 ms 37732 KB Output is correct
12 Correct 34 ms 37760 KB Output is correct
13 Correct 35 ms 37752 KB Output is correct
14 Correct 36 ms 37760 KB Output is correct
15 Correct 36 ms 37760 KB Output is correct
16 Correct 37 ms 37752 KB Output is correct
17 Correct 36 ms 37760 KB Output is correct
18 Correct 36 ms 37760 KB Output is correct
19 Correct 34 ms 37752 KB Output is correct
20 Correct 33 ms 37760 KB Output is correct
21 Correct 33 ms 37760 KB Output is correct
22 Correct 33 ms 37752 KB Output is correct
23 Correct 33 ms 37760 KB Output is correct
24 Correct 34 ms 37760 KB Output is correct
25 Correct 34 ms 37760 KB Output is correct
26 Correct 123 ms 52080 KB Output is correct
27 Correct 186 ms 51856 KB Output is correct
28 Correct 38 ms 38264 KB Output is correct
29 Correct 36 ms 37760 KB Output is correct
30 Correct 46 ms 37664 KB Output is correct
31 Correct 195 ms 51704 KB Output is correct
32 Correct 38 ms 38264 KB Output is correct
33 Correct 207 ms 57336 KB Output is correct
34 Correct 200 ms 51796 KB Output is correct
35 Correct 39 ms 38136 KB Output is correct
36 Correct 175 ms 52156 KB Output is correct
37 Correct 39 ms 38236 KB Output is correct
38 Correct 36 ms 38136 KB Output is correct
39 Correct 135 ms 52028 KB Output is correct
40 Correct 37 ms 38528 KB Output is correct
41 Correct 172 ms 53844 KB Output is correct
42 Correct 179 ms 53520 KB Output is correct
43 Correct 34 ms 37752 KB Output is correct
44 Correct 198 ms 59976 KB Output is correct
45 Correct 189 ms 54648 KB Output is correct
46 Correct 39 ms 38264 KB Output is correct
47 Correct 39 ms 38136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 51696 KB Output is correct
2 Correct 147 ms 57712 KB Output is correct
3 Correct 37 ms 38272 KB Output is correct
4 Correct 40 ms 38136 KB Output is correct
5 Correct 34 ms 37752 KB Output is correct
6 Correct 34 ms 37752 KB Output is correct
7 Correct 36 ms 38136 KB Output is correct
8 Correct 190 ms 52856 KB Output is correct
9 Correct 37 ms 38136 KB Output is correct
10 Correct 155 ms 51704 KB Output is correct
11 Correct 33 ms 37752 KB Output is correct
12 Correct 166 ms 51704 KB Output is correct
13 Correct 241 ms 53240 KB Output is correct
14 Correct 190 ms 57584 KB Output is correct
15 Correct 150 ms 52108 KB Output is correct
16 Correct 36 ms 38272 KB Output is correct
17 Correct 34 ms 37696 KB Output is correct
18 Correct 138 ms 57068 KB Output is correct
19 Correct 209 ms 60476 KB Output is correct
20 Correct 37 ms 38136 KB Output is correct
21 Correct 37 ms 37732 KB Output is correct
22 Correct 162 ms 53692 KB Output is correct
23 Correct 36 ms 38264 KB Output is correct
24 Correct 184 ms 52088 KB Output is correct
25 Correct 223 ms 59468 KB Output is correct
26 Correct 38 ms 38392 KB Output is correct
27 Correct 38 ms 38420 KB Output is correct
28 Correct 39 ms 38236 KB Output is correct
29 Correct 37 ms 38136 KB Output is correct
30 Correct 37 ms 38264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 37760 KB Output is correct
2 Correct 34 ms 37760 KB Output is correct
3 Correct 34 ms 37796 KB Output is correct
4 Correct 35 ms 37752 KB Output is correct
5 Correct 33 ms 37752 KB Output is correct
6 Correct 34 ms 37760 KB Output is correct
7 Correct 33 ms 37624 KB Output is correct
8 Correct 34 ms 37752 KB Output is correct
9 Correct 34 ms 37752 KB Output is correct
10 Correct 33 ms 37760 KB Output is correct
11 Correct 33 ms 37732 KB Output is correct
12 Correct 34 ms 37760 KB Output is correct
13 Correct 35 ms 37752 KB Output is correct
14 Correct 36 ms 37760 KB Output is correct
15 Correct 36 ms 37760 KB Output is correct
16 Correct 37 ms 37752 KB Output is correct
17 Correct 36 ms 37760 KB Output is correct
18 Correct 36 ms 37760 KB Output is correct
19 Correct 34 ms 37752 KB Output is correct
20 Correct 33 ms 37760 KB Output is correct
21 Correct 33 ms 37760 KB Output is correct
22 Correct 33 ms 37752 KB Output is correct
23 Correct 33 ms 37760 KB Output is correct
24 Correct 34 ms 37760 KB Output is correct
25 Correct 35 ms 37752 KB Output is correct
26 Correct 38 ms 38264 KB Output is correct
27 Correct 36 ms 38144 KB Output is correct
28 Correct 37 ms 38392 KB Output is correct
29 Correct 36 ms 38400 KB Output is correct
30 Correct 37 ms 38136 KB Output is correct
31 Correct 35 ms 37752 KB Output is correct
32 Correct 37 ms 38500 KB Output is correct
33 Correct 34 ms 37752 KB Output is correct
34 Correct 36 ms 38264 KB Output is correct
35 Correct 37 ms 38352 KB Output is correct
36 Correct 39 ms 38140 KB Output is correct
37 Correct 38 ms 38264 KB Output is correct
38 Correct 36 ms 37676 KB Output is correct
39 Correct 38 ms 38392 KB Output is correct
40 Correct 36 ms 38264 KB Output is correct
41 Correct 38 ms 38136 KB Output is correct
42 Correct 39 ms 38268 KB Output is correct
43 Correct 39 ms 38392 KB Output is correct
44 Correct 35 ms 37752 KB Output is correct
45 Correct 42 ms 38264 KB Output is correct
46 Correct 50 ms 38264 KB Output is correct
47 Correct 37 ms 37760 KB Output is correct
48 Correct 36 ms 38272 KB Output is correct
49 Correct 35 ms 38400 KB Output is correct
50 Correct 37 ms 38392 KB Output is correct
51 Correct 38 ms 38272 KB Output is correct
52 Correct 35 ms 38264 KB Output is correct
53 Correct 36 ms 38272 KB Output is correct
54 Correct 38 ms 38136 KB Output is correct
55 Correct 34 ms 37760 KB Output is correct
56 Correct 123 ms 52080 KB Output is correct
57 Correct 186 ms 51856 KB Output is correct
58 Correct 38 ms 38264 KB Output is correct
59 Correct 36 ms 37760 KB Output is correct
60 Correct 46 ms 37664 KB Output is correct
61 Correct 195 ms 51704 KB Output is correct
62 Correct 38 ms 38264 KB Output is correct
63 Correct 207 ms 57336 KB Output is correct
64 Correct 200 ms 51796 KB Output is correct
65 Correct 39 ms 38136 KB Output is correct
66 Correct 175 ms 52156 KB Output is correct
67 Correct 39 ms 38236 KB Output is correct
68 Correct 36 ms 38136 KB Output is correct
69 Correct 135 ms 52028 KB Output is correct
70 Correct 37 ms 38528 KB Output is correct
71 Correct 172 ms 53844 KB Output is correct
72 Correct 179 ms 53520 KB Output is correct
73 Correct 34 ms 37752 KB Output is correct
74 Correct 198 ms 59976 KB Output is correct
75 Correct 189 ms 54648 KB Output is correct
76 Correct 39 ms 38264 KB Output is correct
77 Correct 39 ms 38136 KB Output is correct
78 Correct 152 ms 51696 KB Output is correct
79 Correct 147 ms 57712 KB Output is correct
80 Correct 37 ms 38272 KB Output is correct
81 Correct 40 ms 38136 KB Output is correct
82 Correct 34 ms 37752 KB Output is correct
83 Correct 34 ms 37752 KB Output is correct
84 Correct 36 ms 38136 KB Output is correct
85 Correct 190 ms 52856 KB Output is correct
86 Correct 37 ms 38136 KB Output is correct
87 Correct 155 ms 51704 KB Output is correct
88 Correct 33 ms 37752 KB Output is correct
89 Correct 166 ms 51704 KB Output is correct
90 Correct 241 ms 53240 KB Output is correct
91 Correct 190 ms 57584 KB Output is correct
92 Correct 150 ms 52108 KB Output is correct
93 Correct 36 ms 38272 KB Output is correct
94 Correct 34 ms 37696 KB Output is correct
95 Correct 138 ms 57068 KB Output is correct
96 Correct 209 ms 60476 KB Output is correct
97 Correct 37 ms 38136 KB Output is correct
98 Correct 37 ms 37732 KB Output is correct
99 Correct 162 ms 53692 KB Output is correct
100 Correct 36 ms 38264 KB Output is correct
101 Correct 184 ms 52088 KB Output is correct
102 Correct 223 ms 59468 KB Output is correct
103 Correct 38 ms 38392 KB Output is correct
104 Correct 38 ms 38420 KB Output is correct
105 Correct 39 ms 38236 KB Output is correct
106 Correct 37 ms 38136 KB Output is correct
107 Correct 37 ms 38264 KB Output is correct
108 Correct 1102 ms 112772 KB Output is correct
109 Correct 1230 ms 125980 KB Output is correct
110 Correct 1214 ms 139396 KB Output is correct
111 Correct 1537 ms 151588 KB Output is correct
112 Correct 1146 ms 138676 KB Output is correct
113 Correct 933 ms 133032 KB Output is correct
114 Correct 1175 ms 111500 KB Output is correct
115 Correct 1192 ms 115380 KB Output is correct
116 Correct 1524 ms 107896 KB Output is correct
117 Correct 1302 ms 135032 KB Output is correct
118 Correct 1097 ms 104952 KB Output is correct
119 Correct 1347 ms 134856 KB Output is correct
120 Correct 1464 ms 145372 KB Output is correct
121 Correct 1358 ms 134812 KB Output is correct
122 Correct 1325 ms 115572 KB Output is correct
123 Correct 891 ms 137600 KB Output is correct
124 Correct 1358 ms 126904 KB Output is correct