Submission #956087

# Submission time Handle Problem Language Result Execution time Memory
956087 2024-04-01T03:14:33 Z Pring Mergers (JOI19_mergers) C++17
100 / 100
749 ms 135352 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif

#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;

const int MXN = 500005;
int n, m, clr[MXN];
vector<int> sclr[MXN];

struct BIT {
    int n, val[MXN];
    void init(int _n) {
        n = _n;
        fill(val + 1, val + n + 1, 0);
    }
    void modify(int id, int v) {
        for (; id <= n; id += (id & -id)) val[id] += v;
    }
    void modify(int l, int r, int v) {
        modify(l, v);
        modify(r, -v);
    }
    int query(int id) {
        int ans = 0;
        for (; id > 0; id -= (id & -id)) ans += val[id];
        return ans;
    }
} B;

struct DSU {
    int p[MXN];
    void init(int n) {
        fill(p + 1, p + n + 1, -1);
    }
    int find(int x) {
        return (p[x] < 0 ? x : p[x] = find(p[x]));
    }
    bool onion(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return false;
        if (p[x] > p[y]) swap(x, y);
        p[x] += p[y];
        p[y] = x;
        return true;
    }
} dsu;

namespace TREE {
    vector<int> edge[MXN];
    int p[MXN], d[MXN], sz[MXN], son[MXN], top[MXN], dfn[MXN], C;
    int idfn[MXN];
    int deg[MXN];

    void PUSH_EDGE(int x, int y) {
        edge[x].push_back(y);
        edge[y].push_back(x);
    }

    void DFS(int id, int par, int dep) {
        p[id] = par;
        d[id] = dep;
        sz[id] = 1;
        son[id] = -1;
        for (auto &i : edge[id]) {
            if (i == par) continue;
            DFS(i, id, dep + 1);
            sz[id] += sz[i];
            if (son[id] == -1) son[id] = i;
            else if (sz[son[id]] < sz[i]) son[id] = i;
        }
    }

    void HLD(int id, int par, int _t) {
        top[id] = _t;
        dfn[id] = C++;
        if (son[id] != -1) HLD(son[id], id, _t);
        for (auto &i : edge[id]) {
            if (i == par) continue;
            if (i == son[id]) continue;
            HLD(i, id, i);
        }
    }

    void BUILD() {
        DFS(1, 0, 0);
        C = 1;
        HLD(1, 0, 1);
        FOR(i, 1, n) idfn[dfn[i]] = i;
        B.init(n);
    }

    int LCA(int x, int y) {
        while (top[x] != top[y]) {
            if (d[top[x]] < d[top[y]]) swap(x, y);
            B.modify(dfn[top[x]], dfn[x] + 1, 1);
            x = p[top[x]];
        }
        if (d[x] < d[y]) swap(x, y);
        B.modify(dfn[y] + 1, dfn[x] + 1, 1);
        return y;
    }

    void COLOR(vector<int> &v) {
        int lca = v[0];
        FOR(i, 1, v.size()) {
            lca = LCA(lca, v[i]);
        }
    }

    void MERGE() {
        vector<int> v;
        dsu.init(n);
        FOR(i, 2, n + 1) {
            if (B.query(dfn[i])) {
                debug(i, p[i], 1);
                dsu.onion(i, p[i]);
            } else {
                v.push_back(i);
            }
        }
        for (auto &i : v) {
            debug(i, p[i], 0);
            deg[dsu.find(i)]++;
            deg[dsu.find(p[i])]++;
        }
    }

    int LEAF() {
        int ans = 0;
        FOR(i, 1, n + 1) {
            if (dsu.p[i] >= 0) continue;
            if (deg[i] <= 1) ans++;
        }
        return ans;
    }
}

int miku() {
    int x, y;
    cin >> n >> m;
    FOR(i, 1, n) {
        cin >> x >> y;
        TREE::PUSH_EDGE(x, y);
    }
    TREE::BUILD();
    FOR(i, 1, n + 1) {
        cin >> clr[i];
        sclr[clr[i]].push_back(i);
    }
    FOR(i, 1, m + 1) TREE::COLOR(sclr[i]);
    TREE::MERGE();
    x = TREE::LEAF();
    if (x == 1) return 0;
    return (x - 1) / 2 + 1;
}

