Submission #377949

# Submission time Handle Problem Language Result Execution time Memory
377949 2021-03-15T15:29:28 Z 2qbingxuan Mergers (JOI19_mergers) C++14
100 / 100
1428 ms 126600 KB
#include <bits/stdc++.h>
#ifdef local
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
    int cnt = sizeof...(T);
    ((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : "\033[0m)\n")));
}
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define pb emplace_back
#define sort_uni(v) sort(all(v)), v.erase(unique(all(v)), v.end())

using namespace std;
using ll = int64_t;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1e18;
const int maxn = 500025, inf = 1e9;

struct Dsu {
    vector<int> pa, rk;
    Dsu(int n) : pa(n), rk(n) { iota(all(pa), 0); }
    int anc(int x) { return x==pa[x] ? x : pa[x] = anc(pa[x]); }
    bool join(int x, int y) {
        if ((x=anc(x)) == (y=anc(y))) return false;
        if (rk[x] < rk[y]) swap(x, y);
        return pa[y] = x, rk[x] != rk[y] || ++rk[x];
    }
} dsu(maxn);

vector<int> g[maxn], adj[maxn];
int color[maxn];
int cnt[maxn];
map<int,int> mp[maxn];
void join(map<int,int> &a, map<int,int> &b) {
    if (a.size() < b.size()) a.swap(b);
    for (auto [x, y]: b) {
        a[x] += y;
        if (a[x] == cnt[x]) a.erase(x);
    }
    b.clear();
}
void dfs(int i, int p) {
    for (int j: g[i]) {
        if (j == p) continue;
        dfs(j, i);
        join(mp[i], mp[j]);
    }
    mp[i][color[i]] += 1;
    if (mp[i][color[i]] == cnt[color[i]])
        mp[i].erase(color[i]);
    debug(i);
    for (auto [a, b]: mp[i])
        debug(a, b);
    if (!mp[i].empty())
        dsu.join(i, p);
}

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n, k;
    cin >> n >> k;
    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    for (int i = 1; i <= n; i++) cin >> color[i], ++cnt[color[i]];
    dfs(1, 0);

    vector<int> deg(n + 1);
    for (int i = 1; i <= n; i++) {
        for (int j: g[i]) {
            int a = dsu.anc(i);
            int b = dsu.anc(j);
            if (a != b)
                adj[a].push_back(b);
        }
    }
    for (int i = 1; i <= n; i++) sort_uni(adj[i]);
    int leaf = 0;
    for (int i = 1; i <= n; i++) {
        int deg = adj[i].size();
        debug(deg);
        if (deg == 1) ++leaf;
    }
    debug(leaf);
    cout << (leaf + 1) / 2 << '\n';
}

Compilation message

