Submission #604400

# Submission time Handle Problem Language Result Execution time Memory
604400 2022-07-25T05:42:40 Z talant117408 Mergers (JOI19_mergers) C++17
100 / 100
2591 ms 200196 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define long                unsigned long 
#define pb                  push_back
#define mp                  make_pair
#define all(v)              (v).begin(),(v).end()
#define rall(v)             (v).rbegin(),(v).rend()
#define lb                  lower_bound
#define ub                  upper_bound
#define sz(v)               int((v).size())
#define do_not_disturb      ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                '\n'
#define PI                  2*acos(0.0)

const int N = 5e5+7;
vector <int> graph[N], comps_graph[N];
int color[N], states[N], comp[N], num;
int tin[N], tout[N], timer;
int color_lca[N];
int up[N][20];
int n;
int heavy[N], head[N], pos[N];
map <pii, bool> separators;

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

int get_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(up[a][i], b)) {
            a = up[a][i];
        }
    }
    return up[a][0];
}

// HLD
int dfs(int v, int p) {
    heavy[v] = -1;
    tin[v] = ++timer;
    up[v][0] = p;
    for (int i = 1; i < 20; i++) {
        up[v][i] = up[up[v][i-1]][i-1];
    }
    int saizu = 1, mx_saizu = 0;
    
    for (auto to : graph[v]) {
        if (to == p) continue;
        auto res = dfs(to, v);
        saizu += res;
        if (res > mx_saizu) {
            mx_saizu = res;
            heavy[v] = to;
        }
    }
    tout[v] = ++timer;
    return saizu;
}

void decompose(int v, int h) {
    pos[v] = ++timer;
    head[v] = h;
    if (heavy[v] != -1) {
        decompose(heavy[v], h);
    }
    for (auto to : graph[v]) {
        if (to != up[v][0] && to != heavy[v]) {
            decompose(to, to);
        }
    }
}

int tree[N*4], lz[N*4];

void push(int v, int l, int r) {
    if (lz[v] != 0) {
        tree[v] += (r - l + 1) * lz[v];
        if (l != r) {
            lz[v * 2] += lz[v];
            lz[v * 2 + 1] += lz[v];
        }
        lz[v] = 0;
    }
}

void update(int v, int l, int r, int ql, int qr, int val) {
    push(v, l, r);
    if (ql > r || l > qr) return ;
    if (ql <= l && r <= qr) {
        lz[v] += val;
        push(v, l, r);
        return;
    }
    int mid = (l + r) >> 1;
    update(v*2, l, mid, ql, qr, val);
    update(v*2+1, mid+1, r, ql, qr, val);
    tree[v] = tree[v*2] + tree[v*2+1];
}

int get(int v, int l, int r, int ql, int qr) {
    push(v, l, r);
    if (ql > r || l > qr) return 0;
    if (ql <= l && r <= qr) return tree[v];
    int mid = (l + r) >> 1;
    return get(v*2, l, mid, ql, qr) + get(v*2+1, mid+1, r, ql, qr);
}

void hld_update(int v, int origin) {
    for (; head[v] != head[origin]; v = up[head[v]][0]) {
        update(1, 1, n, pos[head[v]], pos[v], 1);
    }
    update(1, 1, n, pos[origin], pos[v], 1);
    update(1, 1, n, pos[origin], pos[origin], -1);
}

void find_states(int v, int p) {
    if (get(1, 1, n, pos[v], pos[v]) == 0) {
        separators[mp(min(v, p), max(v, p))] = 1;
    }
    for (auto to : graph[v]) {
        if (to == p) continue;
        find_states(to, v);
    }
}

void find_comps(int v, int p) {
    comp[v] = num;
    for (auto to : graph[v]) {
        if (to == p || separators[mp(min(v, to), max(v, to))]) continue;
        find_comps(to, v);
    }
}

