답안 #391058

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
391058 2021-04-17T17:24:50 Z parsabahrami Mergers (JOI19_mergers) C++17
100 / 100
1030 ms 108300 KB
// Call my Name and Save me from The Dark
#include <bits/stdc++.h>
 
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
 
#define SZ(x)                       (int) x.size()
#define F                           first
#define S                           second

const int N = 5e5 + 10;
int S[N], ps[N], St[N], big[N], C[N], P[N], H[N], M[N], HD[N], A[N], n, k, tim, ret = 0;
vector<int> adj[N], vec[N];

void szDFS(int v, int p = -1) {
	S[v] = 1;
	for (int u : adj[v]) {
		if (u != p) {
			H[u] = H[v] + 1, P[u] = v;
			szDFS(u, v); S[v] += S[u];
			if (S[u] > S[big[v]]) big[v] = u;
		}
	}
}
 
void HLD(int v, int p = -1) {
	if (p == -1) HD[v] = v;
	St[v] = tim++;
	if (big[v]) HD[big[v]] = HD[v], HLD(big[v], v);
	for (int u : adj[v]) {
		if (u != p && u != big[v]) {
			HD[u] = u, HLD(u, v);
		}
	}
}

int LCA(int u, int v){
    while (HD[u] != HD[v]){
        if (H[HD[u]] > H[HD[v]]) u = P[HD[u]];
        else v = P[HD[v]];
    }
    return H[u] < H[v] ? u : v;
}

void DFS(int v, int p = -1) {
    for (int u : adj[v]) {
        if (u != p) {
            DFS(u, v);
            ps[v] += ps[u], C[v] += C[u];
            if (!ps[u]) M[u] = 1;
        }
    }
    if (~p && !ps[v] && !C[v]) C[v] = 1;
}

void calcDFS(int v, int p = -1, int cnt = 0) {
    int sum = 0, id = 0;
    if (M[v] && !cnt) ret++, cnt = 1;
    for (int u : adj[v]) {
        if (u != p) {
            sum += C[u];
            if (C[u] >= C[id]) id = u;
        }
    }
    if (sum - C[id] + cnt >= C[id]) ret += max(0, (sum - cnt) / 2) + (max(0, (sum - cnt)) & 1);
    else ret += sum - C[id], calcDFS(id, v, cnt + sum - C[id]);
}

