Submission #391503

# Submission time Handle Problem Language Result Execution time Memory
391503 2021-04-19T04:21:14 Z timmyfeng Unique Cities (JOI19_ho_t5) C++17
100 / 100
554 ms 55612 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 200000;

int color[N], depth[N];
vector<int> adj[N];

void dfs_depth(int u, int p = -1) {
    for (auto c : adj[u]) {
        if (c != p) {
            depth[c] = depth[u] + 1;
            dfs_depth(c, u);
        }
    }
}

int height[N];

void dfs_height(int u, int p = -1) {
    height[u] = 0;
    for (auto c : adj[u]) {
        if (c != p) {
            dfs_height(c, u);
            height[u] = max(height[u], height[c] + 1);
        }
    }
}

int tally[N], ans[N], k;
vector<int> path;

void trim(int u, int d) {
    while (!path.empty() && depth[u] - depth[path.back()] <= d) {
        k -= --tally[color[path.back()]] == 0;
        path.pop_back();
    }
}

void dfs_unique(int u, int p = -1) {
    vector<array<int, 2>> children;
    for (auto c : adj[u]) {
        if (c != p) {
            children.push_back({height[c], c});
        }
    }
    sort(children.rbegin(), children.rend());

    if (!children.empty()) {
        if (children.size() > 1) {
            trim(u, children[1][0] + 1);
        }

        for (auto [h, c] : children) {
            k += ++tally[color[u]] == 1;
            path.push_back(u);

            dfs_unique(c, u);

            if (!path.empty() && path.back() == u) {
                k -= --tally[color[u]] == 0;
                path.pop_back();
            }

            trim(u, h + 1);
        }
    }

    ans[u] = max(ans[u], k);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;

        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    for (int i = 0; i < n; ++i) {
        cin >> color[i];
        --color[i];
    }

    dfs_depth(0);

    for (int i = 0; i < 2; ++i) {
        int root = max_element(depth, depth + n) - depth;
        depth[root] = 0;
        dfs_depth(root);
        dfs_height(root);
        dfs_unique(root);
    }

    for (int i = 0; i < n; ++i) {
        cout << ans[i] << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 5 ms 5068 KB Output is correct
3 Correct 6 ms 5196 KB Output is correct
4 Correct 5 ms 5324 KB Output is correct
5 Correct 5 ms 5068 KB Output is correct
6 Correct 5 ms 5452 KB Output is correct
7 Correct 5 ms 5324 KB Output is correct
8 Correct 6 ms 5068 KB Output is correct
9 Correct 5 ms 5196 KB Output is correct
10 Correct 6 ms 5196 KB Output is correct
11 Correct 6 ms 5196 KB Output is correct
12 Correct 6 ms 5196 KB Output is correct
13 Correct 5 ms 5504 KB Output is correct
14 Correct 5 ms 5304 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 6 ms 5196 KB Output is correct
17 Correct 5 ms 5324 KB Output is correct
18 Correct 5 ms 5300 KB Output is correct
19 Correct 5 ms 5196 KB Output is correct
20 Correct 5 ms 5452 KB Output is correct
21 Correct 5 ms 5324 KB Output is correct
22 Correct 5 ms 5072 KB Output is correct
23 Correct 6 ms 5168 KB Output is correct
24 Correct 5 ms 5196 KB Output is correct
25 Correct 5 ms 5172 KB Output is correct
26 Correct 5 ms 5196 KB Output is correct
27 Correct 5 ms 5424 KB Output is correct
28 Correct 5 ms 5324 KB Output is correct
29 Correct 5 ms 5324 KB Output is correct
30 Correct 5 ms 5196 KB Output is correct
31 Correct 5 ms 5324 KB Output is correct
32 Correct 5 ms 5324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 181 ms 12280 KB Output is correct
2 Correct 250 ms 32480 KB Output is correct
3 Correct 32 ms 9156 KB Output is correct
4 Correct 395 ms 17944 KB Output is correct
5 Correct 532 ms 52932 KB Output is correct
6 Correct 490 ms 35136 KB Output is correct
7 Correct 372 ms 18200 KB Output is correct
8 Correct 433 ms 21444 KB Output is correct
9 Correct 411 ms 20328 KB Output is correct
10 Correct 435 ms 20164 KB Output is correct
11 Correct 250 ms 21708 KB Output is correct
12 Correct 483 ms 39624 KB Output is correct
13 Correct 432 ms 35256 KB Output is correct
14 Correct 450 ms 34280 KB Output is correct
15 Correct 219 ms 21636 KB Output is correct
16 Correct 416 ms 42008 KB Output is correct
17 Correct 441 ms 35820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 304 ms 16384 KB Output is correct
2 Correct 489 ms 53192 KB Output is correct
3 Correct 38 ms 10052 KB Output is correct
4 Correct 409 ms 19560 KB Output is correct
5 Correct 540 ms 55612 KB Output is correct
6 Correct 493 ms 37060 KB Output is correct
7 Correct 392 ms 19656 KB Output is correct
8 Correct 500 ms 26812 KB Output is correct
9 Correct 411 ms 24380 KB Output is correct
10 Correct 449 ms 22364 KB Output is correct
11 Correct 337 ms 21164 KB Output is correct
12 Correct 554 ms 48168 KB Output is correct
13 Correct 435 ms 35236 KB Output is correct
14 Correct 450 ms 35468 KB Output is correct
15 Correct 238 ms 22716 KB Output is correct
16 Correct 429 ms 44180 KB Output is correct
17 Correct 466 ms 37716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 5 ms 5068 KB Output is correct
3 Correct 6 ms 5196 KB Output is correct
4 Correct 5 ms 5324 KB Output is correct
5 Correct 5 ms 5068 KB Output is correct
6 Correct 5 ms 5452 KB Output is correct
7 Correct 5 ms 5324 KB Output is correct
8 Correct 6 ms 5068 KB Output is correct
9 Correct 5 ms 5196 KB Output is correct
10 Correct 6 ms 5196 KB Output is correct
11 Correct 6 ms 5196 KB Output is correct
12 Correct 6 ms 5196 KB Output is correct
13 Correct 5 ms 5504 KB Output is correct
14 Correct 5 ms 5304 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 6 ms 5196 KB Output is correct
17 Correct 5 ms 5324 KB Output is correct
18 Correct 5 ms 5300 KB Output is correct
19 Correct 5 ms 5196 KB Output is correct
20 Correct 5 ms 5452 KB Output is correct
21 Correct 5 ms 5324 KB Output is correct
22 Correct 5 ms 5072 KB Output is correct
23 Correct 6 ms 5168 KB Output is correct
24 Correct 5 ms 5196 KB Output is correct
25 Correct 5 ms 5172 KB Output is correct
26 Correct 5 ms 5196 KB Output is correct
27 Correct 5 ms 5424 KB Output is correct
28 Correct 5 ms 5324 KB Output is correct
29 Correct 5 ms 5324 KB Output is correct
30 Correct 5 ms 5196 KB Output is correct
31 Correct 5 ms 5324 KB Output is correct
32 Correct 5 ms 5324 KB Output is correct
33 Correct 181 ms 12280 KB Output is correct
34 Correct 250 ms 32480 KB Output is correct
35 Correct 32 ms 9156 KB Output is correct
36 Correct 395 ms 17944 KB Output is correct
37 Correct 532 ms 52932 KB Output is correct
38 Correct 490 ms 35136 KB Output is correct
39 Correct 372 ms 18200 KB Output is correct
40 Correct 433 ms 21444 KB Output is correct
41 Correct 411 ms 20328 KB Output is correct
42 Correct 435 ms 20164 KB Output is correct
43 Correct 250 ms 21708 KB Output is correct
44 Correct 483 ms 39624 KB Output is correct
45 Correct 432 ms 35256 KB Output is correct
46 Correct 450 ms 34280 KB Output is correct
47 Correct 219 ms 21636 KB Output is correct
48 Correct 416 ms 42008 KB Output is correct
49 Correct 441 ms 35820 KB Output is correct
50 Correct 304 ms 16384 KB Output is correct
51 Correct 489 ms 53192 KB Output is correct
52 Correct 38 ms 10052 KB Output is correct
53 Correct 409 ms 19560 KB Output is correct
54 Correct 540 ms 55612 KB Output is correct
55 Correct 493 ms 37060 KB Output is correct
56 Correct 392 ms 19656 KB Output is correct
57 Correct 500 ms 26812 KB Output is correct
58 Correct 411 ms 24380 KB Output is correct
59 Correct 449 ms 22364 KB Output is correct
60 Correct 337 ms 21164 KB Output is correct
61 Correct 554 ms 48168 KB Output is correct
62 Correct 435 ms 35236 KB Output is correct
63 Correct 450 ms 35468 KB Output is correct
64 Correct 238 ms 22716 KB Output is correct
65 Correct 429 ms 44180 KB Output is correct
66 Correct 466 ms 37716 KB Output is correct
67 Correct 29 ms 6920 KB Output is correct
68 Correct 164 ms 26352 KB Output is correct
69 Correct 263 ms 24328 KB Output is correct
70 Correct 389 ms 17820 KB Output is correct
71 Correct 527 ms 52896 KB Output is correct
72 Correct 519 ms 35120 KB Output is correct
73 Correct 380 ms 17956 KB Output is correct
74 Correct 434 ms 24716 KB Output is correct
75 Correct 465 ms 21160 KB Output is correct
76 Correct 427 ms 20580 KB Output is correct
77 Correct 320 ms 19748 KB Output is correct
78 Correct 508 ms 43404 KB Output is correct
79 Correct 426 ms 39524 KB Output is correct
80 Correct 446 ms 32144 KB Output is correct
81 Correct 263 ms 21396 KB Output is correct
82 Correct 437 ms 41948 KB Output is correct
83 Correct 484 ms 35756 KB Output is correct
84 Correct 397 ms 18124 KB Output is correct
85 Correct 509 ms 53584 KB Output is correct
86 Correct 513 ms 35608 KB Output is correct
87 Correct 449 ms 18272 KB Output is correct
88 Correct 435 ms 25524 KB Output is correct
89 Correct 421 ms 21936 KB Output is correct
90 Correct 419 ms 21168 KB Output is correct
91 Correct 328 ms 20004 KB Output is correct
92 Correct 518 ms 52368 KB Output is correct
93 Correct 440 ms 30760 KB Output is correct
94 Correct 445 ms 27368 KB Output is correct
95 Correct 230 ms 22144 KB Output is correct
96 Correct 440 ms 42472 KB Output is correct
97 Correct 469 ms 36256 KB Output is correct
98 Correct 399 ms 19524 KB Output is correct
99 Correct 514 ms 53972 KB Output is correct
100 Correct 497 ms 36768 KB Output is correct
101 Correct 379 ms 19080 KB Output is correct
102 Correct 436 ms 24516 KB Output is correct
103 Correct 422 ms 22340 KB Output is correct
104 Correct 440 ms 21572 KB Output is correct
105 Correct 312 ms 20668 KB Output is correct
106 Correct 490 ms 38728 KB Output is correct
107 Correct 444 ms 39924 KB Output is correct
108 Correct 481 ms 30408 KB Output is correct
109 Correct 232 ms 22548 KB Output is correct
110 Correct 455 ms 43304 KB Output is correct
111 Correct 458 ms 37248 KB Output is correct