mergers.cpp: In function 'void join(std::map<int, int>&, std::map<int, int>&)':
mergers.cpp:40:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   40 |     for (auto [x, y]: b) {
      |               ^
mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:56:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   56 |     for (auto [a, b]: mp[i])
      |               ^
mergers.cpp:56:15: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
   56 |     for (auto [a, b]: mp[i])
      |               ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 34 ms 51200 KB Output is correct
2 Correct 34 ms 51308 KB Output is correct
3 Correct 35 ms 51180 KB Output is correct
4 Correct 35 ms 51180 KB Output is correct
5 Correct 39 ms 51180 KB Output is correct
6 Correct 35 ms 51332 KB Output is correct
7 Correct 34 ms 51180 KB Output is correct
8 Correct 35 ms 51180 KB Output is correct
9 Correct 35 ms 51308 KB Output is correct
10 Correct 34 ms 51180 KB Output is correct
11 Correct 38 ms 51180 KB Output is correct
12 Correct 37 ms 51308 KB Output is correct
13 Correct 34 ms 51180 KB Output is correct
14 Correct 33 ms 51180 KB Output is correct
15 Correct 34 ms 51180 KB Output is correct
16 Correct 35 ms 51308 KB Output is correct
17 Correct 33 ms 51180 KB Output is correct
18 Correct 37 ms 51304 KB Output is correct
19 Correct 34 ms 51180 KB Output is correct
20 Correct 35 ms 51308 KB Output is correct
21 Correct 34 ms 51180 KB Output is correct
22 Correct 32 ms 51308 KB Output is correct
23 Correct 34 ms 51180 KB Output is correct
24 Correct 32 ms 51180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 51200 KB Output is correct
2 Correct 34 ms 51308 KB Output is correct
3 Correct 35 ms 51180 KB Output is correct
4 Correct 35 ms 51180 KB Output is correct
5 Correct 39 ms 51180 KB Output is correct
6 Correct 35 ms 51332 KB Output is correct
7 Correct 34 ms 51180 KB Output is correct
8 Correct 35 ms 51180 KB Output is correct
9 Correct 35 ms 51308 KB Output is correct
10 Correct 34 ms 51180 KB Output is correct
11 Correct 38 ms 51180 KB Output is correct
12 Correct 37 ms 51308 KB Output is correct
13 Correct 34 ms 51180 KB Output is correct
14 Correct 33 ms 51180 KB Output is correct
15 Correct 34 ms 51180 KB Output is correct
16 Correct 35 ms 51308 KB Output is correct
17 Correct 33 ms 51180 KB Output is correct
18 Correct 37 ms 51304 KB Output is correct
19 Correct 34 ms 51180 KB Output is correct
20 Correct 35 ms 51308 KB Output is correct
21 Correct 34 ms 51180 KB Output is correct
22 Correct 32 ms 51308 KB Output is correct
23 Correct 34 ms 51180 KB Output is correct
24 Correct 32 ms 51180 KB Output is correct
25 Correct 35 ms 51180 KB Output is correct
26 Correct 37 ms 51436 KB Output is correct
27 Correct 34 ms 51436 KB Output is correct
28 Correct 34 ms 51692 KB Output is correct
29 Correct 33 ms 51564 KB Output is correct
30 Correct 35 ms 51436 KB Output is correct
31 Correct 32 ms 51180 KB Output is correct
32 Correct 33 ms 51692 KB Output is correct
33 Correct 32 ms 51180 KB Output is correct
34 Correct 34 ms 51424 KB Output is correct
35 Correct 35 ms 51584 KB Output is correct
36 Correct 33 ms 51436 KB Output is correct
37 Correct 34 ms 51436 KB Output is correct
38 Correct 33 ms 51180 KB Output is correct
39 Correct 37 ms 51436 KB Output is correct
40 Correct 38 ms 51436 KB Output is correct
41 Correct 33 ms 51436 KB Output is correct
42 Correct 40 ms 51492 KB Output is correct
43 Correct 34 ms 51692 KB Output is correct
44 Correct 31 ms 51180 KB Output is correct
45 Correct 35 ms 51436 KB Output is correct
46 Correct 41 ms 51436 KB Output is correct
47 Correct 34 ms 51180 KB Output is correct
48 Correct 40 ms 51436 KB Output is correct
49 Correct 34 ms 51564 KB Output is correct
50 Correct 36 ms 51564 KB Output is correct
51 Correct 34 ms 51436 KB Output is correct
52 Correct 33 ms 51436 KB Output is correct
53 Correct 40 ms 51436 KB Output is correct
54 Correct 36 ms 51436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 51200 KB Output is correct
2 Correct 34 ms 51308 KB Output is correct
3 Correct 35 ms 51180 KB Output is correct
4 Correct 35 ms 51180 KB Output is correct
5 Correct 39 ms 51180 KB Output is correct
6 Correct 35 ms 51332 KB Output is correct
7 Correct 34 ms 51180 KB Output is correct
8 Correct 35 ms 51180 KB Output is correct
9 Correct 35 ms 51308 KB Output is correct
10 Correct 34 ms 51180 KB Output is correct
11 Correct 38 ms 51180 KB Output is correct
12 Correct 37 ms 51308 KB Output is correct
13 Correct 34 ms 51180 KB Output is correct
14 Correct 33 ms 51180 KB Output is correct
15 Correct 34 ms 51180 KB Output is correct
16 Correct 35 ms 51308 KB Output is correct
17 Correct 33 ms 51180 KB Output is correct
18 Correct 37 ms 51304 KB Output is correct
19 Correct 34 ms 51180 KB Output is correct
20 Correct 35 ms 51308 KB Output is correct
21 Correct 34 ms 51180 KB Output is correct
22 Correct 32 ms 51308 KB Output is correct
23 Correct 34 ms 51180 KB Output is correct
24 Correct 32 ms 51180 KB Output is correct
25 Correct 31 ms 51180 KB Output is correct
26 Correct 118 ms 56164 KB Output is correct
27 Correct 173 ms 55916 KB Output is correct
28 Correct 33 ms 51436 KB Output is correct
29 Correct 32 ms 51180 KB Output is correct
30 Correct 33 ms 51180 KB Output is correct
31 Correct 139 ms 55916 KB Output is correct
32 Correct 34 ms 51436 KB Output is correct
33 Correct 164 ms 67436 KB Output is correct
34 Correct 154 ms 55916 KB Output is correct
35 Correct 34 ms 51436 KB Output is correct
36 Correct 131 ms 57196 KB Output is correct
37 Correct 34 ms 51436 KB Output is correct
38 Correct 34 ms 51436 KB Output is correct
39 Correct 112 ms 56168 KB Output is correct
40 Correct 34 ms 51692 KB Output is correct
41 Correct 140 ms 55916 KB Output is correct
42 Correct 152 ms 59628 KB Output is correct
43 Correct 33 ms 51180 KB Output is correct
44 Correct 132 ms 67948 KB Output is correct
45 Correct 156 ms 62700 KB Output is correct
46 Correct 34 ms 51436 KB Output is correct
47 Correct 34 ms 51436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 56164 KB Output is correct
2 Correct 124 ms 60136 KB Output is correct
3 Correct 34 ms 51436 KB Output is correct
4 Correct 34 ms 51436 KB Output is correct
5 Correct 34 ms 51180 KB Output is correct
6 Correct 32 ms 51308 KB Output is correct
7 Correct 34 ms 51436 KB Output is correct
8 Correct 146 ms 56556 KB Output is correct
9 Correct 36 ms 51436 KB Output is correct
10 Correct 119 ms 55916 KB Output is correct
11 Correct 33 ms 51180 KB Output is correct
12 Correct 122 ms 56084 KB Output is correct
13 Correct 145 ms 56812 KB Output is correct
14 Correct 145 ms 59500 KB Output is correct
15 Correct 101 ms 56164 KB Output is correct
16 Correct 35 ms 51436 KB Output is correct
17 Correct 32 ms 51180 KB Output is correct
18 Correct 124 ms 59360 KB Output is correct
19 Correct 186 ms 65772 KB Output is correct
20 Correct 34 ms 51436 KB Output is correct
21 Correct 32 ms 51180 KB Output is correct
22 Correct 146 ms 57052 KB Output is correct
23 Correct 34 ms 51436 KB Output is correct
24 Correct 141 ms 56172 KB Output is correct
25 Correct 138 ms 63624 KB Output is correct
26 Correct 34 ms 51564 KB Output is correct
27 Correct 34 ms 51564 KB Output is correct
28 Correct 34 ms 51436 KB Output is correct
29 Correct 35 ms 51436 KB Output is correct
30 Correct 35 ms 51584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 51200 KB Output is correct
2 Correct 34 ms 51308 KB Output is correct
3 Correct 35 ms 51180 KB Output is correct
4 Correct 35 ms 51180 KB Output is correct
5 Correct 39 ms 51180 KB Output is correct
6 Correct 35 ms 51332 KB Output is correct
7 Correct 34 ms 51180 KB Output is correct
8 Correct 35 ms 51180 KB Output is correct
9 Correct 35 ms 51308 KB Output is correct
10 Correct 34 ms 51180 KB Output is correct
11 Correct 38 ms 51180 KB Output is correct
12 Correct 37 ms 51308 KB Output is correct
13 Correct 34 ms 51180 KB Output is correct
14 Correct 33 ms 51180 KB Output is correct
15 Correct 34 ms 51180 KB Output is correct
16 Correct 35 ms 51308 KB Output is correct
17 Correct 33 ms 51180 KB Output is correct
18 Correct 37 ms 51304 KB Output is correct
19 Correct 34 ms 51180 KB Output is correct
20 Correct 35 ms 51308 KB Output is correct
21 Correct 34 ms 51180 KB Output is correct
22 Correct 32 ms 51308 KB Output is correct
23 Correct 34 ms 51180 KB Output is correct
24 Correct 32 ms 51180 KB Output is correct
25 Correct 35 ms 51180 KB Output is correct
26 Correct 37 ms 51436 KB Output is correct
27 Correct 34 ms 51436 KB Output is correct
28 Correct 34 ms 51692 KB Output is correct
29 Correct 33 ms 51564 KB Output is correct
30 Correct 35 ms 51436 KB Output is correct
31 Correct 32 ms 51180 KB Output is correct
32 Correct 33 ms 51692 KB Output is correct
33 Correct 32 ms 51180 KB Output is correct
34 Correct 34 ms 51424 KB Output is correct
35 Correct 35 ms 51584 KB Output is correct
36 Correct 33 ms 51436 KB Output is correct
37 Correct 34 ms 51436 KB Output is correct
38 Correct 33 ms 51180 KB Output is correct
39 Correct 37 ms 51436 KB Output is correct
40 Correct 38 ms 51436 KB Output is correct
41 Correct 33 ms 51436 KB Output is correct
42 Correct 40 ms 51492 KB Output is correct
43 Correct 34 ms 51692 KB Output is correct
44 Correct 31 ms 51180 KB Output is correct
45 Correct 35 ms 51436 KB Output is correct
46 Correct 41 ms 51436 KB Output is correct
47 Correct 34 ms 51180 KB Output is correct
48 Correct 40 ms 51436 KB Output is correct
49 Correct 34 ms 51564 KB Output is correct
50 Correct 36 ms 51564 KB Output is correct
51 Correct 34 ms 51436 KB Output is correct
52 Correct 33 ms 51436 KB Output is correct
53 Correct 40 ms 51436 KB Output is correct
54 Correct 36 ms 51436 KB Output is correct
55 Correct 31 ms 51180 KB Output is correct
56 Correct 118 ms 56164 KB Output is correct
57 Correct 173 ms 55916 KB Output is correct
58 Correct 33 ms 51436 KB Output is correct
59 Correct 32 ms 51180 KB Output is correct
60 Correct 33 ms 51180 KB Output is correct
61 Correct 139 ms 55916 KB Output is correct
62 Correct 34 ms 51436 KB Output is correct
63 Correct 164 ms 67436 KB Output is correct
64 Correct 154 ms 55916 KB Output is correct
65 Correct 34 ms 51436 KB Output is correct
66 Correct 131 ms 57196 KB Output is correct
67 Correct 34 ms 51436 KB Output is correct
68 Correct 34 ms 51436 KB Output is correct
69 Correct 112 ms 56168 KB Output is correct
70 Correct 34 ms 51692 KB Output is correct
71 Correct 140 ms 55916 KB Output is correct
72 Correct 152 ms 59628 KB Output is correct
73 Correct 33 ms 51180 KB Output is correct
74 Correct 132 ms 67948 KB Output is correct
75 Correct 156 ms 62700 KB Output is correct
76 Correct 34 ms 51436 KB Output is correct
77 Correct 34 ms 51436 KB Output is correct
78 Correct 105 ms 56164 KB Output is correct
79 Correct 124 ms 60136 KB Output is correct
80 Correct 34 ms 51436 KB Output is correct
81 Correct 34 ms 51436 KB Output is correct
82 Correct 34 ms 51180 KB Output is correct
83 Correct 32 ms 51308 KB Output is correct
84 Correct 34 ms 51436 KB Output is correct
85 Correct 146 ms 56556 KB Output is correct
86 Correct 36 ms 51436 KB Output is correct
87 Correct 119 ms 55916 KB Output is correct
88 Correct 33 ms 51180 KB Output is correct
89 Correct 122 ms 56084 KB Output is correct
90 Correct 145 ms 56812 KB Output is correct
91 Correct 145 ms 59500 KB Output is correct
92 Correct 101 ms 56164 KB Output is correct
93 Correct 35 ms 51436 KB Output is correct
94 Correct 32 ms 51180 KB Output is correct
95 Correct 124 ms 59360 KB Output is correct
96 Correct 186 ms 65772 KB Output is correct
97 Correct 34 ms 51436 KB Output is correct
98 Correct 32 ms 51180 KB Output is correct
99 Correct 146 ms 57052 KB Output is correct
100 Correct 34 ms 51436 KB Output is correct
101 Correct 141 ms 56172 KB Output is correct
102 Correct 138 ms 63624 KB Output is correct
103 Correct 34 ms 51564 KB Output is correct
104 Correct 34 ms 51564 KB Output is correct
105 Correct 34 ms 51436 KB Output is correct
106 Correct 35 ms 51436 KB Output is correct
107 Correct 35 ms 51584 KB Output is correct
108 Correct 883 ms 80112 KB Output is correct
109 Correct 735 ms 101112 KB Output is correct
110 Correct 762 ms 119148 KB Output is correct
111 Correct 893 ms 126600 KB Output is correct
112 Correct 914 ms 105576 KB Output is correct
113 Correct 672 ms 89960 KB Output is correct
114 Correct 770 ms 71916 KB Output is correct
115 Correct 764 ms 72044 KB Output is correct
116 Correct 1120 ms 73964 KB Output is correct
117 Correct 901 ms 90748 KB Output is correct
118 Correct 739 ms 71936 KB Output is correct
119 Correct 866 ms 90936 KB Output is correct
120 Correct 878 ms 112800 KB Output is correct
121 Correct 823 ms 90696 KB Output is correct
122 Correct 1428 ms 80540 KB Output is correct
123 Correct 630 ms 93532 KB Output is correct
124 Correct 888 ms 84460 KB Output is correct