void solve(int test) {
    int k;
    cin >> n >> k;
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].pb(b);
        graph[b].pb(a);
    }
    for (int i = 1; i <= n; i++) {
        cin >> color[i];
        states[color[i]]++;
    }
    dfs(1, 1);
    timer = 0;
    decompose(1, 1);
    
    vector <pii> LCAS[k + 1];
    for (int i = 1; i <= n; i++) {
        LCAS[color[i]].pb(mp(tin[i], i));
    }
    for (int i = 1; i <= k; i++) {
        sort(all(LCAS[i]));
        int tmp = LCAS[i][0].second;
        for (int j = 1; j < sz(LCAS[i]); j++) {
            tmp = get_lca(tmp, LCAS[i][j].second);
        }
        color_lca[i] = tmp;
    }
    
    for (int i = 1; i <= n; i++) {
        hld_update(i, color_lca[color[i]]);
    }
    
    find_states(1, 1);
    for (int i = 1; i <= n; i++) {
        if (!comp[i]) {
            num++;
            find_comps(i, i);
        }
    }
    for (int i = 1; i <= n; i++) {
        for (auto to : graph[i]) {
            if (comp[i] != comp[to]) {
                comps_graph[comp[i]].pb(comp[to]);
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= num; i++) {
        if (sz(comps_graph[i]) == 1) ans++;
    }
    cout << (ans + 1) / 2 << endl;
}  

int main() {
    do_not_disturb
    
    int t = 1;
    //~ cin >> t;
    for (int i = 1; i <= t; i++) {
        solve(i);
    }
    
    return 0;
}
/*
11 6
1 2
2 4
4 3
2 5
5 6
2 8
8 7
7 9
7 10 
10 11
5 6 4 3 4 5 2 1 2 1 2
*/
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23892 KB Output is correct
2 Correct 14 ms 23920 KB Output is correct
3 Correct 12 ms 23852 KB Output is correct
4 Correct 11 ms 23892 KB Output is correct
5 Correct 13 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 12 ms 23892 KB Output is correct
8 Correct 13 ms 23872 KB Output is correct
9 Correct 12 ms 23892 KB Output is correct
10 Correct 12 ms 23892 KB Output is correct
11 Correct 13 ms 23892 KB Output is correct
12 Correct 12 ms 23892 KB Output is correct
13 Correct 12 ms 23880 KB Output is correct
14 Correct 12 ms 23880 KB Output is correct
15 Correct 12 ms 23916 KB Output is correct
16 Correct 12 ms 23792 KB Output is correct
17 Correct 12 ms 23812 KB Output is correct
18 Correct 12 ms 23908 KB Output is correct
19 Correct 12 ms 23892 KB Output is correct
20 Correct 13 ms 23844 KB Output is correct
21 Correct 12 ms 23892 KB Output is correct
22 Correct 11 ms 23924 KB Output is correct
23 Correct 12 ms 23892 KB Output is correct
24 Correct 13 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23892 KB Output is correct
2 Correct 14 ms 23920 KB Output is correct
3 Correct 12 ms 23852 KB Output is correct
4 Correct 11 ms 23892 KB Output is correct
5 Correct 13 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 12 ms 23892 KB Output is correct
8 Correct 13 ms 23872 KB Output is correct
9 Correct 12 ms 23892 KB Output is correct
10 Correct 12 ms 23892 KB Output is correct
11 Correct 13 ms 23892 KB Output is correct
12 Correct 12 ms 23892 KB Output is correct
13 Correct 12 ms 23880 KB Output is correct
14 Correct 12 ms 23880 KB Output is correct
15 Correct 12 ms 23916 KB Output is correct
16 Correct 12 ms 23792 KB Output is correct
17 Correct 12 ms 23812 KB Output is correct
18 Correct 12 ms 23908 KB Output is correct
19 Correct 12 ms 23892 KB Output is correct
20 Correct 13 ms 23844 KB Output is correct
21 Correct 12 ms 23892 KB Output is correct
22 Correct 11 ms 23924 KB Output is correct
23 Correct 12 ms 23892 KB Output is correct
24 Correct 13 ms 23764 KB Output is correct
25 Correct 12 ms 23892 KB Output is correct
26 Correct 17 ms 24668 KB Output is correct
27 Correct 16 ms 24540 KB Output is correct
28 Correct 17 ms 24824 KB Output is correct
29 Correct 19 ms 24916 KB Output is correct
30 Correct 22 ms 24516 KB Output is correct
31 Correct 12 ms 23892 KB Output is correct
32 Correct 18 ms 24928 KB Output is correct
33 Correct 12 ms 23892 KB Output is correct
34 Correct 16 ms 24608 KB Output is correct
35 Correct 17 ms 24788 KB Output is correct
36 Correct 17 ms 24528 KB Output is correct
37 Correct 17 ms 24636 KB Output is correct
38 Correct 12 ms 23892 KB Output is correct
39 Correct 17 ms 24660 KB Output is correct
40 Correct 16 ms 24532 KB Output is correct
41 Correct 16 ms 24544 KB Output is correct
42 Correct 16 ms 24576 KB Output is correct
43 Correct 16 ms 24840 KB Output is correct
44 Correct 12 ms 23892 KB Output is correct
45 Correct 16 ms 24752 KB Output is correct
46 Correct 17 ms 24660 KB Output is correct
47 Correct 12 ms 23892 KB Output is correct
48 Correct 16 ms 24532 KB Output is correct
49 Correct 16 ms 24808 KB Output is correct
50 Correct 18 ms 24788 KB Output is correct
51 Correct 17 ms 24532 KB Output is correct
52 Correct 15 ms 24532 KB Output is correct
53 Correct 16 ms 24672 KB Output is correct
54 Correct 17 ms 24532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23892 KB Output is correct
2 Correct 14 ms 23920 KB Output is correct
3 Correct 12 ms 23852 KB Output is correct
4 Correct 11 ms 23892 KB Output is correct
5 Correct 13 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 12 ms 23892 KB Output is correct
8 Correct 13 ms 23872 KB Output is correct
9 Correct 12 ms 23892 KB Output is correct
10 Correct 12 ms 23892 KB Output is correct
11 Correct 13 ms 23892 KB Output is correct
12 Correct 12 ms 23892 KB Output is correct
13 Correct 12 ms 23880 KB Output is correct
14 Correct 12 ms 23880 KB Output is correct
15 Correct 12 ms 23916 KB Output is correct
16 Correct 12 ms 23792 KB Output is correct
17 Correct 12 ms 23812 KB Output is correct
18 Correct 12 ms 23908 KB Output is correct
19 Correct 12 ms 23892 KB Output is correct
20 Correct 13 ms 23844 KB Output is correct
21 Correct 12 ms 23892 KB Output is correct
22 Correct 11 ms 23924 KB Output is correct
23 Correct 12 ms 23892 KB Output is correct
24 Correct 13 ms 23764 KB Output is correct
25 Correct 13 ms 23904 KB Output is correct
26 Correct 197 ms 46876 KB Output is correct
27 Correct 333 ms 46744 KB Output is correct
28 Correct 16 ms 24532 KB Output is correct
29 Correct 15 ms 23812 KB Output is correct
30 Correct 12 ms 23828 KB Output is correct
31 Correct 293 ms 46720 KB Output is correct
32 Correct 16 ms 24532 KB Output is correct
33 Correct 249 ms 55464 KB Output is correct
34 Correct 390 ms 46896 KB Output is correct
35 Correct 20 ms 24524 KB Output is correct
36 Correct 255 ms 47580 KB Output is correct
37 Correct 17 ms 24532 KB Output is correct
38 Correct 19 ms 24516 KB Output is correct
39 Correct 206 ms 46836 KB Output is correct
40 Correct 16 ms 24832 KB Output is correct
41 Correct 359 ms 46772 KB Output is correct
42 Correct 268 ms 50616 KB Output is correct
43 Correct 12 ms 23892 KB Output is correct
44 Correct 258 ms 55740 KB Output is correct
45 Correct 256 ms 51308 KB Output is correct
46 Correct 16 ms 24552 KB Output is correct
47 Correct 17 ms 24532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 184 ms 46852 KB Output is correct
2 Correct 245 ms 55732 KB Output is correct
3 Correct 18 ms 24692 KB Output is correct
4 Correct 15 ms 24528 KB Output is correct
5 Correct 12 ms 23892 KB Output is correct
6 Correct 12 ms 23912 KB Output is correct
7 Correct 15 ms 24540 KB Output is correct
8 Correct 259 ms 49708 KB Output is correct
9 Correct 16 ms 24532 KB Output is correct
10 Correct 261 ms 47004 KB Output is correct
11 Correct 12 ms 23892 KB Output is correct
12 Correct 240 ms 47044 KB Output is correct
13 Correct 245 ms 49792 KB Output is correct
14 Correct 276 ms 55216 KB Output is correct
15 Correct 184 ms 46768 KB Output is correct
16 Correct 16 ms 24660 KB Output is correct
17 Correct 13 ms 23864 KB Output is correct
18 Correct 230 ms 54520 KB Output is correct
19 Correct 283 ms 58964 KB Output is correct
20 Correct 18 ms 24660 KB Output is correct
21 Correct 12 ms 23920 KB Output is correct
22 Correct 211 ms 50044 KB Output is correct
23 Correct 16 ms 24564 KB Output is correct
24 Correct 244 ms 47804 KB Output is correct
25 Correct 292 ms 57652 KB Output is correct
26 Correct 16 ms 24804 KB Output is correct
27 Correct 17 ms 24856 KB Output is correct
28 Correct 17 ms 24600 KB Output is correct
29 Correct 16 ms 24532 KB Output is correct
30 Correct 16 ms 24596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23892 KB Output is correct
2 Correct 14 ms 23920 KB Output is correct
3 Correct 12 ms 23852 KB Output is correct
4 Correct 11 ms 23892 KB Output is correct
5 Correct 13 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 12 ms 23892 KB Output is correct
8 Correct 13 ms 23872 KB Output is correct
9 Correct 12 ms 23892 KB Output is correct
10 Correct 12 ms 23892 KB Output is correct
11 Correct 13 ms 23892 KB Output is correct
12 Correct 12 ms 23892 KB Output is correct
13 Correct 12 ms 23880 KB Output is correct
14 Correct 12 ms 23880 KB Output is correct
15 Correct 12 ms 23916 KB Output is correct
16 Correct 12 ms 23792 KB Output is correct
17 Correct 12 ms 23812 KB Output is correct
18 Correct 12 ms 23908 KB Output is correct
19 Correct 12 ms 23892 KB Output is correct
20 Correct 13 ms 23844 KB Output is correct
21 Correct 12 ms 23892 KB Output is correct
22 Correct 11 ms 23924 KB Output is correct
23 Correct 12 ms 23892 KB Output is correct
24 Correct 13 ms 23764 KB Output is correct
25 Correct 12 ms 23892 KB Output is correct
26 Correct 17 ms 24668 KB Output is correct
27 Correct 16 ms 24540 KB Output is correct
28 Correct 17 ms 24824 KB Output is correct
29 Correct 19 ms 24916 KB Output is correct
30 Correct 22 ms 24516 KB Output is correct
31 Correct 12 ms 23892 KB Output is correct
32 Correct 18 ms 24928 KB Output is correct
33 Correct 12 ms 23892 KB Output is correct
34 Correct 16 ms 24608 KB Output is correct
35 Correct 17 ms 24788 KB Output is correct
36 Correct 17 ms 24528 KB Output is correct
37 Correct 17 ms 24636 KB Output is correct
38 Correct 12 ms 23892 KB Output is correct
39 Correct 17 ms 24660 KB Output is correct
40 Correct 16 ms 24532 KB Output is correct
41 Correct 16 ms 24544 KB Output is correct
42 Correct 16 ms 24576 KB Output is correct
43 Correct 16 ms 24840 KB Output is correct
44 Correct 12 ms 23892 KB Output is correct
45 Correct 16 ms 24752 KB Output is correct
46 Correct 17 ms 24660 KB Output is correct
47 Correct 12 ms 23892 KB Output is correct
48 Correct 16 ms 24532 KB Output is correct
49 Correct 16 ms 24808 KB Output is correct
50 Correct 18 ms 24788 KB Output is correct
51 Correct 17 ms 24532 KB Output is correct
52 Correct 15 ms 24532 KB Output is correct
53 Correct 16 ms 24672 KB Output is correct
54 Correct 17 ms 24532 KB Output is correct
55 Correct 13 ms 23904 KB Output is correct
56 Correct 197 ms 46876 KB Output is correct
57 Correct 333 ms 46744 KB Output is correct
58 Correct 16 ms 24532 KB Output is correct
59 Correct 15 ms 23812 KB Output is correct
60 Correct 12 ms 23828 KB Output is correct
61 Correct 293 ms 46720 KB Output is correct
62 Correct 16 ms 24532 KB Output is correct
63 Correct 249 ms 55464 KB Output is correct
64 Correct 390 ms 46896 KB Output is correct
65 Correct 20 ms 24524 KB Output is correct
66 Correct 255 ms 47580 KB Output is correct
67 Correct 17 ms 24532 KB Output is correct
68 Correct 19 ms 24516 KB Output is correct
69 Correct 206 ms 46836 KB Output is correct
70 Correct 16 ms 24832 KB Output is correct
71 Correct 359 ms 46772 KB Output is correct
72 Correct 268 ms 50616 KB Output is correct
73 Correct 12 ms 23892 KB Output is correct
74 Correct 258 ms 55740 KB Output is correct
75 Correct 256 ms 51308 KB Output is correct
76 Correct 16 ms 24552 KB Output is correct
77 Correct 17 ms 24532 KB Output is correct
78 Correct 184 ms 46852 KB Output is correct
79 Correct 245 ms 55732 KB Output is correct
80 Correct 18 ms 24692 KB Output is correct
81 Correct 15 ms 24528 KB Output is correct
82 Correct 12 ms 23892 KB Output is correct
83 Correct 12 ms 23912 KB Output is correct
84 Correct 15 ms 24540 KB Output is correct
85 Correct 259 ms 49708 KB Output is correct
86 Correct 16 ms 24532 KB Output is correct
87 Correct 261 ms 47004 KB Output is correct
88 Correct 12 ms 23892 KB Output is correct
89 Correct 240 ms 47044 KB Output is correct
90 Correct 245 ms 49792 KB Output is correct
91 Correct 276 ms 55216 KB Output is correct
92 Correct 184 ms 46768 KB Output is correct
93 Correct 16 ms 24660 KB Output is correct
94 Correct 13 ms 23864 KB Output is correct
95 Correct 230 ms 54520 KB Output is correct
96 Correct 283 ms 58964 KB Output is correct
97 Correct 18 ms 24660 KB Output is correct
98 Correct 12 ms 23920 KB Output is correct
99 Correct 211 ms 50044 KB Output is correct
100 Correct 16 ms 24564 KB Output is correct
101 Correct 244 ms 47804 KB Output is correct
102 Correct 292 ms 57652 KB Output is correct
103 Correct 16 ms 24804 KB Output is correct
104 Correct 17 ms 24856 KB Output is correct
105 Correct 17 ms 24600 KB Output is correct
106 Correct 16 ms 24532 KB Output is correct
107 Correct 16 ms 24596 KB Output is correct
108 Correct 1477 ms 148680 KB Output is correct
109 Correct 1853 ms 157972 KB Output is correct
110 Correct 1707 ms 171352 KB Output is correct
111 Correct 2092 ms 200196 KB Output is correct
112 Correct 2116 ms 187084 KB Output is correct
113 Correct 1669 ms 175292 KB Output is correct
114 Correct 2392 ms 136124 KB Output is correct
115 Correct 2330 ms 136016 KB Output is correct
116 Correct 2287 ms 142584 KB Output is correct
117 Correct 1971 ms 178832 KB Output is correct
118 Correct 2089 ms 135968 KB Output is correct
119 Correct 2166 ms 178748 KB Output is correct
120 Correct 2591 ms 192232 KB Output is correct
121 Correct 2256 ms 179016 KB Output is correct
122 Correct 2288 ms 154636 KB Output is correct
123 Correct 1824 ms 181776 KB Output is correct
124 Correct 2017 ms 170848 KB Output is correct