int main() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i < n; i++) {
        int u, v; scanf("%d%d", &u, &v);
        adj[v].push_back(u);
        adj[u].push_back(v);
    }
    for (int i = 1; i <= n; i++) scanf("%d", &A[i]), vec[A[i]].push_back(i);
    szDFS(1); HLD(1);
    for (int i = 1; i <= k; i++) {
        sort(vec[i].begin(), vec[i].end(), [&] (int u, int v) { return St[u] < St[v]; });
        for (int j = 0; j + 1 < SZ(vec[i]); j++) {
            int p = LCA(vec[i][j], vec[i][j + 1]);
            ps[p] -= 2, ps[vec[i][j + 1]]++, ps[vec[i][j]]++;
        }
    }
    DFS(1);
    calcDFS(1);
    printf("%d\n", ret);
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   72 |     scanf("%d%d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:74:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   74 |         int u, v; scanf("%d%d", &u, &v);
      |                   ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:78:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   78 |     for (int i = 1; i <= n; i++) scanf("%d", &A[i]), vec[A[i]].push_back(i);
      |                                  ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 14 ms 23776 KB Output is correct
3 Correct 16 ms 23752 KB Output is correct
4 Correct 14 ms 23856 KB Output is correct
5 Correct 14 ms 23756 KB Output is correct
6 Correct 15 ms 23788 KB Output is correct
7 Correct 15 ms 23756 KB Output is correct
8 Correct 14 ms 23756 KB Output is correct
9 Correct 15 ms 23792 KB Output is correct
10 Correct 14 ms 23756 KB Output is correct
11 Correct 14 ms 23756 KB Output is correct
12 Correct 14 ms 23856 KB Output is correct
13 Correct 14 ms 23804 KB Output is correct
14 Correct 14 ms 23884 KB Output is correct
15 Correct 14 ms 23756 KB Output is correct
16 Correct 14 ms 23756 KB Output is correct
17 Correct 16 ms 23788 KB Output is correct
18 Correct 14 ms 23792 KB Output is correct
19 Correct 14 ms 23792 KB Output is correct
20 Correct 15 ms 23884 KB Output is correct
21 Correct 17 ms 23856 KB Output is correct
22 Correct 14 ms 23824 KB Output is correct
23 Correct 14 ms 23756 KB Output is correct
24 Correct 16 ms 23760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 14 ms 23776 KB Output is correct
3 Correct 16 ms 23752 KB Output is correct
4 Correct 14 ms 23856 KB Output is correct
5 Correct 14 ms 23756 KB Output is correct
6 Correct 15 ms 23788 KB Output is correct
7 Correct 15 ms 23756 KB Output is correct
8 Correct 14 ms 23756 KB Output is correct
9 Correct 15 ms 23792 KB Output is correct
10 Correct 14 ms 23756 KB Output is correct
11 Correct 14 ms 23756 KB Output is correct
12 Correct 14 ms 23856 KB Output is correct
13 Correct 14 ms 23804 KB Output is correct
14 Correct 14 ms 23884 KB Output is correct
15 Correct 14 ms 23756 KB Output is correct
16 Correct 14 ms 23756 KB Output is correct
17 Correct 16 ms 23788 KB Output is correct
18 Correct 14 ms 23792 KB Output is correct
19 Correct 14 ms 23792 KB Output is correct
20 Correct 15 ms 23884 KB Output is correct
21 Correct 17 ms 23856 KB Output is correct
22 Correct 14 ms 23824 KB Output is correct
23 Correct 14 ms 23756 KB Output is correct
24 Correct 16 ms 23760 KB Output is correct
25 Correct 14 ms 23796 KB Output is correct
26 Correct 17 ms 24140 KB Output is correct
27 Correct 16 ms 24064 KB Output is correct
28 Correct 17 ms 24268 KB Output is correct
29 Correct 18 ms 24140 KB Output is correct
30 Correct 16 ms 24112 KB Output is correct
31 Correct 14 ms 23792 KB Output is correct
32 Correct 17 ms 24268 KB Output is correct
33 Correct 14 ms 23756 KB Output is correct
34 Correct 16 ms 24012 KB Output is correct
35 Correct 16 ms 24140 KB Output is correct
36 Correct 16 ms 24140 KB Output is correct
37 Correct 19 ms 24164 KB Output is correct
38 Correct 14 ms 23788 KB Output is correct
39 Correct 16 ms 24144 KB Output is correct
40 Correct 17 ms 24112 KB Output is correct
41 Correct 17 ms 24040 KB Output is correct
42 Correct 17 ms 24156 KB Output is correct
43 Correct 16 ms 24328 KB Output is correct
44 Correct 17 ms 23792 KB Output is correct
45 Correct 17 ms 24140 KB Output is correct
46 Correct 17 ms 24160 KB Output is correct
47 Correct 14 ms 23756 KB Output is correct
48 Correct 17 ms 24064 KB Output is correct
49 Correct 17 ms 24132 KB Output is correct
50 Correct 16 ms 24264 KB Output is correct
51 Correct 17 ms 24140 KB Output is correct
52 Correct 18 ms 24140 KB Output is correct
53 Correct 17 ms 24044 KB Output is correct
54 Correct 16 ms 24064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 14 ms 23776 KB Output is correct
3 Correct 16 ms 23752 KB Output is correct
4 Correct 14 ms 23856 KB Output is correct
5 Correct 14 ms 23756 KB Output is correct
6 Correct 15 ms 23788 KB Output is correct
7 Correct 15 ms 23756 KB Output is correct
8 Correct 14 ms 23756 KB Output is correct
9 Correct 15 ms 23792 KB Output is correct
10 Correct 14 ms 23756 KB Output is correct
11 Correct 14 ms 23756 KB Output is correct
12 Correct 14 ms 23856 KB Output is correct
13 Correct 14 ms 23804 KB Output is correct
14 Correct 14 ms 23884 KB Output is correct
15 Correct 14 ms 23756 KB Output is correct
16 Correct 14 ms 23756 KB Output is correct
17 Correct 16 ms 23788 KB Output is correct
18 Correct 14 ms 23792 KB Output is correct
19 Correct 14 ms 23792 KB Output is correct
20 Correct 15 ms 23884 KB Output is correct
21 Correct 17 ms 23856 KB Output is correct
22 Correct 14 ms 23824 KB Output is correct
23 Correct 14 ms 23756 KB Output is correct
24 Correct 16 ms 23760 KB Output is correct
25 Correct 14 ms 23800 KB Output is correct
26 Correct 81 ms 32052 KB Output is correct
27 Correct 113 ms 32540 KB Output is correct
28 Correct 17 ms 24064 KB Output is correct
29 Correct 15 ms 23872 KB Output is correct
30 Correct 14 ms 23780 KB Output is correct
31 Correct 110 ms 32584 KB Output is correct
32 Correct 16 ms 24064 KB Output is correct
33 Correct 125 ms 39696 KB Output is correct
34 Correct 111 ms 32480 KB Output is correct
35 Correct 17 ms 24108 KB Output is correct
36 Correct 111 ms 33344 KB Output is correct
37 Correct 16 ms 24012 KB Output is correct
38 Correct 16 ms 24140 KB Output is correct
39 Correct 81 ms 31960 KB Output is correct
40 Correct 17 ms 24252 KB Output is correct
41 Correct 134 ms 32532 KB Output is correct
42 Correct 158 ms 34884 KB Output is correct
43 Correct 15 ms 23756 KB Output is correct
44 Correct 139 ms 40000 KB Output is correct
45 Correct 116 ms 36220 KB Output is correct
46 Correct 16 ms 24064 KB Output is correct
47 Correct 17 ms 24012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 32060 KB Output is correct
2 Correct 88 ms 35384 KB Output is correct
3 Correct 17 ms 24140 KB Output is correct
4 Correct 19 ms 24140 KB Output is correct
5 Correct 15 ms 23792 KB Output is correct
6 Correct 15 ms 23768 KB Output is correct
7 Correct 17 ms 24012 KB Output is correct
8 Correct 118 ms 34244 KB Output is correct
9 Correct 16 ms 24064 KB Output is correct
10 Correct 114 ms 33132 KB Output is correct
11 Correct 14 ms 23756 KB Output is correct
12 Correct 120 ms 32980 KB Output is correct
13 Correct 122 ms 34128 KB Output is correct
14 Correct 120 ms 35836 KB Output is correct
15 Correct 84 ms 32016 KB Output is correct
16 Correct 16 ms 24140 KB Output is correct
17 Correct 14 ms 23772 KB Output is correct
18 Correct 90 ms 35420 KB Output is correct
19 Correct 127 ms 39676 KB Output is correct
20 Correct 16 ms 24140 KB Output is correct
21 Correct 14 ms 23792 KB Output is correct
22 Correct 86 ms 34056 KB Output is correct
23 Correct 17 ms 24144 KB Output is correct
24 Correct 125 ms 33352 KB Output is correct
25 Correct 135 ms 38340 KB Output is correct
26 Correct 16 ms 24196 KB Output is correct
27 Correct 17 ms 24192 KB Output is correct
28 Correct 17 ms 24140 KB Output is correct
29 Correct 16 ms 24140 KB Output is correct
30 Correct 16 ms 24140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 14 ms 23776 KB Output is correct
3 Correct 16 ms 23752 KB Output is correct
4 Correct 14 ms 23856 KB Output is correct
5 Correct 14 ms 23756 KB Output is correct
6 Correct 15 ms 23788 KB Output is correct
7 Correct 15 ms 23756 KB Output is correct
8 Correct 14 ms 23756 KB Output is correct
9 Correct 15 ms 23792 KB Output is correct
10 Correct 14 ms 23756 KB Output is correct
11 Correct 14 ms 23756 KB Output is correct
12 Correct 14 ms 23856 KB Output is correct
13 Correct 14 ms 23804 KB Output is correct
14 Correct 14 ms 23884 KB Output is correct
15 Correct 14 ms 23756 KB Output is correct
16 Correct 14 ms 23756 KB Output is correct
17 Correct 16 ms 23788 KB Output is correct
18 Correct 14 ms 23792 KB Output is correct
19 Correct 14 ms 23792 KB Output is correct
20 Correct 15 ms 23884 KB Output is correct
21 Correct 17 ms 23856 KB Output is correct
22 Correct 14 ms 23824 KB Output is correct
23 Correct 14 ms 23756 KB Output is correct
24 Correct 16 ms 23760 KB Output is correct
25 Correct 14 ms 23796 KB Output is correct
26 Correct 17 ms 24140 KB Output is correct
27 Correct 16 ms 24064 KB Output is correct
28 Correct 17 ms 24268 KB Output is correct
29 Correct 18 ms 24140 KB Output is correct
30 Correct 16 ms 24112 KB Output is correct
31 Correct 14 ms 23792 KB Output is correct
32 Correct 17 ms 24268 KB Output is correct
33 Correct 14 ms 23756 KB Output is correct
34 Correct 16 ms 24012 KB Output is correct
35 Correct 16 ms 24140 KB Output is correct
36 Correct 16 ms 24140 KB Output is correct
37 Correct 19 ms 24164 KB Output is correct
38 Correct 14 ms 23788 KB Output is correct
39 Correct 16 ms 24144 KB Output is correct
40 Correct 17 ms 24112 KB Output is correct
41 Correct 17 ms 24040 KB Output is correct
42 Correct 17 ms 24156 KB Output is correct
43 Correct 16 ms 24328 KB Output is correct
44 Correct 17 ms 23792 KB Output is correct
45 Correct 17 ms 24140 KB Output is correct
46 Correct 17 ms 24160 KB Output is correct
47 Correct 14 ms 23756 KB Output is correct
48 Correct 17 ms 24064 KB Output is correct
49 Correct 17 ms 24132 KB Output is correct
50 Correct 16 ms 24264 KB Output is correct
51 Correct 17 ms 24140 KB Output is correct
52 Correct 18 ms 24140 KB Output is correct
53 Correct 17 ms 24044 KB Output is correct
54 Correct 16 ms 24064 KB Output is correct
55 Correct 14 ms 23800 KB Output is correct
56 Correct 81 ms 32052 KB Output is correct
57 Correct 113 ms 32540 KB Output is correct
58 Correct 17 ms 24064 KB Output is correct
59 Correct 15 ms 23872 KB Output is correct
60 Correct 14 ms 23780 KB Output is correct
61 Correct 110 ms 32584 KB Output is correct
62 Correct 16 ms 24064 KB Output is correct
63 Correct 125 ms 39696 KB Output is correct
64 Correct 111 ms 32480 KB Output is correct
65 Correct 17 ms 24108 KB Output is correct
66 Correct 111 ms 33344 KB Output is correct
67 Correct 16 ms 24012 KB Output is correct
68 Correct 16 ms 24140 KB Output is correct
69 Correct 81 ms 31960 KB Output is correct
70 Correct 17 ms 24252 KB Output is correct
71 Correct 134 ms 32532 KB Output is correct
72 Correct 158 ms 34884 KB Output is correct
73 Correct 15 ms 23756 KB Output is correct
74 Correct 139 ms 40000 KB Output is correct
75 Correct 116 ms 36220 KB Output is correct
76 Correct 16 ms 24064 KB Output is correct
77 Correct 17 ms 24012 KB Output is correct
78 Correct 79 ms 32060 KB Output is correct
79 Correct 88 ms 35384 KB Output is correct
80 Correct 17 ms 24140 KB Output is correct
81 Correct 19 ms 24140 KB Output is correct
82 Correct 15 ms 23792 KB Output is correct
83 Correct 15 ms 23768 KB Output is correct
84 Correct 17 ms 24012 KB Output is correct
85 Correct 118 ms 34244 KB Output is correct
86 Correct 16 ms 24064 KB Output is correct
87 Correct 114 ms 33132 KB Output is correct
88 Correct 14 ms 23756 KB Output is correct
89 Correct 120 ms 32980 KB Output is correct
90 Correct 122 ms 34128 KB Output is correct
91 Correct 120 ms 35836 KB Output is correct
92 Correct 84 ms 32016 KB Output is correct
93 Correct 16 ms 24140 KB Output is correct
94 Correct 14 ms 23772 KB Output is correct
95 Correct 90 ms 35420 KB Output is correct
96 Correct 127 ms 39676 KB Output is correct
97 Correct 16 ms 24140 KB Output is correct
98 Correct 14 ms 23792 KB Output is correct
99 Correct 86 ms 34056 KB Output is correct
100 Correct 17 ms 24144 KB Output is correct
101 Correct 125 ms 33352 KB Output is correct
102 Correct 135 ms 38340 KB Output is correct
103 Correct 16 ms 24196 KB Output is correct
104 Correct 17 ms 24192 KB Output is correct
105 Correct 17 ms 24140 KB Output is correct
106 Correct 16 ms 24140 KB Output is correct
107 Correct 16 ms 24140 KB Output is correct
108 Correct 647 ms 75432 KB Output is correct
109 Correct 927 ms 85296 KB Output is correct
110 Correct 924 ms 96468 KB Output is correct
111 Correct 1028 ms 108300 KB Output is correct
112 Correct 966 ms 98336 KB Output is correct
113 Correct 653 ms 83164 KB Output is correct
114 Correct 861 ms 67388 KB Output is correct
115 Correct 846 ms 67252 KB Output is correct
116 Correct 996 ms 73636 KB Output is correct
117 Correct 922 ms 85188 KB Output is correct
118 Correct 822 ms 69848 KB Output is correct
119 Correct 913 ms 85240 KB Output is correct
120 Correct 1030 ms 99128 KB Output is correct
121 Correct 906 ms 85100 KB Output is correct
122 Correct 1004 ms 78776 KB Output is correct
123 Correct 603 ms 82692 KB Output is correct
124 Correct 1002 ms 83932 KB Output is correct