답안 #428493

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
428493 2021-06-15T12:18:49 Z tengiz05 수도 (JOI20_capital_city) C++17
100 / 100
612 ms 105580 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, 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;
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) {
    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]])) {
            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);
        timer++;
    }
    for (int i = 0; i < k; i++) {
        for (auto v : edges[i]) {
            if (belong[i] != belong[v]) {
                ne[belong[i]].insert(belong[v]);
            }
        }
    }
    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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 25292 KB Output is correct
2 Correct 16 ms 25292 KB Output is correct
3 Correct 14 ms 25292 KB Output is correct
4 Correct 18 ms 25292 KB Output is correct
5 Correct 14 ms 25408 KB Output is correct
6 Correct 15 ms 25292 KB Output is correct
7 Correct 16 ms 25340 KB Output is correct
8 Correct 16 ms 25388 KB Output is correct
9 Correct 15 ms 25292 KB Output is correct
10 Correct 15 ms 25292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 25292 KB Output is correct
2 Correct 16 ms 25292 KB Output is correct
3 Correct 14 ms 25292 KB Output is correct
4 Correct 18 ms 25292 KB Output is correct
5 Correct 14 ms 25408 KB Output is correct
6 Correct 15 ms 25292 KB Output is correct
7 Correct 16 ms 25340 KB Output is correct
8 Correct 16 ms 25388 KB Output is correct
9 Correct 15 ms 25292 KB Output is correct
10 Correct 15 ms 25292 KB Output is correct
11 Correct 18 ms 25936 KB Output is correct
12 Correct 20 ms 25960 KB Output is correct
13 Correct 21 ms 26000 KB Output is correct
14 Correct 19 ms 26012 KB Output is correct
15 Correct 21 ms 26060 KB Output is correct
16 Correct 22 ms 26060 KB Output is correct
17 Correct 19 ms 26060 KB Output is correct
18 Correct 18 ms 26028 KB Output is correct
19 Correct 20 ms 26068 KB Output is correct
20 Correct 16 ms 26084 KB Output is correct
21 Correct 21 ms 26060 KB Output is correct
22 Correct 16 ms 26188 KB Output is correct
23 Correct 18 ms 26120 KB Output is correct
24 Correct 19 ms 26124 KB Output is correct
25 Correct 17 ms 25996 KB Output is correct
26 Correct 17 ms 26060 KB Output is correct
27 Correct 16 ms 26060 KB Output is correct
28 Correct 16 ms 25992 KB Output is correct
29 Correct 17 ms 26060 KB Output is correct
30 Correct 17 ms 26088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 574 ms 105176 KB Output is correct
2 Correct 254 ms 105580 KB Output is correct
3 Correct 564 ms 104976 KB Output is correct
4 Correct 261 ms 105568 KB Output is correct
5 Correct 588 ms 101764 KB Output is correct
6 Correct 262 ms 105364 KB Output is correct
7 Correct 580 ms 101488 KB Output is correct
8 Correct 298 ms 103344 KB Output is correct
9 Correct 450 ms 94480 KB Output is correct
10 Correct 469 ms 92460 KB Output is correct
11 Correct 464 ms 94840 KB Output is correct
12 Correct 522 ms 96904 KB Output is correct
13 Correct 469 ms 92264 KB Output is correct
14 Correct 524 ms 97152 KB Output is correct
15 Correct 465 ms 96988 KB Output is correct
16 Correct 469 ms 92908 KB Output is correct
17 Correct 475 ms 93628 KB Output is correct
18 Correct 468 ms 93612 KB Output is correct
19 Correct 472 ms 96164 KB Output is correct
20 Correct 495 ms 97880 KB Output is correct
21 Correct 16 ms 25292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 25292 KB Output is correct
2 Correct 16 ms 25292 KB Output is correct
3 Correct 14 ms 25292 KB Output is correct
4 Correct 18 ms 25292 KB Output is correct
5 Correct 14 ms 25408 KB Output is correct
6 Correct 15 ms 25292 KB Output is correct
7 Correct 16 ms 25340 KB Output is correct
8 Correct 16 ms 25388 KB Output is correct
9 Correct 15 ms 25292 KB Output is correct
10 Correct 15 ms 25292 KB Output is correct
11 Correct 18 ms 25936 KB Output is correct
12 Correct 20 ms 25960 KB Output is correct
13 Correct 21 ms 26000 KB Output is correct
14 Correct 19 ms 26012 KB Output is correct
15 Correct 21 ms 26060 KB Output is correct
16 Correct 22 ms 26060 KB Output is correct
17 Correct 19 ms 26060 KB Output is correct
18 Correct 18 ms 26028 KB Output is correct
19 Correct 20 ms 26068 KB Output is correct
20 Correct 16 ms 26084 KB Output is correct
21 Correct 21 ms 26060 KB Output is correct
22 Correct 16 ms 26188 KB Output is correct
23 Correct 18 ms 26120 KB Output is correct
24 Correct 19 ms 26124 KB Output is correct
25 Correct 17 ms 25996 KB Output is correct
26 Correct 17 ms 26060 KB Output is correct
27 Correct 16 ms 26060 KB Output is correct
28 Correct 16 ms 25992 KB Output is correct
29 Correct 17 ms 26060 KB Output is correct
30 Correct 17 ms 26088 KB Output is correct
31 Correct 574 ms 105176 KB Output is correct
32 Correct 254 ms 105580 KB Output is correct
33 Correct 564 ms 104976 KB Output is correct
34 Correct 261 ms 105568 KB Output is correct
35 Correct 588 ms 101764 KB Output is correct
36 Correct 262 ms 105364 KB Output is correct
37 Correct 580 ms 101488 KB Output is correct
38 Correct 298 ms 103344 KB Output is correct
39 Correct 450 ms 94480 KB Output is correct
40 Correct 469 ms 92460 KB Output is correct
41 Correct 464 ms 94840 KB Output is correct
42 Correct 522 ms 96904 KB Output is correct
43 Correct 469 ms 92264 KB Output is correct
44 Correct 524 ms 97152 KB Output is correct
45 Correct 465 ms 96988 KB Output is correct
46 Correct 469 ms 92908 KB Output is correct
47 Correct 475 ms 93628 KB Output is correct
48 Correct 468 ms 93612 KB Output is correct
49 Correct 472 ms 96164 KB Output is correct
50 Correct 495 ms 97880 KB Output is correct
51 Correct 16 ms 25292 KB Output is correct
52 Correct 363 ms 85152 KB Output is correct
53 Correct 368 ms 85200 KB Output is correct
54 Correct 346 ms 85156 KB Output is correct
55 Correct 372 ms 85176 KB Output is correct
56 Correct 421 ms 85140 KB Output is correct
57 Correct 368 ms 85116 KB Output is correct
58 Correct 505 ms 91456 KB Output is correct
59 Correct 504 ms 91992 KB Output is correct
60 Correct 575 ms 93872 KB Output is correct
61 Correct 505 ms 93628 KB Output is correct
62 Correct 255 ms 105528 KB Output is correct
63 Correct 288 ms 105568 KB Output is correct
64 Correct 288 ms 103948 KB Output is correct
65 Correct 256 ms 105364 KB Output is correct
66 Correct 579 ms 92436 KB Output is correct
67 Correct 566 ms 92428 KB Output is correct
68 Correct 590 ms 92412 KB Output is correct
69 Correct 549 ms 92392 KB Output is correct
70 Correct 579 ms 92500 KB Output is correct
71 Correct 585 ms 92392 KB Output is correct
72 Correct 576 ms 92472 KB Output is correct
73 Correct 480 ms 91780 KB Output is correct
74 Correct 569 ms 92380 KB Output is correct
75 Correct 612 ms 92540 KB Output is correct
76 Correct 566 ms 97520 KB Output is correct
77 Correct 580 ms 96024 KB Output is correct
78 Correct 447 ms 93404 KB Output is correct
79 Correct 457 ms 91800 KB Output is correct
80 Correct 473 ms 97376 KB Output is correct
81 Correct 475 ms 94692 KB Output is correct
82 Correct 475 ms 94756 KB Output is correct
83 Correct 488 ms 92016 KB Output is correct
84 Correct 489 ms 96836 KB Output is correct
85 Correct 482 ms 95484 KB Output is correct
86 Correct 487 ms 91880 KB Output is correct
87 Correct 500 ms 93180 KB Output is correct
88 Correct 527 ms 94788 KB Output is correct
89 Correct 505 ms 91448 KB Output is correct
90 Correct 468 ms 91372 KB Output is correct
91 Correct 481 ms 93168 KB Output is correct
92 Correct 488 ms 92152 KB Output is correct
93 Correct 465 ms 91976 KB Output is correct
94 Correct 489 ms 91496 KB Output is correct
95 Correct 480 ms 92656 KB Output is correct
96 Correct 517 ms 91692 KB Output is correct
97 Correct 465 ms 93248 KB Output is correct