Submission #591344

# Submission time Handle Problem Language Result Execution time Memory
591344 2022-07-07T10:30:17 Z piOOE Unique Cities (JOI19_ho_t5) C++17
100 / 100
349 ms 38424 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 200001;

vector<int> g[N], stk;

int c[N], cnt[N], cnt_unique = 0, ans[N], depth[N], dp[N], diam;

void pop() {
    int x = stk.back();
    stk.pop_back();
    if (--cnt[c[x]] == 0) {
        cnt_unique -= 1;
    }
}

void push(int x) {
    stk.push_back(x);
    if (++cnt[c[x]] == 1) {
        cnt_unique += 1;
    }
}

void dfs1(int v, int p) {
    dp[v] = depth[v] = depth[p] + 1;
    if (dp[v] > dp[diam]) {
        diam = v;
    }
    for (int to: g[v]) {
        if (to != p) {
            dfs1(to, v);
            dp[v] = max(dp[v], dp[to]);
        }
    }
}

void dfs(int v, int p) {
    if (p != 0) {
        push(p);
    }
    for (int t = 0; t < 2; ++t) {
        for (int i = t; i < g[v].size(); ++i) {
            if (g[v][i] != p && (g[v][t] == p || dp[g[v][i]] > dp[g[v][t]])) {
                swap(g[v][t], g[v][i]);
            }
        }
    }
    int mx1 = g[v][0], mx2 = g[v].size() == 1 || g[v][1] == p ? 0 : g[v][1];
    for (int to: g[v]) {
        if (to != p) {
            int len = (to == mx1 ? dp[mx2] : dp[mx1]) - depth[v];
            while (!stk.empty() && depth[v] - depth[stk.back()] <= len) {
                pop();
            }
            dfs(to, v);
        }
    }
    while (!stk.empty() && dp[v] - depth[v] >= depth[v] - depth[stk.back()]) {
        pop();
    }
    ans[v] = max(ans[v], cnt_unique);
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i < n; ++i) {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i = 1; i <= n; ++i) {
        cin >> c[i];
    }
    dfs1(diam = 1, 0);
    for (int t = 0, x; t < 2; ++t) {
        //t == 0 ? go from one end : from another
        dfs1(x = diam, 0);
        dfs(x, 0);
    }
    for (int i = 1; i <= n; ++i) {
        cout << ans[i] << "\n";
    }
    return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'void dfs(int, int)':
