Submission #423262

# Submission time Handle Problem Language Result Execution time Memory
423262 2021-06-10T21:12:02 Z Kerim Unique Cities (JOI19_ho_t5) C++17
100 / 100
574 ms 55564 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);
        }
		bool f=0;
        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();
            }
            if(!f)
				trim(u, h + 1),f=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 3 ms 5032 KB Output is correct
2 Correct 5 ms 5068 KB Output is correct
3 Correct 4 ms 5168 KB Output is correct
4 Correct 5 ms 5292 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 5 ms 5068 KB Output is correct
9 Correct 5 ms 5208 KB Output is correct
10 Correct 5 ms 5168 KB Output is correct
11 Correct 5 ms 5196 KB Output is correct
12 Correct 6 ms 5196 KB Output is correct
13 Correct 5 ms 5452 KB Output is correct
14 Correct 5 ms 5196 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 5 ms 5196 KB Output is correct
17 Correct 5 ms 5300 KB Output is correct
18 Correct 5 ms 5324 KB Output is correct
19 Correct 5 ms 5068 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 5068 KB Output is correct
23 Correct 5 ms 5196 KB Output is correct
24 Correct 4 ms 5196 KB Output is correct
25 Correct 5 ms 5208 KB Output is correct
26 Correct 5 ms 5196 KB Output is correct
27 Correct 5 ms 5324 KB Output is correct
28 Correct 5 ms 5324 KB Output is correct
29 Correct 6 ms 5296 KB Output is correct
30 Correct 5 ms 5168 KB Output is correct
31 Correct 5 ms 5420 KB Output is correct
32 Correct 5 ms 5324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 12456 KB Output is correct
2 Correct 240 ms 32476 KB Output is correct
3 Correct 33 ms 9184 KB Output is correct
4 Correct 389 ms 17928 KB Output is correct
5 Correct 490 ms 52932 KB Output is correct
6 Correct 483 ms 35212 KB Output is correct
7 Correct 384 ms 18232 KB Output is correct
8 Correct 461 ms 21496 KB Output is correct
9 Correct 414 ms 20376 KB Output is correct
10 Correct 402 ms 20136 KB Output is correct
11 Correct 249 ms 21624 KB Output is correct
12 Correct 497 ms 39660 KB Output is correct
13 Correct 453 ms 35344 KB Output is correct
14 Correct 411 ms 34280 KB Output is correct
15 Correct 209 ms 21520 KB Output is correct
16 Correct 413 ms 42068 KB Output is correct
17 Correct 485 ms 35804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 310 ms 16376 KB Output is correct
2 Correct 572 ms 53196 KB Output is correct
3 Correct 37 ms 10036 KB Output is correct
4 Correct 407 ms 19528 KB Output is correct
5 Correct 497 ms 55564 KB Output is correct
6 Correct 490 ms 37052 KB Output is correct
7 Correct 404 ms 19700 KB Output is correct
8 Correct 417 ms 26776 KB Output is correct
9 Correct 447 ms 24424 KB Output is correct
10 Correct 404 ms 22328 KB Output is correct
11 Correct 354 ms 21164 KB Output is correct
12 Correct 509 ms 48180 KB Output is correct
13 Correct 457 ms 35136 KB Output is correct
14 Correct 430 ms 35464 KB Output is correct
15 Correct 217 ms 22688 KB Output is correct
16 Correct 423 ms 44188 KB Output is correct
17 Correct 460 ms 37704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5032 KB Output is correct
2 Correct 5 ms 5068 KB Output is correct
3 Correct 4 ms 5168 KB Output is correct
4 Correct 5 ms 5292 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 5 ms 5068 KB Output is correct
9 Correct 5 ms 5208 KB Output is correct
10 Correct 5 ms 5168 KB Output is correct
11 Correct 5 ms 5196 KB Output is correct
12 Correct 6 ms 5196 KB Output is correct
13 Correct 5 ms 5452 KB Output is correct
14 Correct 5 ms 5196 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 5 ms 5196 KB Output is correct
17 Correct 5 ms 5300 KB Output is correct
18 Correct 5 ms 5324 KB Output is correct
19 Correct 5 ms 5068 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 5068 KB Output is correct
23 Correct 5 ms 5196 KB Output is correct
24 Correct 4 ms 5196 KB Output is correct
25 Correct 5 ms 5208 KB Output is correct
26 Correct 5 ms 5196 KB Output is correct
27 Correct 5 ms 5324 KB Output is correct
28 Correct 5 ms 5324 KB Output is correct
29 Correct 6 ms 5296 KB Output is correct
30 Correct 5 ms 5168 KB Output is correct
31 Correct 5 ms 5420 KB Output is correct
32 Correct 5 ms 5324 KB Output is correct
33 Correct 177 ms 12456 KB Output is correct
34 Correct 240 ms 32476 KB Output is correct
35 Correct 33 ms 9184 KB Output is correct
36 Correct 389 ms 17928 KB Output is correct
37 Correct 490 ms 52932 KB Output is correct
38 Correct 483 ms 35212 KB Output is correct
39 Correct 384 ms 18232 KB Output is correct
40 Correct 461 ms 21496 KB Output is correct
41 Correct 414 ms 20376 KB Output is correct
42 Correct 402 ms 20136 KB Output is correct
43 Correct 249 ms 21624 KB Output is correct
44 Correct 497 ms 39660 KB Output is correct
45 Correct 453 ms 35344 KB Output is correct
46 Correct 411 ms 34280 KB Output is correct
47 Correct 209 ms 21520 KB Output is correct
48 Correct 413 ms 42068 KB Output is correct
49 Correct 485 ms 35804 KB Output is correct
50 Correct 310 ms 16376 KB Output is correct
51 Correct 572 ms 53196 KB Output is correct
52 Correct 37 ms 10036 KB Output is correct
53 Correct 407 ms 19528 KB Output is correct
54 Correct 497 ms 55564 KB Output is correct
55 Correct 490 ms 37052 KB Output is correct
56 Correct 404 ms 19700 KB Output is correct
57 Correct 417 ms 26776 KB Output is correct
58 Correct 447 ms 24424 KB Output is correct
59 Correct 404 ms 22328 KB Output is correct
60 Correct 354 ms 21164 KB Output is correct
61 Correct 509 ms 48180 KB Output is correct
62 Correct 457 ms 35136 KB Output is correct
63 Correct 430 ms 35464 KB Output is correct
64 Correct 217 ms 22688 KB Output is correct
65 Correct 423 ms 44188 KB Output is correct
66 Correct 460 ms 37704 KB Output is correct
67 Correct 29 ms 6896 KB Output is correct
68 Correct 141 ms 26340 KB Output is correct
69 Correct 225 ms 24276 KB Output is correct
70 Correct 395 ms 17816 KB Output is correct
71 Correct 468 ms 52932 KB Output is correct
72 Correct 450 ms 35084 KB Output is correct
73 Correct 367 ms 17980 KB Output is correct
74 Correct 390 ms 24772 KB Output is correct
75 Correct 394 ms 21124 KB Output is correct
76 Correct 437 ms 20576 KB Output is correct
77 Correct 300 ms 19632 KB Output is correct
78 Correct 454 ms 43384 KB Output is correct
79 Correct 415 ms 39484 KB Output is correct
80 Correct 475 ms 32096 KB Output is correct
81 Correct 229 ms 21452 KB Output is correct
82 Correct 494 ms 41948 KB Output is correct
83 Correct 452 ms 35760 KB Output is correct
84 Correct 468 ms 18152 KB Output is correct
85 Correct 574 ms 53580 KB Output is correct
86 Correct 519 ms 35592 KB Output is correct
87 Correct 435 ms 18304 KB Output is correct
88 Correct 464 ms 25412 KB Output is correct
89 Correct 414 ms 22000 KB Output is correct
90 Correct 505 ms 21172 KB Output is correct
91 Correct 318 ms 20028 KB Output is correct
92 Correct 545 ms 52392 KB Output is correct
93 Correct 428 ms 30764 KB Output is correct
94 Correct 456 ms 27320 KB Output is correct
95 Correct 229 ms 21912 KB Output is correct
96 Correct 434 ms 42540 KB Output is correct
97 Correct 479 ms 36280 KB Output is correct
98 Correct 482 ms 19524 KB Output is correct
99 Correct 555 ms 53964 KB Output is correct
100 Correct 510 ms 36772 KB Output is correct
101 Correct 509 ms 19124 KB Output is correct
102 Correct 491 ms 24464 KB Output is correct
103 Correct 432 ms 22264 KB Output is correct
104 Correct 469 ms 21608 KB Output is correct
105 Correct 331 ms 20448 KB Output is correct
106 Correct 508 ms 38840 KB Output is correct
107 Correct 437 ms 39972 KB Output is correct
108 Correct 516 ms 30228 KB Output is correct
109 Correct 237 ms 22616 KB Output is correct
110 Correct 458 ms 43376 KB Output is correct
111 Correct 507 ms 37196 KB Output is correct