Submission #99280

# Submission time Handle Problem Language Result Execution time Memory
99280 2019-03-02T05:07:46 Z imeimi2000 Unique Cities (JOI19_ho_t5) C++17
100 / 100
777 ms 30644 KB
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int n, m;
vector<int> edge[200001];
int col[200001];
int dist[200001];
int dep[200001];

void dfs1(int x, int p) {
    dist[x] = dist[p] + 1;
    for (int i : edge[x]) {
        if (i == p) continue;
        dfs1(i, x);
    }
}

int ans[200001];

void dfs2(int x, int p) {
    dist[x] = dist[p] + 1;
    dep[x] = 0;
    for (int i : edge[x]) {
        if (i == p) continue;
        dfs2(i, x);
        dep[x] = max(dep[x], dep[i] + 1);
    }
}

vector<int> st;
int cnt[200001];
int sum;

void add(int x, int v) {
    x = col[x];
    if (cnt[x] == 0) ++sum;
    cnt[x] += v;
    if (cnt[x] == 0) --sum;
}

void dfs3(int x, int p) {
    int mx1 = -1, mx2 = -1;
    for (int i : edge[x]) {
        if (i == p) continue;
        if (mx1 < dep[i]) mx2 = mx1, mx1 = dep[i];
        else if (mx2 < dep[i]) mx2 = dep[i];
    }
    int mxi = 0;
    for (int i : edge[x]) {
        if (i == p) continue;
        if (dep[i] == mx1) {
            mxi = i;
            break;
        }
    }
    ++mx1; ++mx2;
    while (!st.empty() && dist[x] - dist[st.back()] <= mx2) {
        add(st.back(), -1);
        st.pop_back();
    }
    if (mxi) {
        add(x, 1);
        st.push_back(x);
        dfs3(mxi, x);
    }
    while (!st.empty() && dist[x] - dist[st.back()] <= mx1) {
        add(st.back(), -1);
        st.pop_back();
    }
    ans[x] = max(ans[x], sum);
    for (int i : edge[x]) {
        if (i == p || i == mxi) continue;
        if (st.empty() || st.back() != x) {
            add(x, 1);
            st.push_back(x);
        }
        dfs3(i, x);
        if (!st.empty() && st.back() == x) {
            add(x, -1);
            st.pop_back();
        }
    }
}

