Submission #366398

# Submission time Handle Problem Language Result Execution time Memory
366398 2021-02-14T06:37:52 Z Kazalika Mergers (JOI19_mergers) C++14
100 / 100
1573 ms 226828 KB
#include <bits/stdc++.h>
using namespace std;
 
const int N = 5e5 + 5;
 
int n, k, s[N], cnt[N];
 
vector<int> g[N];
 
int ptr[N];
map<int, int> mp[N];
int comp[N], deg[N];
 
int gptr(int v) {
  if (v == ptr[v])
    return v;
  return ptr[v] = gptr(ptr[v]);
}
 
void merge(int a, int b) {
  a = gptr(a), b = gptr(b);
  if (mp[a].size() > mp[b].size())
    swap(a, b);
  ptr[a] = b;
  for (auto &p : mp[a]) {
    mp[b][p.first] += p.second;
    if (mp[b][p.first] == cnt[p.first])
      comp[b]++;
  }
}
 
vector<pair<int, int>> gd;
 
void go(int v, int p = -1) {
  ptr[v] = v;
  mp[ptr[v]][s[v]]++;
  if (cnt[s[v]] == 1)
    comp[ptr[v]]++;
  for (int u : g[v]) {
    if (u == p)
      continue;
    go(u, v);
    if (comp[gptr(u)] == mp[gptr(u)].size()) {
      deg[v]++;
      deg[u]++;
    } else
      gd.emplace_back(v, u);
    merge(v, u);
  }
}
 
int par[N], sm[N], rnk[N];
 
int gpar(int a) {
  if (a == par[a])
    return a;
  return par[a] = gpar(par[a]);
}
void mrg(int a, int b) {
  a = gpar(a), b = gpar(b);
  if (a == b)
    return;
  if (rnk[a] > rnk[b])
    swap(a, b);
  rnk[b]++;
  par[a] = b;
}
 
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> k;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
    cnt[s[i]]++;
  }
  go(0);
  iota(par, par + n, 0);
  for (auto &e : gd)
    mrg(e.first, e.second);
  for (int i = 0; i < n; ++i)
    sm[gpar(i)] += deg[i];
  int res = 0;
  for (int i = 0; i < n; ++i)
    if (sm[i] == 1)
      res++;
  cout << (res + 1) / 2;
}

Compilation message

