답안 #104752

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
104752 2019-04-09T06:07:13 Z Just_Solve_The_Problem Mergers (JOI19_mergers) C++11
100 / 100
2298 ms 155000 KB
#include <bits/stdc++.h>

using namespace std;

const int N = (int)5e5 + 7;

struct DSU {
	int par[N];
	int siz[N];
	DSU() {
		iota(par, par + N, 0);
		fill(siz, siz + N, 1);
	}
	int getpar(int a) {
		if (par[a] == a) return a;
		return par[a] = getpar(par[a]);
	}
	void connect(int a, int b) {
		a = getpar(a);
		b = getpar(b);
		if (a != b) {
			if (siz[a] > siz[b]) swap(a, b);
			par[a] = b;
			siz[b] += siz[a];
		}
	}
};
DSU dsu;

int n, k;
vector < int > gr[N];
int st[N], s[N], sb[N], prs[N];
int deg[N], tin[N], tout[N];
int lc[20][N];
int tiktak;
vector < int > vec[N];

bool upper(int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

int lca(int a, int b) {
	if (upper(a, b)) return a;
	if (upper(b, a)) return b;
	for (int i = 19; i >= 0; i--) {
		if (!upper(lc[i][a], b)) a = lc[i][a];
	}
	return lc[0][a];
}

void precalc(int v, int pr) {
	tin[v] = ++tiktak;
	lc[0][v] = pr;
	for (int i = 1; i < 20; i++) {
		lc[i][v] = lc[i - 1][lc[i - 1][v]];
	}
	for (int to : gr[v]) {
		if (to == pr) continue;
		precalc(to, v);
	}
	tout[v] = tiktak;
}

void dfs(int v, int pr) {
	sb[prs[s[v]]]++;
	for (int to : gr[v]) {
		if (to == pr) continue;
		dfs(to, v);
		st[v] += (st[to] - sb[to]);
		if (st[to] - sb[to] > 0) {
			dsu.connect(v, to);
		}
	}
	st[v]++;
}

main() {
	scanf("%d %d", &n, &k);
	for (int i = 1; i < n; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		gr[u].push_back(v);
		gr[v].push_back(u);
	}
	for (int i = 1; i <= n; i++) {
		scanf("%d", &s[i]);
		vec[s[i]].push_back(i);
	}
	precalc(1, 1);
	for (int i = 1; i <= k; i++) {
		int v = vec[i][0];
		for (int j = 1; j < vec[i].size(); j++) {
			v = lca(v, vec[i][j]);
		}
		prs[i] = v;
	}
	dfs(1, 1);
	for (int i = 1; i <= n; i++) {
		for (int to : gr[i]) {
			if (dsu.getpar(i) != dsu.getpar(to)) {
				deg[dsu.getpar(i)]++;
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		ans += (deg[i] == 1);
	}
	cout << (ans + 1) / 2;
}

Compilation message

mergers.cpp:77:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
mergers.cpp: In function 'int main()':
mergers.cpp:92:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 1; j < vec[i].size(); j++) {
                   ~~^~~~~~~~~~~~~~~
mergers.cpp:78: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:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:86:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &s[i]);
   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 27904 KB Output is correct
2 Correct 30 ms 27904 KB Output is correct
3 Correct 27 ms 27896 KB Output is correct
4 Correct 27 ms 27904 KB Output is correct
5 Correct 27 ms 27904 KB Output is correct
6 Correct 28 ms 27896 KB Output is correct
7 Correct 32 ms 27904 KB Output is correct
8 Correct 28 ms 27896 KB Output is correct
9 Correct 28 ms 27896 KB Output is correct
10 Correct 27 ms 27904 KB Output is correct
11 Correct 27 ms 27904 KB Output is correct
12 Correct 28 ms 27904 KB Output is correct
13 Correct 30 ms 27852 KB Output is correct
14 Correct 28 ms 27904 KB Output is correct
15 Correct 27 ms 27904 KB Output is correct
16 Correct 27 ms 27896 KB Output is correct
17 Correct 27 ms 27896 KB Output is correct
18 Correct 26 ms 27904 KB Output is correct
19 Correct 30 ms 27904 KB Output is correct
20 Correct 26 ms 27860 KB Output is correct
21 Correct 27 ms 27904 KB Output is correct
22 Correct 27 ms 27904 KB Output is correct
23 Correct 27 ms 27904 KB Output is correct
24 Correct 31 ms 27904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 27904 KB Output is correct
2 Correct 30 ms 27904 KB Output is correct
3 Correct 27 ms 27896 KB Output is correct
4 Correct 27 ms 27904 KB Output is correct
5 Correct 27 ms 27904 KB Output is correct
6 Correct 28 ms 27896 KB Output is correct
7 Correct 32 ms 27904 KB Output is correct
8 Correct 28 ms 27896 KB Output is correct
9 Correct 28 ms 27896 KB Output is correct
10 Correct 27 ms 27904 KB Output is correct
11 Correct 27 ms 27904 KB Output is correct
12 Correct 28 ms 27904 KB Output is correct
13 Correct 30 ms 27852 KB Output is correct
14 Correct 28 ms 27904 KB Output is correct
15 Correct 27 ms 27904 KB Output is correct
16 Correct 27 ms 27896 KB Output is correct
17 Correct 27 ms 27896 KB Output is correct
18 Correct 26 ms 27904 KB Output is correct
19 Correct 30 ms 27904 KB Output is correct
20 Correct 26 ms 27860 KB Output is correct
21 Correct 27 ms 27904 KB Output is correct
22 Correct 27 ms 27904 KB Output is correct
23 Correct 27 ms 27904 KB Output is correct
24 Correct 31 ms 27904 KB Output is correct
25 Correct 26 ms 27896 KB Output is correct
26 Correct 30 ms 28416 KB Output is correct
27 Correct 31 ms 28408 KB Output is correct
28 Correct 36 ms 28664 KB Output is correct
29 Correct 32 ms 28536 KB Output is correct
30 Correct 28 ms 28416 KB Output is correct
31 Correct 29 ms 27904 KB Output is correct
32 Correct 30 ms 28672 KB Output is correct
33 Correct 30 ms 27904 KB Output is correct
34 Correct 30 ms 28416 KB Output is correct
35 Correct 32 ms 28408 KB Output is correct
36 Correct 30 ms 28416 KB Output is correct
37 Correct 33 ms 28352 KB Output is correct
38 Correct 27 ms 27896 KB Output is correct
39 Correct 37 ms 28408 KB Output is correct
40 Correct 29 ms 28272 KB Output is correct
41 Correct 33 ms 28416 KB Output is correct
42 Correct 30 ms 28504 KB Output is correct
43 Correct 31 ms 28664 KB Output is correct
44 Correct 28 ms 28024 KB Output is correct
45 Correct 33 ms 28536 KB Output is correct
46 Correct 33 ms 28416 KB Output is correct
47 Correct 35 ms 27896 KB Output is correct
48 Correct 33 ms 28416 KB Output is correct
49 Correct 33 ms 28536 KB Output is correct
50 Correct 34 ms 28664 KB Output is correct
51 Correct 34 ms 28408 KB Output is correct
52 Correct 32 ms 28416 KB Output is correct
53 Correct 31 ms 28412 KB Output is correct
54 Correct 37 ms 28288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 27904 KB Output is correct
2 Correct 30 ms 27904 KB Output is correct
3 Correct 27 ms 27896 KB Output is correct
4 Correct 27 ms 27904 KB Output is correct
5 Correct 27 ms 27904 KB Output is correct
6 Correct 28 ms 27896 KB Output is correct
7 Correct 32 ms 27904 KB Output is correct
8 Correct 28 ms 27896 KB Output is correct
9 Correct 28 ms 27896 KB Output is correct
10 Correct 27 ms 27904 KB Output is correct
11 Correct 27 ms 27904 KB Output is correct
12 Correct 28 ms 27904 KB Output is correct
13 Correct 30 ms 27852 KB Output is correct
14 Correct 28 ms 27904 KB Output is correct
15 Correct 27 ms 27904 KB Output is correct
16 Correct 27 ms 27896 KB Output is correct
17 Correct 27 ms 27896 KB Output is correct
18 Correct 26 ms 27904 KB Output is correct
19 Correct 30 ms 27904 KB Output is correct
20 Correct 26 ms 27860 KB Output is correct
21 Correct 27 ms 27904 KB Output is correct
22 Correct 27 ms 27904 KB Output is correct
23 Correct 27 ms 27904 KB Output is correct
24 Correct 31 ms 27904 KB Output is correct
25 Correct 27 ms 27904 KB Output is correct
26 Correct 176 ms 42964 KB Output is correct
27 Correct 258 ms 42484 KB Output is correct
28 Correct 33 ms 28408 KB Output is correct
29 Correct 31 ms 28012 KB Output is correct
30 Correct 30 ms 27980 KB Output is correct
31 Correct 244 ms 42876 KB Output is correct
32 Correct 28 ms 28280 KB Output is correct
33 Correct 227 ms 52444 KB Output is correct
34 Correct 241 ms 42516 KB Output is correct
35 Correct 33 ms 28408 KB Output is correct
36 Correct 267 ms 43920 KB Output is correct
37 Correct 37 ms 28416 KB Output is correct
38 Correct 31 ms 28380 KB Output is correct
39 Correct 181 ms 42864 KB Output is correct
40 Correct 39 ms 28672 KB Output is correct
41 Correct 248 ms 42356 KB Output is correct
42 Correct 250 ms 45916 KB Output is correct
43 Correct 29 ms 27896 KB Output is correct
44 Correct 269 ms 52852 KB Output is correct
45 Correct 274 ms 47736 KB Output is correct
46 Correct 31 ms 28408 KB Output is correct
47 Correct 32 ms 28416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 164 ms 41328 KB Output is correct
2 Correct 244 ms 45124 KB Output is correct
3 Correct 33 ms 28536 KB Output is correct
4 Correct 31 ms 28408 KB Output is correct
5 Correct 26 ms 27896 KB Output is correct
6 Correct 29 ms 27900 KB Output is correct
7 Correct 32 ms 28416 KB Output is correct
8 Correct 296 ms 44792 KB Output is correct
9 Correct 34 ms 28416 KB Output is correct
10 Correct 238 ms 43512 KB Output is correct
11 Correct 27 ms 27904 KB Output is correct
12 Correct 231 ms 43392 KB Output is correct
13 Correct 301 ms 44788 KB Output is correct
14 Correct 247 ms 46584 KB Output is correct
15 Correct 169 ms 42708 KB Output is correct
16 Correct 29 ms 28408 KB Output is correct
17 Correct 27 ms 27980 KB Output is correct
18 Correct 252 ms 46680 KB Output is correct
19 Correct 252 ms 52088 KB Output is correct
20 Correct 28 ms 28408 KB Output is correct
21 Correct 30 ms 28032 KB Output is correct
22 Correct 253 ms 45028 KB Output is correct
23 Correct 30 ms 28412 KB Output is correct
24 Correct 226 ms 43768 KB Output is correct
25 Correct 266 ms 50168 KB Output is correct
26 Correct 30 ms 28536 KB Output is correct
27 Correct 34 ms 28536 KB Output is correct
28 Correct 34 ms 28484 KB Output is correct
29 Correct 28 ms 28408 KB Output is correct
30 Correct 35 ms 28408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 27904 KB Output is correct
2 Correct 30 ms 27904 KB Output is correct
3 Correct 27 ms 27896 KB Output is correct
4 Correct 27 ms 27904 KB Output is correct
5 Correct 27 ms 27904 KB Output is correct
6 Correct 28 ms 27896 KB Output is correct
7 Correct 32 ms 27904 KB Output is correct
8 Correct 28 ms 27896 KB Output is correct
9 Correct 28 ms 27896 KB Output is correct
10 Correct 27 ms 27904 KB Output is correct
11 Correct 27 ms 27904 KB Output is correct
12 Correct 28 ms 27904 KB Output is correct
13 Correct 30 ms 27852 KB Output is correct
14 Correct 28 ms 27904 KB Output is correct
15 Correct 27 ms 27904 KB Output is correct
16 Correct 27 ms 27896 KB Output is correct
17 Correct 27 ms 27896 KB Output is correct
18 Correct 26 ms 27904 KB Output is correct
19 Correct 30 ms 27904 KB Output is correct
20 Correct 26 ms 27860 KB Output is correct
21 Correct 27 ms 27904 KB Output is correct
22 Correct 27 ms 27904 KB Output is correct
23 Correct 27 ms 27904 KB Output is correct
24 Correct 31 ms 27904 KB Output is correct
25 Correct 26 ms 27896 KB Output is correct
26 Correct 30 ms 28416 KB Output is correct
27 Correct 31 ms 28408 KB Output is correct
28 Correct 36 ms 28664 KB Output is correct
29 Correct 32 ms 28536 KB Output is correct
30 Correct 28 ms 28416 KB Output is correct
31 Correct 29 ms 27904 KB Output is correct
32 Correct 30 ms 28672 KB Output is correct
33 Correct 30 ms 27904 KB Output is correct
34 Correct 30 ms 28416 KB Output is correct
35 Correct 32 ms 28408 KB Output is correct
36 Correct 30 ms 28416 KB Output is correct
37 Correct 33 ms 28352 KB Output is correct
38 Correct 27 ms 27896 KB Output is correct
39 Correct 37 ms 28408 KB Output is correct
40 Correct 29 ms 28272 KB Output is correct
41 Correct 33 ms 28416 KB Output is correct
42 Correct 30 ms 28504 KB Output is correct
43 Correct 31 ms 28664 KB Output is correct
44 Correct 28 ms 28024 KB Output is correct
45 Correct 33 ms 28536 KB Output is correct
46 Correct 33 ms 28416 KB Output is correct
47 Correct 35 ms 27896 KB Output is correct
48 Correct 33 ms 28416 KB Output is correct
49 Correct 33 ms 28536 KB Output is correct
50 Correct 34 ms 28664 KB Output is correct
51 Correct 34 ms 28408 KB Output is correct
52 Correct 32 ms 28416 KB Output is correct
53 Correct 31 ms 28412 KB Output is correct
54 Correct 37 ms 28288 KB Output is correct
55 Correct 27 ms 27904 KB Output is correct
56 Correct 176 ms 42964 KB Output is correct
57 Correct 258 ms 42484 KB Output is correct
58 Correct 33 ms 28408 KB Output is correct
59 Correct 31 ms 28012 KB Output is correct
60 Correct 30 ms 27980 KB Output is correct
61 Correct 244 ms 42876 KB Output is correct
62 Correct 28 ms 28280 KB Output is correct
63 Correct 227 ms 52444 KB Output is correct
64 Correct 241 ms 42516 KB Output is correct
65 Correct 33 ms 28408 KB Output is correct
66 Correct 267 ms 43920 KB Output is correct
67 Correct 37 ms 28416 KB Output is correct
68 Correct 31 ms 28380 KB Output is correct
69 Correct 181 ms 42864 KB Output is correct
70 Correct 39 ms 28672 KB Output is correct
71 Correct 248 ms 42356 KB Output is correct
72 Correct 250 ms 45916 KB Output is correct
73 Correct 29 ms 27896 KB Output is correct
74 Correct 269 ms 52852 KB Output is correct
75 Correct 274 ms 47736 KB Output is correct
76 Correct 31 ms 28408 KB Output is correct
77 Correct 32 ms 28416 KB Output is correct
78 Correct 164 ms 41328 KB Output is correct
79 Correct 244 ms 45124 KB Output is correct
80 Correct 33 ms 28536 KB Output is correct
81 Correct 31 ms 28408 KB Output is correct
82 Correct 26 ms 27896 KB Output is correct
83 Correct 29 ms 27900 KB Output is correct
84 Correct 32 ms 28416 KB Output is correct
85 Correct 296 ms 44792 KB Output is correct
86 Correct 34 ms 28416 KB Output is correct
87 Correct 238 ms 43512 KB Output is correct
88 Correct 27 ms 27904 KB Output is correct
89 Correct 231 ms 43392 KB Output is correct
90 Correct 301 ms 44788 KB Output is correct
91 Correct 247 ms 46584 KB Output is correct
92 Correct 169 ms 42708 KB Output is correct
93 Correct 29 ms 28408 KB Output is correct
94 Correct 27 ms 27980 KB Output is correct
95 Correct 252 ms 46680 KB Output is correct
96 Correct 252 ms 52088 KB Output is correct
97 Correct 28 ms 28408 KB Output is correct
98 Correct 30 ms 28032 KB Output is correct
99 Correct 253 ms 45028 KB Output is correct
100 Correct 30 ms 28412 KB Output is correct
101 Correct 226 ms 43768 KB Output is correct
102 Correct 266 ms 50168 KB Output is correct
103 Correct 30 ms 28536 KB Output is correct
104 Correct 34 ms 28536 KB Output is correct
105 Correct 34 ms 28484 KB Output is correct
106 Correct 28 ms 28408 KB Output is correct
107 Correct 35 ms 28408 KB Output is correct
108 Correct 1844 ms 113488 KB Output is correct
109 Correct 1765 ms 126160 KB Output is correct
110 Correct 1894 ms 141928 KB Output is correct
111 Correct 2088 ms 155000 KB Output is correct
112 Correct 2167 ms 141248 KB Output is correct
113 Correct 1569 ms 122336 KB Output is correct
114 Correct 1425 ms 100960 KB Output is correct
115 Correct 1436 ms 100712 KB Output is correct
116 Correct 2059 ms 109464 KB Output is correct
117 Correct 1594 ms 122392 KB Output is correct
118 Correct 1400 ms 104312 KB Output is correct
119 Correct 1621 ms 122360 KB Output is correct
120 Correct 2106 ms 142396 KB Output is correct
121 Correct 1827 ms 122488 KB Output is correct
122 Correct 2298 ms 115332 KB Output is correct
123 Correct 1430 ms 124192 KB Output is correct
124 Correct 1903 ms 120852 KB Output is correct