int32_t main() {
    cin.tie(0) -> sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    cout << miku() << '\n';
    return 0;
}

Compilation message

mergers.cpp: In function 'void TREE::MERGE()':
mergers.cpp:11:20: warning: statement has no effect [-Wunused-value]
   11 | #define debug(...) 39
      |                    ^~
mergers.cpp:131:17: note: in expansion of macro 'debug'
  131 |                 debug(i, p[i], 1);
      |                 ^~~~~
mergers.cpp:11:20: warning: statement has no effect [-Wunused-value]
   11 | #define debug(...) 39
      |                    ^~
mergers.cpp:138:13: note: in expansion of macro 'debug'
  138 |             debug(i, p[i], 0);
      |             ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 43352 KB Output is correct
2 Correct 8 ms 43352 KB Output is correct
3 Correct 8 ms 43356 KB Output is correct
4 Correct 8 ms 43352 KB Output is correct
5 Correct 9 ms 43352 KB Output is correct
6 Correct 8 ms 43356 KB Output is correct
7 Correct 8 ms 41308 KB Output is correct
8 Correct 9 ms 43604 KB Output is correct
9 Correct 8 ms 43552 KB Output is correct
10 Correct 8 ms 43356 KB Output is correct
11 Correct 9 ms 43356 KB Output is correct
12 Correct 8 ms 43564 KB Output is correct
13 Correct 8 ms 43556 KB Output is correct
14 Correct 8 ms 43356 KB Output is correct
15 Correct 8 ms 43464 KB Output is correct
16 Correct 8 ms 43352 KB Output is correct
17 Correct 8 ms 43356 KB Output is correct
18 Correct 8 ms 43356 KB Output is correct
19 Correct 9 ms 43544 KB Output is correct
20 Correct 8 ms 43356 KB Output is correct
21 Correct 8 ms 43356 KB Output is correct
22 Correct 8 ms 43560 KB Output is correct
23 Correct 8 ms 43520 KB Output is correct
24 Correct 8 ms 43356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 43352 KB Output is correct
2 Correct 8 ms 43352 KB Output is correct
3 Correct 8 ms 43356 KB Output is correct
4 Correct 8 ms 43352 KB Output is correct
5 Correct 9 ms 43352 KB Output is correct
6 Correct 8 ms 43356 KB Output is correct
7 Correct 8 ms 41308 KB Output is correct
8 Correct 9 ms 43604 KB Output is correct
9 Correct 8 ms 43552 KB Output is correct
10 Correct 8 ms 43356 KB Output is correct
11 Correct 9 ms 43356 KB Output is correct
12 Correct 8 ms 43564 KB Output is correct
13 Correct 8 ms 43556 KB Output is correct
14 Correct 8 ms 43356 KB Output is correct
15 Correct 8 ms 43464 KB Output is correct
16 Correct 8 ms 43352 KB Output is correct
17 Correct 8 ms 43356 KB Output is correct
18 Correct 8 ms 43356 KB Output is correct
19 Correct 9 ms 43544 KB Output is correct
20 Correct 8 ms 43356 KB Output is correct
21 Correct 8 ms 43356 KB Output is correct
22 Correct 8 ms 43560 KB Output is correct
23 Correct 8 ms 43520 KB Output is correct
24 Correct 8 ms 43356 KB Output is correct
25 Correct 8 ms 43356 KB Output is correct
26 Correct 9 ms 43864 KB Output is correct
27 Correct 9 ms 43692 KB Output is correct
28 Correct 9 ms 43864 KB Output is correct
29 Correct 9 ms 43648 KB Output is correct
30 Correct 10 ms 43612 KB Output is correct
31 Correct 9 ms 43520 KB Output is correct
32 Correct 11 ms 43988 KB Output is correct
33 Correct 8 ms 43356 KB Output is correct
34 Correct 9 ms 43612 KB Output is correct
35 Correct 10 ms 44068 KB Output is correct
36 Correct 10 ms 43864 KB Output is correct
37 Correct 10 ms 43676 KB Output is correct
38 Correct 8 ms 43352 KB Output is correct
39 Correct 10 ms 43612 KB Output is correct
40 Correct 10 ms 43612 KB Output is correct
41 Correct 9 ms 43612 KB Output is correct
42 Correct 10 ms 43612 KB Output is correct
43 Correct 9 ms 43868 KB Output is correct
44 Correct 8 ms 43356 KB Output is correct
45 Correct 10 ms 43736 KB Output is correct
46 Correct 10 ms 43608 KB Output is correct
47 Correct 8 ms 43356 KB Output is correct
48 Correct 10 ms 43612 KB Output is correct
49 Correct 9 ms 43868 KB Output is correct
50 Correct 10 ms 43864 KB Output is correct
51 Correct 9 ms 43608 KB Output is correct
52 Correct 9 ms 43608 KB Output is correct
53 Correct 9 ms 43612 KB Output is correct
54 Correct 10 ms 43704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 43352 KB Output is correct
2 Correct 8 ms 43352 KB Output is correct
3 Correct 8 ms 43356 KB Output is correct
4 Correct 8 ms 43352 KB Output is correct
5 Correct 9 ms 43352 KB Output is correct
6 Correct 8 ms 43356 KB Output is correct
7 Correct 8 ms 41308 KB Output is correct
8 Correct 9 ms 43604 KB Output is correct
9 Correct 8 ms 43552 KB Output is correct
10 Correct 8 ms 43356 KB Output is correct
11 Correct 9 ms 43356 KB Output is correct
12 Correct 8 ms 43564 KB Output is correct
13 Correct 8 ms 43556 KB Output is correct
14 Correct 8 ms 43356 KB Output is correct
15 Correct 8 ms 43464 KB Output is correct
16 Correct 8 ms 43352 KB Output is correct
17 Correct 8 ms 43356 KB Output is correct
18 Correct 8 ms 43356 KB Output is correct
19 Correct 9 ms 43544 KB Output is correct
20 Correct 8 ms 43356 KB Output is correct
21 Correct 8 ms 43356 KB Output is correct
22 Correct 8 ms 43560 KB Output is correct
23 Correct 8 ms 43520 KB Output is correct
24 Correct 8 ms 43356 KB Output is correct
25 Correct 8 ms 43356 KB Output is correct
26 Correct 54 ms 60136 KB Output is correct
27 Correct 90 ms 59988 KB Output is correct
28 Correct 10 ms 43608 KB Output is correct
29 Correct 9 ms 43356 KB Output is correct
30 Correct 8 ms 43608 KB Output is correct
31 Correct 71 ms 59908 KB Output is correct
32 Correct 9 ms 43612 KB Output is correct
33 Correct 67 ms 66612 KB Output is correct
34 Correct 77 ms 60052 KB Output is correct
35 Correct 9 ms 43608 KB Output is correct
36 Correct 62 ms 62288 KB Output is correct
37 Correct 9 ms 43612 KB Output is correct
38 Correct 10 ms 43612 KB Output is correct
39 Correct 52 ms 59996 KB Output is correct
40 Correct 9 ms 43864 KB Output is correct
41 Correct 73 ms 59768 KB Output is correct
42 Correct 66 ms 63788 KB Output is correct
43 Correct 9 ms 43560 KB Output is correct
44 Correct 64 ms 66756 KB Output is correct
45 Correct 73 ms 64336 KB Output is correct
46 Correct 11 ms 43612 KB Output is correct
47 Correct 11 ms 43612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 58832 KB Output is correct
2 Correct 51 ms 63984 KB Output is correct
3 Correct 10 ms 43612 KB Output is correct
4 Correct 9 ms 43608 KB Output is correct
5 Correct 8 ms 43356 KB Output is correct
6 Correct 9 ms 43488 KB Output is correct
7 Correct 9 ms 43612 KB Output is correct
8 Correct 81 ms 63436 KB Output is correct
9 Correct 9 ms 43612 KB Output is correct
10 Correct 66 ms 62288 KB Output is correct
11 Correct 9 ms 43356 KB Output is correct
12 Correct 59 ms 61716 KB Output is correct
13 Correct 70 ms 63292 KB Output is correct
14 Correct 72 ms 65516 KB Output is correct
15 Correct 50 ms 59972 KB Output is correct
16 Correct 9 ms 43740 KB Output is correct
17 Correct 8 ms 43352 KB Output is correct
18 Correct 55 ms 65028 KB Output is correct
19 Correct 73 ms 68848 KB Output is correct
20 Correct 10 ms 43612 KB Output is correct
21 Correct 8 ms 43528 KB Output is correct
22 Correct 55 ms 63720 KB Output is correct
23 Correct 9 ms 43784 KB Output is correct
24 Correct 71 ms 62668 KB Output is correct
25 Correct 81 ms 68216 KB Output is correct
26 Correct 9 ms 43868 KB Output is correct
27 Correct 10 ms 43948 KB Output is correct
28 Correct 10 ms 43744 KB Output is correct
29 Correct 10 ms 43612 KB Output is correct
30 Correct 9 ms 43788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 43352 KB Output is correct
2 Correct 8 ms 43352 KB Output is correct
3 Correct 8 ms 43356 KB Output is correct
4 Correct 8 ms 43352 KB Output is correct
5 Correct 9 ms 43352 KB Output is correct
6 Correct 8 ms 43356 KB Output is correct
7 Correct 8 ms 41308 KB Output is correct
8 Correct 9 ms 43604 KB Output is correct
9 Correct 8 ms 43552 KB Output is correct
10 Correct 8 ms 43356 KB Output is correct
11 Correct 9 ms 43356 KB Output is correct
12 Correct 8 ms 43564 KB Output is correct
13 Correct 8 ms 43556 KB Output is correct
14 Correct 8 ms 43356 KB Output is correct
15 Correct 8 ms 43464 KB Output is correct
16 Correct 8 ms 43352 KB Output is correct
17 Correct 8 ms 43356 KB Output is correct
18 Correct 8 ms 43356 KB Output is correct
19 Correct 9 ms 43544 KB Output is correct
20 Correct 8 ms 43356 KB Output is correct
21 Correct 8 ms 43356 KB Output is correct
22 Correct 8 ms 43560 KB Output is correct
23 Correct 8 ms 43520 KB Output is correct
24 Correct 8 ms 43356 KB Output is correct
25 Correct 8 ms 43356 KB Output is correct
26 Correct 9 ms 43864 KB Output is correct
27 Correct 9 ms 43692 KB Output is correct
28 Correct 9 ms 43864 KB Output is correct
29 Correct 9 ms 43648 KB Output is correct
30 Correct 10 ms 43612 KB Output is correct
31 Correct 9 ms 43520 KB Output is correct
32 Correct 11 ms 43988 KB Output is correct
33 Correct 8 ms 43356 KB Output is correct
34 Correct 9 ms 43612 KB Output is correct
35 Correct 10 ms 44068 KB Output is correct
36 Correct 10 ms 43864 KB Output is correct
37 Correct 10 ms 43676 KB Output is correct
38 Correct 8 ms 43352 KB Output is correct
39 Correct 10 ms 43612 KB Output is correct
40 Correct 10 ms 43612 KB Output is correct
41 Correct 9 ms 43612 KB Output is correct
42 Correct 10 ms 43612 KB Output is correct
43 Correct 9 ms 43868 KB Output is correct
44 Correct 8 ms 43356 KB Output is correct
45 Correct 10 ms 43736 KB Output is correct
46 Correct 10 ms 43608 KB Output is correct
47 Correct 8 ms 43356 KB Output is correct
48 Correct 10 ms 43612 KB Output is correct
49 Correct 9 ms 43868 KB Output is correct
50 Correct 10 ms 43864 KB Output is correct
51 Correct 9 ms 43608 KB Output is correct
52 Correct 9 ms 43608 KB Output is correct
53 Correct 9 ms 43612 KB Output is correct
54 Correct 10 ms 43704 KB Output is correct
55 Correct 8 ms 43356 KB Output is correct
56 Correct 54 ms 60136 KB Output is correct
57 Correct 90 ms 59988 KB Output is correct
58 Correct 10 ms 43608 KB Output is correct
59 Correct 9 ms 43356 KB Output is correct
60 Correct 8 ms 43608 KB Output is correct
61 Correct 71 ms 59908 KB Output is correct
62 Correct 9 ms 43612 KB Output is correct
63 Correct 67 ms 66612 KB Output is correct
64 Correct 77 ms 60052 KB Output is correct
65 Correct 9 ms 43608 KB Output is correct
66 Correct 62 ms 62288 KB Output is correct
67 Correct 9 ms 43612 KB Output is correct
68 Correct 10 ms 43612 KB Output is correct
69 Correct 52 ms 59996 KB Output is correct
70 Correct 9 ms 43864 KB Output is correct
71 Correct 73 ms 59768 KB Output is correct
72 Correct 66 ms 63788 KB Output is correct
73 Correct 9 ms 43560 KB Output is correct
74 Correct 64 ms 66756 KB Output is correct
75 Correct 73 ms 64336 KB Output is correct
76 Correct 11 ms 43612 KB Output is correct
77 Correct 11 ms 43612 KB Output is correct
78 Correct 52 ms 58832 KB Output is correct
79 Correct 51 ms 63984 KB Output is correct
80 Correct 10 ms 43612 KB Output is correct
81 Correct 9 ms 43608 KB Output is correct
82 Correct 8 ms 43356 KB Output is correct
83 Correct 9 ms 43488 KB Output is correct
84 Correct 9 ms 43612 KB Output is correct
85 Correct 81 ms 63436 KB Output is correct
86 Correct 9 ms 43612 KB Output is correct
87 Correct 66 ms 62288 KB Output is correct
88 Correct 9 ms 43356 KB Output is correct
89 Correct 59 ms 61716 KB Output is correct
90 Correct 70 ms 63292 KB Output is correct
91 Correct 72 ms 65516 KB Output is correct
92 Correct 50 ms 59972 KB Output is correct
93 Correct 9 ms 43740 KB Output is correct
94 Correct 8 ms 43352 KB Output is correct
95 Correct 55 ms 65028 KB Output is correct
96 Correct 73 ms 68848 KB Output is correct
97 Correct 10 ms 43612 KB Output is correct
98 Correct 8 ms 43528 KB Output is correct
99 Correct 55 ms 63720 KB Output is correct
100 Correct 9 ms 43784 KB Output is correct
101 Correct 71 ms 62668 KB Output is correct
102 Correct 81 ms 68216 KB Output is correct
103 Correct 9 ms 43868 KB Output is correct
104 Correct 10 ms 43948 KB Output is correct
105 Correct 10 ms 43744 KB Output is correct
106 Correct 10 ms 43612 KB Output is correct
107 Correct 9 ms 43788 KB Output is correct
108 Correct 399 ms 105964 KB Output is correct
109 Correct 494 ms 112424 KB Output is correct
110 Correct 538 ms 121840 KB Output is correct
111 Correct 533 ms 135352 KB Output is correct
112 Correct 558 ms 129112 KB Output is correct
113 Correct 339 ms 113800 KB Output is correct
114 Correct 572 ms 95132 KB Output is correct
115 Correct 683 ms 95196 KB Output is correct
116 Correct 695 ms 102664 KB Output is correct
117 Correct 475 ms 115264 KB Output is correct
118 Correct 547 ms 99664 KB Output is correct
119 Correct 541 ms 115396 KB Output is correct
120 Correct 624 ms 130652 KB Output is correct
121 Correct 525 ms 115136 KB Output is correct
122 Correct 749 ms 106372 KB Output is correct
123 Correct 331 ms 116920 KB Output is correct
124 Correct 557 ms 110536 KB Output is correct