void solve(int R) {
    dfs2(R, 0);
    st.clear();
    sum = 0;
    for (int i = 1; i <= m; ++i) cnt[i] = 0;
    dfs3(R, 0);
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    for (int i = 1; i <= n; ++i) cin >> col[i];
    dfs1(1, 0);
    int P = max_element(dist + 1, dist + (n + 1)) - dist;
    dfs1(P, 0);
    int Q = max_element(dist + 1, dist + (n + 1)) - dist;
    solve(P); solve(Q);
    for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 10 ms 5248 KB Output is correct
5 Correct 9 ms 5200 KB Output is correct
6 Correct 10 ms 5248 KB Output is correct
7 Correct 10 ms 5248 KB Output is correct
8 Correct 11 ms 5120 KB Output is correct
9 Correct 11 ms 5120 KB Output is correct
10 Correct 10 ms 5248 KB Output is correct
11 Correct 15 ms 5120 KB Output is correct
12 Correct 9 ms 5248 KB Output is correct
13 Correct 9 ms 5248 KB Output is correct
14 Correct 4 ms 5376 KB Output is correct
15 Correct 10 ms 5248 KB Output is correct
16 Correct 11 ms 5120 KB Output is correct
17 Correct 9 ms 5248 KB Output is correct
18 Correct 9 ms 5248 KB Output is correct
19 Correct 10 ms 5248 KB Output is correct
20 Correct 9 ms 5248 KB Output is correct
21 Correct 11 ms 5276 KB Output is correct
22 Correct 11 ms 5248 KB Output is correct
23 Correct 9 ms 5120 KB Output is correct
24 Correct 10 ms 5248 KB Output is correct
25 Correct 10 ms 5120 KB Output is correct
26 Correct 10 ms 5248 KB Output is correct
27 Correct 9 ms 5248 KB Output is correct
28 Correct 9 ms 5248 KB Output is correct
29 Correct 8 ms 5248 KB Output is correct
30 Correct 11 ms 5120 KB Output is correct
31 Correct 9 ms 5220 KB Output is correct
32 Correct 10 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 360 ms 10940 KB Output is correct
2 Correct 431 ms 18580 KB Output is correct
3 Correct 56 ms 7544 KB Output is correct
4 Correct 622 ms 15088 KB Output is correct
5 Correct 671 ms 28452 KB Output is correct
6 Correct 674 ms 21604 KB Output is correct
7 Correct 453 ms 15188 KB Output is correct
8 Correct 589 ms 16340 KB Output is correct
9 Correct 576 ms 15888 KB Output is correct
10 Correct 573 ms 15924 KB Output is correct
11 Correct 402 ms 15708 KB Output is correct
12 Correct 752 ms 23252 KB Output is correct
13 Correct 676 ms 21620 KB Output is correct
14 Correct 679 ms 21192 KB Output is correct
15 Correct 277 ms 15784 KB Output is correct
16 Correct 661 ms 24280 KB Output is correct
17 Correct 687 ms 21912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 429 ms 13648 KB Output is correct
2 Correct 673 ms 29576 KB Output is correct
3 Correct 53 ms 8312 KB Output is correct
4 Correct 586 ms 16440 KB Output is correct
5 Correct 669 ms 30644 KB Output is correct
6 Correct 704 ms 23152 KB Output is correct
7 Correct 570 ms 16504 KB Output is correct
8 Correct 639 ms 19320 KB Output is correct
9 Correct 639 ms 18296 KB Output is correct
10 Correct 557 ms 17552 KB Output is correct
11 Correct 479 ms 16816 KB Output is correct
12 Correct 777 ms 27776 KB Output is correct
13 Correct 671 ms 22436 KB Output is correct
14 Correct 581 ms 22388 KB Output is correct
15 Correct 359 ms 17132 KB Output is correct
16 Correct 688 ms 26172 KB Output is correct
17 Correct 685 ms 23540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 10 ms 5248 KB Output is correct
5 Correct 9 ms 5200 KB Output is correct
6 Correct 10 ms 5248 KB Output is correct
7 Correct 10 ms 5248 KB Output is correct
8 Correct 11 ms 5120 KB Output is correct
9 Correct 11 ms 5120 KB Output is correct
10 Correct 10 ms 5248 KB Output is correct
11 Correct 15 ms 5120 KB Output is correct
12 Correct 9 ms 5248 KB Output is correct
13 Correct 9 ms 5248 KB Output is correct
14 Correct 4 ms 5376 KB Output is correct
15 Correct 10 ms 5248 KB Output is correct
16 Correct 11 ms 5120 KB Output is correct
17 Correct 9 ms 5248 KB Output is correct
18 Correct 9 ms 5248 KB Output is correct
19 Correct 10 ms 5248 KB Output is correct
20 Correct 9 ms 5248 KB Output is correct
21 Correct 11 ms 5276 KB Output is correct
22 Correct 11 ms 5248 KB Output is correct
23 Correct 9 ms 5120 KB Output is correct
24 Correct 10 ms 5248 KB Output is correct
25 Correct 10 ms 5120 KB Output is correct
26 Correct 10 ms 5248 KB Output is correct
27 Correct 9 ms 5248 KB Output is correct
28 Correct 9 ms 5248 KB Output is correct
29 Correct 8 ms 5248 KB Output is correct
30 Correct 11 ms 5120 KB Output is correct
31 Correct 9 ms 5220 KB Output is correct
32 Correct 10 ms 5248 KB Output is correct
33 Correct 360 ms 10940 KB Output is correct
34 Correct 431 ms 18580 KB Output is correct
35 Correct 56 ms 7544 KB Output is correct
36 Correct 622 ms 15088 KB Output is correct
37 Correct 671 ms 28452 KB Output is correct
38 Correct 674 ms 21604 KB Output is correct
39 Correct 453 ms 15188 KB Output is correct
40 Correct 589 ms 16340 KB Output is correct
41 Correct 576 ms 15888 KB Output is correct
42 Correct 573 ms 15924 KB Output is correct
43 Correct 402 ms 15708 KB Output is correct
44 Correct 752 ms 23252 KB Output is correct
45 Correct 676 ms 21620 KB Output is correct
46 Correct 679 ms 21192 KB Output is correct
47 Correct 277 ms 15784 KB Output is correct
48 Correct 661 ms 24280 KB Output is correct
49 Correct 687 ms 21912 KB Output is correct
50 Correct 429 ms 13648 KB Output is correct
51 Correct 673 ms 29576 KB Output is correct
52 Correct 53 ms 8312 KB Output is correct
53 Correct 586 ms 16440 KB Output is correct
54 Correct 669 ms 30644 KB Output is correct
55 Correct 704 ms 23152 KB Output is correct
56 Correct 570 ms 16504 KB Output is correct
57 Correct 639 ms 19320 KB Output is correct
58 Correct 639 ms 18296 KB Output is correct
59 Correct 557 ms 17552 KB Output is correct
60 Correct 479 ms 16816 KB Output is correct
61 Correct 777 ms 27776 KB Output is correct
62 Correct 671 ms 22436 KB Output is correct
63 Correct 581 ms 22388 KB Output is correct
64 Correct 359 ms 17132 KB Output is correct
65 Correct 688 ms 26172 KB Output is correct
66 Correct 685 ms 23540 KB Output is correct
67 Correct 43 ms 6876 KB Output is correct
68 Correct 300 ms 16280 KB Output is correct
69 Correct 357 ms 16120 KB Output is correct
70 Correct 561 ms 15652 KB Output is correct
71 Correct 765 ms 28912 KB Output is correct
72 Correct 661 ms 22132 KB Output is correct
73 Correct 558 ms 15608 KB Output is correct
74 Correct 567 ms 18040 KB Output is correct
75 Correct 626 ms 16588 KB Output is correct
76 Correct 586 ms 16504 KB Output is correct
77 Correct 391 ms 15852 KB Output is correct
78 Correct 651 ms 25340 KB Output is correct
79 Correct 566 ms 23900 KB Output is correct
80 Correct 563 ms 20940 KB Output is correct
81 Correct 242 ms 16108 KB Output is correct
82 Correct 599 ms 24648 KB Output is correct
83 Correct 545 ms 22388 KB Output is correct
84 Correct 508 ms 15500 KB Output is correct
85 Correct 639 ms 29164 KB Output is correct
86 Correct 698 ms 22292 KB Output is correct
87 Correct 617 ms 15608 KB Output is correct
88 Correct 691 ms 18296 KB Output is correct
89 Correct 547 ms 17016 KB Output is correct
90 Correct 575 ms 16760 KB Output is correct
91 Correct 363 ms 15828 KB Output is correct
92 Correct 726 ms 28728 KB Output is correct
93 Correct 586 ms 20184 KB Output is correct
94 Correct 589 ms 18756 KB Output is correct
95 Correct 247 ms 16220 KB Output is correct
96 Correct 574 ms 24844 KB Output is correct
97 Correct 631 ms 22400 KB Output is correct
98 Correct 600 ms 16244 KB Output is correct
99 Correct 750 ms 29420 KB Output is correct
100 Correct 638 ms 22872 KB Output is correct
101 Correct 537 ms 15664 KB Output is correct
102 Correct 614 ms 17912 KB Output is correct
103 Correct 628 ms 17016 KB Output is correct
104 Correct 620 ms 16624 KB Output is correct
105 Correct 357 ms 15900 KB Output is correct
106 Correct 632 ms 23576 KB Output is correct
107 Correct 573 ms 23912 KB Output is correct
108 Correct 564 ms 20248 KB Output is correct
109 Correct 234 ms 16392 KB Output is correct
110 Correct 528 ms 25156 KB Output is correct
111 Correct 598 ms 22772 KB Output is correct