joi2019_ho_t5.cpp:44:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |         for (int i = t; i < g[v].size(); ++i) {
      |                         ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 4 ms 5176 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 4 ms 5332 KB Output is correct
7 Correct 6 ms 5204 KB Output is correct
8 Correct 5 ms 5076 KB Output is correct
9 Correct 5 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 4 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 4 ms 5332 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 4 ms 5228 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 4 ms 5304 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 4 ms 5172 KB Output is correct
20 Correct 4 ms 5332 KB Output is correct
21 Correct 5 ms 5176 KB Output is correct
22 Correct 4 ms 5144 KB Output is correct
23 Correct 5 ms 5204 KB Output is correct
24 Correct 5 ms 5192 KB Output is correct
25 Correct 5 ms 5168 KB Output is correct
26 Correct 4 ms 5172 KB Output is correct
27 Correct 4 ms 5296 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
29 Correct 4 ms 5168 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5228 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 136 ms 12256 KB Output is correct
2 Correct 181 ms 23532 KB Output is correct
3 Correct 22 ms 8196 KB Output is correct
4 Correct 291 ms 17212 KB Output is correct
5 Correct 343 ms 36740 KB Output is correct
6 Correct 298 ms 26900 KB Output is correct
7 Correct 260 ms 17220 KB Output is correct
8 Correct 263 ms 19120 KB Output is correct
9 Correct 286 ms 18568 KB Output is correct
10 Correct 264 ms 18380 KB Output is correct
11 Correct 128 ms 17776 KB Output is correct
12 Correct 318 ms 29200 KB Output is correct
13 Correct 283 ms 26720 KB Output is correct
14 Correct 288 ms 26136 KB Output is correct
15 Correct 117 ms 17896 KB Output is correct
16 Correct 309 ms 30484 KB Output is correct
17 Correct 267 ms 27036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 232 ms 15728 KB Output is correct
2 Correct 326 ms 37064 KB Output is correct
3 Correct 24 ms 8732 KB Output is correct
4 Correct 300 ms 18116 KB Output is correct
5 Correct 326 ms 38424 KB Output is correct
6 Correct 321 ms 27872 KB Output is correct
7 Correct 254 ms 18036 KB Output is correct
8 Correct 330 ms 22028 KB Output is correct
9 Correct 288 ms 20628 KB Output is correct
10 Correct 308 ms 19408 KB Output is correct
11 Correct 224 ms 18236 KB Output is correct
12 Correct 349 ms 34244 KB Output is correct
13 Correct 308 ms 26640 KB Output is correct
14 Correct 297 ms 26760 KB Output is correct
15 Correct 120 ms 17856 KB Output is correct
16 Correct 292 ms 31812 KB Output is correct
17 Correct 295 ms 28296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 4 ms 5176 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 4 ms 5332 KB Output is correct
7 Correct 6 ms 5204 KB Output is correct
8 Correct 5 ms 5076 KB Output is correct
9 Correct 5 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 4 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 4 ms 5332 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 4 ms 5228 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 4 ms 5304 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 4 ms 5172 KB Output is correct
20 Correct 4 ms 5332 KB Output is correct
21 Correct 5 ms 5176 KB Output is correct
22 Correct 4 ms 5144 KB Output is correct
23 Correct 5 ms 5204 KB Output is correct
24 Correct 5 ms 5192 KB Output is correct
25 Correct 5 ms 5168 KB Output is correct
26 Correct 4 ms 5172 KB Output is correct
27 Correct 4 ms 5296 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
29 Correct 4 ms 5168 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5228 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 136 ms 12256 KB Output is correct
34 Correct 181 ms 23532 KB Output is correct
35 Correct 22 ms 8196 KB Output is correct
36 Correct 291 ms 17212 KB Output is correct
37 Correct 343 ms 36740 KB Output is correct
38 Correct 298 ms 26900 KB Output is correct
39 Correct 260 ms 17220 KB Output is correct
40 Correct 263 ms 19120 KB Output is correct
41 Correct 286 ms 18568 KB Output is correct
42 Correct 264 ms 18380 KB Output is correct
43 Correct 128 ms 17776 KB Output is correct
44 Correct 318 ms 29200 KB Output is correct
45 Correct 283 ms 26720 KB Output is correct
46 Correct 288 ms 26136 KB Output is correct
47 Correct 117 ms 17896 KB Output is correct
48 Correct 309 ms 30484 KB Output is correct
49 Correct 267 ms 27036 KB Output is correct
50 Correct 232 ms 15728 KB Output is correct
51 Correct 326 ms 37064 KB Output is correct
52 Correct 24 ms 8732 KB Output is correct
53 Correct 300 ms 18116 KB Output is correct
54 Correct 326 ms 38424 KB Output is correct
55 Correct 321 ms 27872 KB Output is correct
56 Correct 254 ms 18036 KB Output is correct
57 Correct 330 ms 22028 KB Output is correct
58 Correct 288 ms 20628 KB Output is correct
59 Correct 308 ms 19408 KB Output is correct
60 Correct 224 ms 18236 KB Output is correct
61 Correct 349 ms 34244 KB Output is correct
62 Correct 308 ms 26640 KB Output is correct
63 Correct 297 ms 26760 KB Output is correct
64 Correct 120 ms 17856 KB Output is correct
65 Correct 292 ms 31812 KB Output is correct
66 Correct 295 ms 28296 KB Output is correct
67 Correct 23 ms 6920 KB Output is correct
68 Correct 109 ms 19740 KB Output is correct
69 Correct 195 ms 19540 KB Output is correct
70 Correct 255 ms 17328 KB Output is correct
71 Correct 322 ms 36812 KB Output is correct
72 Correct 333 ms 26888 KB Output is correct
73 Correct 237 ms 17220 KB Output is correct
74 Correct 346 ms 20928 KB Output is correct
75 Correct 265 ms 18836 KB Output is correct
76 Correct 291 ms 18596 KB Output is correct
77 Correct 190 ms 17556 KB Output is correct
78 Correct 321 ms 31376 KB Output is correct
79 Correct 311 ms 29316 KB Output is correct
80 Correct 266 ms 25008 KB Output is correct
81 Correct 123 ms 17900 KB Output is correct
82 Correct 286 ms 30544 KB Output is correct
83 Correct 263 ms 27080 KB Output is correct
84 Correct 284 ms 17324 KB Output is correct
85 Correct 322 ms 37180 KB Output is correct
86 Correct 337 ms 27048 KB Output is correct
87 Correct 267 ms 17228 KB Output is correct
88 Correct 287 ms 21332 KB Output is correct
89 Correct 284 ms 19232 KB Output is correct
90 Correct 297 ms 18852 KB Output is correct
91 Correct 201 ms 17512 KB Output is correct
92 Correct 334 ms 36412 KB Output is correct
93 Correct 290 ms 24064 KB Output is correct
94 Correct 285 ms 22220 KB Output is correct
95 Correct 124 ms 17788 KB Output is correct
96 Correct 279 ms 30800 KB Output is correct
97 Correct 319 ms 27300 KB Output is correct
98 Correct 288 ms 17956 KB Output is correct
99 Correct 344 ms 37320 KB Output is correct
100 Correct 324 ms 27624 KB Output is correct
101 Correct 245 ms 17512 KB Output is correct
102 Correct 274 ms 20652 KB Output is correct
103 Correct 321 ms 19292 KB Output is correct
104 Correct 265 ms 18912 KB Output is correct
105 Correct 226 ms 17660 KB Output is correct
106 Correct 341 ms 28916 KB Output is correct
107 Correct 298 ms 29252 KB Output is correct
108 Correct 277 ms 23924 KB Output is correct
109 Correct 132 ms 17896 KB Output is correct
110 Correct 273 ms 31168 KB Output is correct
111 Correct 304 ms 27568 KB Output is correct