Submission #428490

# Submission time Handle Problem Language Result Execution time Memory
428490 2021-06-15T12:16:36 Z tengiz05 Capital City (JOI20_capital_city) C++17
100 / 100
609 ms 115056 KB
#include <bits/stdc++.h>
constexpr int N = 2e5;
std::vector<int> e[N], edges[N], rev[N];
int c[N], up[N][20], in[N], out[N], timeStamp, lc[N], mx[N][20], mn[N][20];
void dfs(int u, int p) {
    in[u] = timeStamp++;
    up[u][0] = p;
    mx[u][0] = mn[u][0] = c[u];
    for (int i = 1; i < 20; i++) {
        up[u][i] = up[up[u][i - 1]][i - 1];
        mn[u][i] = std::min(mn[u][i - 1], mn[up[u][i - 1]][i - 1]);
        mx[u][i] = std::max(mx[u][i - 1], mx[up[u][i - 1]][i - 1]);
    }
    for (auto v : e[u]) {
        if (v != p) {
            dfs(v, u);
        }
    }
    out[u] = timeStamp;
}
inline bool is(int u, int v) {
    return in[u] <= in[v] && out[u] >= out[v];
}
int lca(int u, int v) {
    if (is(u, v)) return u;
    if (is(v, u)) return v;
    for (int i = 19; i >= 0; i--) {
        if (!is(up[u][i], v)) {
            u = up[u][i];
        }
    }
    return up[u][0];
}
std::vector<bool> vis;
std::vector<int> order, comp[N];
std::set<int> ne[N];
void dfs2(int u) {
    vis[u] = true;
    for (auto v : edges[u]) {
        if (!vis[v]) {
            dfs2(v);
        }
    }
    order.push_back(u);
}
int belong[N], timer = 0, siz[N];
void dfs3(int u) {
    comp[timer].push_back(u);
    belong[u] = timer;
    siz[timer]++;
    vis[u] = true;
    for (auto v : rev[u]) {
        if (!vis[v]) {
            dfs3(v);
        }
    }
}
int dp[N];
int calc(int u) {
    if (~dp[u]) {
        return dp[u];
    }
    dp[u] = siz[u];
    for (auto v : ne[u]) {
        dp[u] += calc(v);
    }
    return dp[u];
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int n, k;
    std::cin >> n >> k;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    std::vector<int> comlca(k);
    for (int i = 0; i < n; i++) {
        std::cin >> c[i];
        c[i]--;
        comlca[c[i]] = i;
    }
    dfs(0, 0);
    for (int i = 0; i < n; i++) {
        comlca[c[i]] = lca(comlca[c[i]], i);
    }
    for (int i = 1; i < n; i++) {
        int u = i;
        for (int j = 18; j >= 0; j--) {
            if (mx[u][j + 1] == mn[u][j + 1] && (!is(up[u][j], comlca[c[i]]))) {
                u = up[u][j];
            }
        }
        u = up[u][0];
        if (c[u] != c[i] && (!is(u, comlca[c[i]]) || u == comlca[c[i]])) {
            // std::cout << c[i] + 1 << " " << c[u] + 1 << "\n";
            edges[c[i]].push_back(c[u]);
            rev[c[u]].push_back(c[i]);
        }
    }
    vis.assign(k, 0);
    for (int i = 0; i < k; i++) {
        if (!vis[i]) {
            dfs2(i);
        }
    }
    memset(belong, -1, sizeof belong);
    vis.assign(k, 0);
    reverse(order.begin(), order.end());
    for (auto u : order) {
        if (vis[u]) {
            continue;
        }
        dfs3(u);
        // for (auto x : comp[timer]) {
        //     std::cout << x + 1 << " ";
        // }std::cout << "\n\n";
        timer++;
    }
    for (int i = 0; i < k; i++) {
        for (auto v : edges[i]) {
            if (belong[i] != belong[v]) {
                ne[belong[i]].insert(belong[v]);
                // std::cout << belong[i] << " " << belong[v] << "\n";
            }
        }
    }
    memset(dp, -1, sizeof dp);
    int ans = 1e9;
    for (int i = 0; i < timer; i++) {
        ans = std::min(ans, calc(i));
    }
    std::cout << ans - 1 << "\n";
    return 0;
}