mergers.cpp: In function 'void go(int, int)':
mergers.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     if (comp[gptr(u)] == mp[gptr(u)].size()) {
      |         ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35564 KB Output is correct
2 Correct 25 ms 35692 KB Output is correct
3 Correct 25 ms 35692 KB Output is correct
4 Correct 24 ms 35564 KB Output is correct
5 Correct 24 ms 35564 KB Output is correct
6 Correct 27 ms 35692 KB Output is correct
7 Correct 26 ms 35564 KB Output is correct
8 Correct 24 ms 35692 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 24 ms 35564 KB Output is correct
11 Correct 24 ms 35692 KB Output is correct
12 Correct 24 ms 35692 KB Output is correct
13 Correct 24 ms 35564 KB Output is correct
14 Correct 24 ms 35692 KB Output is correct
15 Correct 24 ms 35692 KB Output is correct
16 Correct 27 ms 35564 KB Output is correct
17 Correct 23 ms 35564 KB Output is correct
18 Correct 27 ms 35692 KB Output is correct
19 Correct 23 ms 35564 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 24 ms 35948 KB Output is correct
22 Correct 25 ms 35820 KB Output is correct
23 Correct 23 ms 35564 KB Output is correct
24 Correct 32 ms 35564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35564 KB Output is correct
2 Correct 25 ms 35692 KB Output is correct
3 Correct 25 ms 35692 KB Output is correct
4 Correct 24 ms 35564 KB Output is correct
5 Correct 24 ms 35564 KB Output is correct
6 Correct 27 ms 35692 KB Output is correct
7 Correct 26 ms 35564 KB Output is correct
8 Correct 24 ms 35692 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 24 ms 35564 KB Output is correct
11 Correct 24 ms 35692 KB Output is correct
12 Correct 24 ms 35692 KB Output is correct
13 Correct 24 ms 35564 KB Output is correct
14 Correct 24 ms 35692 KB Output is correct
15 Correct 24 ms 35692 KB Output is correct
16 Correct 27 ms 35564 KB Output is correct
17 Correct 23 ms 35564 KB Output is correct
18 Correct 27 ms 35692 KB Output is correct
19 Correct 23 ms 35564 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 24 ms 35948 KB Output is correct
22 Correct 25 ms 35820 KB Output is correct
23 Correct 23 ms 35564 KB Output is correct
24 Correct 32 ms 35564 KB Output is correct
25 Correct 23 ms 35692 KB Output is correct
26 Correct 27 ms 36332 KB Output is correct
27 Correct 28 ms 36076 KB Output is correct
28 Correct 30 ms 36332 KB Output is correct
29 Correct 28 ms 36588 KB Output is correct
30 Correct 27 ms 36204 KB Output is correct
31 Correct 24 ms 35692 KB Output is correct
32 Correct 27 ms 36460 KB Output is correct
33 Correct 24 ms 35692 KB Output is correct
34 Correct 26 ms 35948 KB Output is correct
35 Correct 30 ms 36560 KB Output is correct
36 Correct 35 ms 36076 KB Output is correct
37 Correct 37 ms 36332 KB Output is correct
38 Correct 25 ms 35692 KB Output is correct
39 Correct 29 ms 36460 KB Output is correct
40 Correct 28 ms 35948 KB Output is correct
41 Correct 25 ms 35948 KB Output is correct
42 Correct 27 ms 36076 KB Output is correct
43 Correct 28 ms 36332 KB Output is correct
44 Correct 31 ms 35692 KB Output is correct
45 Correct 26 ms 36332 KB Output is correct
46 Correct 27 ms 36332 KB Output is correct
47 Correct 23 ms 35692 KB Output is correct
48 Correct 29 ms 36108 KB Output is correct
49 Correct 26 ms 36204 KB Output is correct
50 Correct 26 ms 36332 KB Output is correct
51 Correct 26 ms 36076 KB Output is correct
52 Correct 26 ms 35948 KB Output is correct
53 Correct 29 ms 36076 KB Output is correct
54 Correct 27 ms 36204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35564 KB Output is correct
2 Correct 25 ms 35692 KB Output is correct
3 Correct 25 ms 35692 KB Output is correct
4 Correct 24 ms 35564 KB Output is correct
5 Correct 24 ms 35564 KB Output is correct
6 Correct 27 ms 35692 KB Output is correct
7 Correct 26 ms 35564 KB Output is correct
8 Correct 24 ms 35692 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 24 ms 35564 KB Output is correct
11 Correct 24 ms 35692 KB Output is correct
12 Correct 24 ms 35692 KB Output is correct
13 Correct 24 ms 35564 KB Output is correct
14 Correct 24 ms 35692 KB Output is correct
15 Correct 24 ms 35692 KB Output is correct
16 Correct 27 ms 35564 KB Output is correct
17 Correct 23 ms 35564 KB Output is correct
18 Correct 27 ms 35692 KB Output is correct
19 Correct 23 ms 35564 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 24 ms 35948 KB Output is correct
22 Correct 25 ms 35820 KB Output is correct
23 Correct 23 ms 35564 KB Output is correct
24 Correct 32 ms 35564 KB Output is correct
25 Correct 24 ms 35692 KB Output is correct
26 Correct 104 ms 46688 KB Output is correct
27 Correct 177 ms 53576 KB Output is correct
28 Correct 26 ms 36076 KB Output is correct
29 Correct 24 ms 35692 KB Output is correct
30 Correct 24 ms 35692 KB Output is correct
31 Correct 130 ms 47332 KB Output is correct
32 Correct 26 ms 35948 KB Output is correct
33 Correct 155 ms 58080 KB Output is correct
34 Correct 167 ms 52324 KB Output is correct
35 Correct 26 ms 36076 KB Output is correct
36 Correct 137 ms 48480 KB Output is correct
37 Correct 26 ms 35948 KB Output is correct
38 Correct 26 ms 35948 KB Output is correct
39 Correct 105 ms 46684 KB Output is correct
40 Correct 26 ms 36332 KB Output is correct
41 Correct 136 ms 46816 KB Output is correct
42 Correct 155 ms 50212 KB Output is correct
43 Correct 24 ms 35692 KB Output is correct
44 Correct 144 ms 58532 KB Output is correct
45 Correct 150 ms 53984 KB Output is correct
46 Correct 26 ms 35948 KB Output is correct
47 Correct 27 ms 36204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 46728 KB Output is correct
2 Correct 209 ms 52192 KB Output is correct
3 Correct 27 ms 36332 KB Output is correct
4 Correct 28 ms 36076 KB Output is correct
5 Correct 24 ms 35692 KB Output is correct
6 Correct 27 ms 35692 KB Output is correct
7 Correct 25 ms 35948 KB Output is correct
8 Correct 217 ms 61020 KB Output is correct
9 Correct 25 ms 36076 KB Output is correct
10 Correct 137 ms 48096 KB Output is correct
11 Correct 23 ms 35692 KB Output is correct
12 Correct 129 ms 48096 KB Output is correct
13 Correct 221 ms 59936 KB Output is correct
14 Correct 268 ms 71532 KB Output is correct
15 Correct 111 ms 46820 KB Output is correct
16 Correct 27 ms 36076 KB Output is correct
17 Correct 22 ms 35692 KB Output is correct
18 Correct 207 ms 52836 KB Output is correct
19 Correct 244 ms 60780 KB Output is correct
20 Correct 27 ms 36332 KB Output is correct
21 Correct 23 ms 35692 KB Output is correct
22 Correct 184 ms 50292 KB Output is correct
23 Correct 27 ms 36076 KB Output is correct
24 Correct 154 ms 49888 KB Output is correct
25 Correct 241 ms 58732 KB Output is correct
26 Correct 29 ms 36204 KB Output is correct
27 Correct 28 ms 36332 KB Output is correct
28 Correct 27 ms 36076 KB Output is correct
29 Correct 26 ms 35948 KB Output is correct
30 Correct 27 ms 36076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35564 KB Output is correct
2 Correct 25 ms 35692 KB Output is correct
3 Correct 25 ms 35692 KB Output is correct
4 Correct 24 ms 35564 KB Output is correct
5 Correct 24 ms 35564 KB Output is correct
6 Correct 27 ms 35692 KB Output is correct
7 Correct 26 ms 35564 KB Output is correct
8 Correct 24 ms 35692 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 24 ms 35564 KB Output is correct
11 Correct 24 ms 35692 KB Output is correct
12 Correct 24 ms 35692 KB Output is correct
13 Correct 24 ms 35564 KB Output is correct
14 Correct 24 ms 35692 KB Output is correct
15 Correct 24 ms 35692 KB Output is correct
16 Correct 27 ms 35564 KB Output is correct
17 Correct 23 ms 35564 KB Output is correct
18 Correct 27 ms 35692 KB Output is correct
19 Correct 23 ms 35564 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 24 ms 35948 KB Output is correct
22 Correct 25 ms 35820 KB Output is correct
23 Correct 23 ms 35564 KB Output is correct
24 Correct 32 ms 35564 KB Output is correct
25 Correct 23 ms 35692 KB Output is correct
26 Correct 27 ms 36332 KB Output is correct
27 Correct 28 ms 36076 KB Output is correct
28 Correct 30 ms 36332 KB Output is correct
29 Correct 28 ms 36588 KB Output is correct
30 Correct 27 ms 36204 KB Output is correct
31 Correct 24 ms 35692 KB Output is correct
32 Correct 27 ms 36460 KB Output is correct
33 Correct 24 ms 35692 KB Output is correct
34 Correct 26 ms 35948 KB Output is correct
35 Correct 30 ms 36560 KB Output is correct
36 Correct 35 ms 36076 KB Output is correct
37 Correct 37 ms 36332 KB Output is correct
38 Correct 25 ms 35692 KB Output is correct
39 Correct 29 ms 36460 KB Output is correct
40 Correct 28 ms 35948 KB Output is correct
41 Correct 25 ms 35948 KB Output is correct
42 Correct 27 ms 36076 KB Output is correct
43 Correct 28 ms 36332 KB Output is correct
44 Correct 31 ms 35692 KB Output is correct
45 Correct 26 ms 36332 KB Output is correct
46 Correct 27 ms 36332 KB Output is correct
47 Correct 23 ms 35692 KB Output is correct
48 Correct 29 ms 36108 KB Output is correct
49 Correct 26 ms 36204 KB Output is correct
50 Correct 26 ms 36332 KB Output is correct
51 Correct 26 ms 36076 KB Output is correct
52 Correct 26 ms 35948 KB Output is correct
53 Correct 29 ms 36076 KB Output is correct
54 Correct 27 ms 36204 KB Output is correct
55 Correct 24 ms 35692 KB Output is correct
56 Correct 104 ms 46688 KB Output is correct
57 Correct 177 ms 53576 KB Output is correct
58 Correct 26 ms 36076 KB Output is correct
59 Correct 24 ms 35692 KB Output is correct
60 Correct 24 ms 35692 KB Output is correct
61 Correct 130 ms 47332 KB Output is correct
62 Correct 26 ms 35948 KB Output is correct
63 Correct 155 ms 58080 KB Output is correct
64 Correct 167 ms 52324 KB Output is correct
65 Correct 26 ms 36076 KB Output is correct
66 Correct 137 ms 48480 KB Output is correct
67 Correct 26 ms 35948 KB Output is correct
68 Correct 26 ms 35948 KB Output is correct
69 Correct 105 ms 46684 KB Output is correct
70 Correct 26 ms 36332 KB Output is correct
71 Correct 136 ms 46816 KB Output is correct
72 Correct 155 ms 50212 KB Output is correct
73 Correct 24 ms 35692 KB Output is correct
74 Correct 144 ms 58532 KB Output is correct
75 Correct 150 ms 53984 KB Output is correct
76 Correct 26 ms 35948 KB Output is correct
77 Correct 27 ms 36204 KB Output is correct
78 Correct 116 ms 46728 KB Output is correct
79 Correct 209 ms 52192 KB Output is correct
80 Correct 27 ms 36332 KB Output is correct
81 Correct 28 ms 36076 KB Output is correct
82 Correct 24 ms 35692 KB Output is correct
83 Correct 27 ms 35692 KB Output is correct
84 Correct 25 ms 35948 KB Output is correct
85 Correct 217 ms 61020 KB Output is correct
86 Correct 25 ms 36076 KB Output is correct
87 Correct 137 ms 48096 KB Output is correct
88 Correct 23 ms 35692 KB Output is correct
89 Correct 129 ms 48096 KB Output is correct
90 Correct 221 ms 59936 KB Output is correct
91 Correct 268 ms 71532 KB Output is correct
92 Correct 111 ms 46820 KB Output is correct
93 Correct 27 ms 36076 KB Output is correct
94 Correct 22 ms 35692 KB Output is correct
95 Correct 207 ms 52836 KB Output is correct
96 Correct 244 ms 60780 KB Output is correct
97 Correct 27 ms 36332 KB Output is correct
98 Correct 23 ms 35692 KB Output is correct
99 Correct 184 ms 50292 KB Output is correct
100 Correct 27 ms 36076 KB Output is correct
101 Correct 154 ms 49888 KB Output is correct
102 Correct 241 ms 58732 KB Output is correct
103 Correct 29 ms 36204 KB Output is correct
104 Correct 28 ms 36332 KB Output is correct
105 Correct 27 ms 36076 KB Output is correct
106 Correct 26 ms 35948 KB Output is correct
107 Correct 27 ms 36076 KB Output is correct
108 Correct 1085 ms 102872 KB Output is correct
109 Correct 816 ms 115240 KB Output is correct
110 Correct 837 ms 133080 KB Output is correct
111 Correct 1465 ms 159972 KB Output is correct
112 Correct 1539 ms 149364 KB Output is correct
113 Correct 1272 ms 113492 KB Output is correct
114 Correct 761 ms 88020 KB Output is correct
115 Correct 747 ms 88152 KB Output is correct
116 Correct 1212 ms 165080 KB Output is correct
117 Correct 1573 ms 226828 KB Output is correct
118 Correct 742 ms 91404 KB Output is correct
119 Correct 1551 ms 222700 KB Output is correct
120 Correct 1473 ms 147840 KB Output is correct
121 Correct 1562 ms 220088 KB Output is correct
122 Correct 1569 ms 215632 KB Output is correct
123 Correct 1269 ms 114780 KB Output is correct
124 Correct 1359 ms 177244 KB Output is correct