//   10 9 8 5 6 7 1 2 3 4 5 6 7 1 2 3 4 8 9 10
# Verdict Execution time Memory Grader output
1 Correct 18 ms 30028 KB Output is correct
2 Correct 18 ms 30040 KB Output is correct
3 Correct 17 ms 30032 KB Output is correct
4 Correct 20 ms 30028 KB Output is correct
5 Correct 18 ms 30028 KB Output is correct
6 Correct 18 ms 30028 KB Output is correct
7 Correct 16 ms 30028 KB Output is correct
8 Correct 18 ms 30028 KB Output is correct
9 Correct 18 ms 30048 KB Output is correct
10 Correct 18 ms 30108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 30028 KB Output is correct
2 Correct 18 ms 30040 KB Output is correct
3 Correct 17 ms 30032 KB Output is correct
4 Correct 20 ms 30028 KB Output is correct
5 Correct 18 ms 30028 KB Output is correct
6 Correct 18 ms 30028 KB Output is correct
7 Correct 16 ms 30028 KB Output is correct
8 Correct 18 ms 30028 KB Output is correct
9 Correct 18 ms 30048 KB Output is correct
10 Correct 18 ms 30108 KB Output is correct
11 Correct 21 ms 30660 KB Output is correct
12 Correct 20 ms 30736 KB Output is correct
13 Correct 22 ms 30684 KB Output is correct
14 Correct 20 ms 30740 KB Output is correct
15 Correct 21 ms 30796 KB Output is correct
16 Correct 21 ms 30796 KB Output is correct
17 Correct 20 ms 30696 KB Output is correct
18 Correct 20 ms 30820 KB Output is correct
19 Correct 21 ms 30684 KB Output is correct
20 Correct 20 ms 30720 KB Output is correct
21 Correct 19 ms 30808 KB Output is correct
22 Correct 23 ms 30952 KB Output is correct
23 Correct 20 ms 30868 KB Output is correct
24 Correct 22 ms 31000 KB Output is correct
25 Correct 19 ms 30804 KB Output is correct
26 Correct 19 ms 30796 KB Output is correct
27 Correct 20 ms 30788 KB Output is correct
28 Correct 19 ms 30796 KB Output is correct
29 Correct 20 ms 30820 KB Output is correct
30 Correct 20 ms 30812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 569 ms 114640 KB Output is correct
2 Correct 257 ms 115052 KB Output is correct
3 Correct 548 ms 114544 KB Output is correct
4 Correct 250 ms 115044 KB Output is correct
5 Correct 575 ms 110920 KB Output is correct
6 Correct 251 ms 114788 KB Output is correct
7 Correct 609 ms 110412 KB Output is correct
8 Correct 265 ms 112484 KB Output is correct
9 Correct 469 ms 101220 KB Output is correct
10 Correct 473 ms 99248 KB Output is correct
11 Correct 461 ms 101476 KB Output is correct
12 Correct 497 ms 103792 KB Output is correct
13 Correct 487 ms 98888 KB Output is correct
14 Correct 527 ms 103900 KB Output is correct
15 Correct 496 ms 103692 KB Output is correct
16 Correct 504 ms 99648 KB Output is correct
17 Correct 448 ms 100108 KB Output is correct
18 Correct 480 ms 100296 KB Output is correct
19 Correct 482 ms 102776 KB Output is correct
20 Correct 529 ms 104560 KB Output is correct
21 Correct 19 ms 30040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 30028 KB Output is correct
2 Correct 18 ms 30040 KB Output is correct
3 Correct 17 ms 30032 KB Output is correct
4 Correct 20 ms 30028 KB Output is correct
5 Correct 18 ms 30028 KB Output is correct
6 Correct 18 ms 30028 KB Output is correct
7 Correct 16 ms 30028 KB Output is correct
8 Correct 18 ms 30028 KB Output is correct
9 Correct 18 ms 30048 KB Output is correct
10 Correct 18 ms 30108 KB Output is correct
11 Correct 21 ms 30660 KB Output is correct
12 Correct 20 ms 30736 KB Output is correct
13 Correct 22 ms 30684 KB Output is correct
14 Correct 20 ms 30740 KB Output is correct
15 Correct 21 ms 30796 KB Output is correct
16 Correct 21 ms 30796 KB Output is correct
17 Correct 20 ms 30696 KB Output is correct
18 Correct 20 ms 30820 KB Output is correct
19 Correct 21 ms 30684 KB Output is correct
20 Correct 20 ms 30720 KB Output is correct
21 Correct 19 ms 30808 KB Output is correct
22 Correct 23 ms 30952 KB Output is correct
23 Correct 20 ms 30868 KB Output is correct
24 Correct 22 ms 31000 KB Output is correct
25 Correct 19 ms 30804 KB Output is correct
26 Correct 19 ms 30796 KB Output is correct
27 Correct 20 ms 30788 KB Output is correct
28 Correct 19 ms 30796 KB Output is correct
29 Correct 20 ms 30820 KB Output is correct
30 Correct 20 ms 30812 KB Output is correct
31 Correct 569 ms 114640 KB Output is correct
32 Correct 257 ms 115052 KB Output is correct
33 Correct 548 ms 114544 KB Output is correct
34 Correct 250 ms 115044 KB Output is correct
35 Correct 575 ms 110920 KB Output is correct
36 Correct 251 ms 114788 KB Output is correct
37 Correct 609 ms 110412 KB Output is correct
38 Correct 265 ms 112484 KB Output is correct
39 Correct 469 ms 101220 KB Output is correct
40 Correct 473 ms 99248 KB Output is correct
41 Correct 461 ms 101476 KB Output is correct
42 Correct 497 ms 103792 KB Output is correct
43 Correct 487 ms 98888 KB Output is correct
44 Correct 527 ms 103900 KB Output is correct
45 Correct 496 ms 103692 KB Output is correct
46 Correct 504 ms 99648 KB Output is correct
47 Correct 448 ms 100108 KB Output is correct
48 Correct 480 ms 100296 KB Output is correct
49 Correct 482 ms 102776 KB Output is correct
50 Correct 529 ms 104560 KB Output is correct
51 Correct 19 ms 30040 KB Output is correct
52 Correct 346 ms 91616 KB Output is correct
53 Correct 356 ms 91764 KB Output is correct
54 Correct 344 ms 91576 KB Output is correct
55 Correct 355 ms 91848 KB Output is correct
56 Correct 320 ms 91648 KB Output is correct
57 Correct 345 ms 91568 KB Output is correct
58 Correct 481 ms 99788 KB Output is correct
59 Correct 503 ms 100392 KB Output is correct
60 Correct 519 ms 102588 KB Output is correct
61 Correct 496 ms 102384 KB Output is correct
62 Correct 294 ms 114996 KB Output is correct
63 Correct 259 ms 115056 KB Output is correct
64 Correct 266 ms 113044 KB Output is correct
65 Correct 292 ms 114880 KB Output is correct
66 Correct 571 ms 100416 KB Output is correct
67 Correct 502 ms 100308 KB Output is correct
68 Correct 598 ms 100476 KB Output is correct
69 Correct 571 ms 100456 KB Output is correct
70 Correct 545 ms 100436 KB Output is correct
71 Correct 594 ms 100320 KB Output is correct
72 Correct 546 ms 100412 KB Output is correct
73 Correct 516 ms 99640 KB Output is correct
74 Correct 559 ms 100428 KB Output is correct
75 Correct 599 ms 100648 KB Output is correct
76 Correct 548 ms 107144 KB Output is correct
77 Correct 544 ms 105524 KB Output is correct
78 Correct 525 ms 100252 KB Output is correct
79 Correct 494 ms 98496 KB Output is correct
80 Correct 531 ms 104104 KB Output is correct
81 Correct 478 ms 101376 KB Output is correct
82 Correct 457 ms 101440 KB Output is correct
83 Correct 475 ms 98844 KB Output is correct
84 Correct 503 ms 103492 KB Output is correct
85 Correct 503 ms 102212 KB Output is correct
86 Correct 476 ms 98500 KB Output is correct
87 Correct 445 ms 99904 KB Output is correct
88 Correct 494 ms 102224 KB Output is correct
89 Correct 519 ms 98764 KB Output is correct
90 Correct 449 ms 98568 KB Output is correct
91 Correct 512 ms 100708 KB Output is correct
92 Correct 459 ms 99444 KB Output is correct
93 Correct 488 ms 99264 KB Output is correct
94 Correct 466 ms 98832 KB Output is correct
95 Correct 482 ms 99860 KB Output is correct
96 Correct 474 ms 98976 KB Output is correct
97 Correct 491 ms 100456 KB Output is correct