Submission #947513

# Submission time Handle Problem Language Result Execution time Memory
947513 2024-03-16T10:24:55 Z weakweakweak Capital City (JOI20_capital_city) C++17
100 / 100
499 ms 57052 KB
//又抄解
#include <bits/stdc++.h>
using namespace std;

const int N = 510000;
int n, k, col[N], sz[N], parr[N], vis[N] = {0}, ans = INT_MAX, ins[N] = {0}, tt = 3, tt2 = 5;
bool block[N] = {0}, finish[N] = {0};
vector <int> e[N], bel[N];


int dfs_sz (int i, int par) {
    sz[i] = 1;
    parr[i] = par;
    for (int j : e[i]) if (j != par and !block[j]) {
        dfs_sz (j, i);
        sz[i] += sz[j];
    }
    vis[i] = tt;
return sz[i];}

int dfs_cen (int i, int par, int tot) {
    for (int j : e[i]) if (j != par and !block[j] and sz[j] * 2 > tot) return dfs_cen(j, i, tot);
    return i;
}

void solve (int st) {
    vector <int> color_used = {st};
    queue<int> q;
    tt2++;
    ins[st] = tt2;
    for (int j : bel[st]) {
        q.push(j);
        if (vis[j] != tt) return;
    }
    while (q.size()) {
        int i = q.front(); q.pop();
        if (ins[col[parr[i]]] != tt2) {
            ins[col[parr[i]]] = tt2;
            color_used.push_back(col[parr[i]]);
            for (int x : bel[col[parr[i]]]) {
                q.push(x);
                if (vis[x] != tt) return;
            }
        }
     }
    ans = min(ans, (int)color_used.size() - 1);
}

void CD (int i) {
    int tot = dfs_sz(i, i);
    int cen = dfs_cen(i, i, tot);
    dfs_sz(cen, cen);
    solve(col[cen]);
    block[cen] = 1;
    tt++;
    for (int j : e[cen]) if (!block[j]) CD(j);
}

int main () {
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> n >> k;
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        e[x].push_back(y);
        e[y].push_back(x);
    }   
    for (int i = 1; i <= n; i++) {
        cin >> col[i];
        bel[col[i]] . push_back(i);
    }

    CD(1);

    cout << ans << '\n';
return 0;}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24412 KB Output is correct
2 Correct 11 ms 24244 KB Output is correct
3 Correct 11 ms 24412 KB Output is correct
4 Correct 10 ms 24412 KB Output is correct
5 Correct 11 ms 24412 KB Output is correct
6 Correct 12 ms 24392 KB Output is correct
7 Correct 14 ms 24412 KB Output is correct
8 Correct 11 ms 24408 KB Output is correct
9 Correct 12 ms 24308 KB Output is correct
10 Correct 11 ms 24412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24412 KB Output is correct
2 Correct 11 ms 24244 KB Output is correct
3 Correct 11 ms 24412 KB Output is correct
4 Correct 10 ms 24412 KB Output is correct
5 Correct 11 ms 24412 KB Output is correct
6 Correct 12 ms 24392 KB Output is correct
7 Correct 14 ms 24412 KB Output is correct
8 Correct 11 ms 24408 KB Output is correct
9 Correct 12 ms 24308 KB Output is correct
10 Correct 11 ms 24412 KB Output is correct
11 Correct 13 ms 24412 KB Output is correct
12 Correct 13 ms 24412 KB Output is correct
13 Correct 12 ms 24424 KB Output is correct
14 Correct 13 ms 24412 KB Output is correct
15 Correct 13 ms 24408 KB Output is correct
16 Correct 13 ms 24412 KB Output is correct
17 Correct 16 ms 24748 KB Output is correct
18 Correct 13 ms 24668 KB Output is correct
19 Correct 13 ms 24668 KB Output is correct
20 Correct 13 ms 24664 KB Output is correct
21 Correct 12 ms 24668 KB Output is correct
22 Correct 12 ms 24668 KB Output is correct
23 Correct 13 ms 24668 KB Output is correct
24 Correct 13 ms 24664 KB Output is correct
25 Correct 17 ms 24668 KB Output is correct
26 Correct 16 ms 24668 KB Output is correct
27 Correct 15 ms 24668 KB Output is correct
28 Correct 15 ms 24668 KB Output is correct
29 Correct 13 ms 24668 KB Output is correct
30 Correct 13 ms 24668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 363 ms 56148 KB Output is correct
2 Correct 169 ms 56624 KB Output is correct
3 Correct 351 ms 55888 KB Output is correct
4 Correct 149 ms 56660 KB Output is correct
5 Correct 333 ms 53076 KB Output is correct
6 Correct 158 ms 57052 KB Output is correct
7 Correct 406 ms 53480 KB Output is correct
8 Correct 153 ms 56456 KB Output is correct
9 Correct 417 ms 51700 KB Output is correct
10 Correct 401 ms 49272 KB Output is correct
11 Correct 428 ms 51940 KB Output is correct
12 Correct 406 ms 54736 KB Output is correct
13 Correct 421 ms 48968 KB Output is correct
14 Correct 430 ms 54924 KB Output is correct
15 Correct 407 ms 54540 KB Output is correct
16 Correct 499 ms 49688 KB Output is correct
17 Correct 378 ms 50160 KB Output is correct
18 Correct 422 ms 50492 KB Output is correct
19 Correct 451 ms 53480 KB Output is correct
20 Correct 419 ms 55824 KB Output is correct
21 Correct 13 ms 24412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24412 KB Output is correct
2 Correct 11 ms 24244 KB Output is correct
3 Correct 11 ms 24412 KB Output is correct
4 Correct 10 ms 24412 KB Output is correct
5 Correct 11 ms 24412 KB Output is correct
6 Correct 12 ms 24392 KB Output is correct
7 Correct 14 ms 24412 KB Output is correct
8 Correct 11 ms 24408 KB Output is correct
9 Correct 12 ms 24308 KB Output is correct
10 Correct 11 ms 24412 KB Output is correct
11 Correct 13 ms 24412 KB Output is correct
12 Correct 13 ms 24412 KB Output is correct
13 Correct 12 ms 24424 KB Output is correct
14 Correct 13 ms 24412 KB Output is correct
15 Correct 13 ms 24408 KB Output is correct
16 Correct 13 ms 24412 KB Output is correct
17 Correct 16 ms 24748 KB Output is correct
18 Correct 13 ms 24668 KB Output is correct
19 Correct 13 ms 24668 KB Output is correct
20 Correct 13 ms 24664 KB Output is correct
21 Correct 12 ms 24668 KB Output is correct
22 Correct 12 ms 24668 KB Output is correct
23 Correct 13 ms 24668 KB Output is correct
24 Correct 13 ms 24664 KB Output is correct
25 Correct 17 ms 24668 KB Output is correct
26 Correct 16 ms 24668 KB Output is correct
27 Correct 15 ms 24668 KB Output is correct
28 Correct 15 ms 24668 KB Output is correct
29 Correct 13 ms 24668 KB Output is correct
30 Correct 13 ms 24668 KB Output is correct
31 Correct 363 ms 56148 KB Output is correct
32 Correct 169 ms 56624 KB Output is correct
33 Correct 351 ms 55888 KB Output is correct
34 Correct 149 ms 56660 KB Output is correct
35 Correct 333 ms 53076 KB Output is correct
36 Correct 158 ms 57052 KB Output is correct
37 Correct 406 ms 53480 KB Output is correct
38 Correct 153 ms 56456 KB Output is correct
39 Correct 417 ms 51700 KB Output is correct
40 Correct 401 ms 49272 KB Output is correct
41 Correct 428 ms 51940 KB Output is correct
42 Correct 406 ms 54736 KB Output is correct
43 Correct 421 ms 48968 KB Output is correct
44 Correct 430 ms 54924 KB Output is correct
45 Correct 407 ms 54540 KB Output is correct
46 Correct 499 ms 49688 KB Output is correct
47 Correct 378 ms 50160 KB Output is correct
48 Correct 422 ms 50492 KB Output is correct
49 Correct 451 ms 53480 KB Output is correct
50 Correct 419 ms 55824 KB Output is correct
51 Correct 13 ms 24412 KB Output is correct
52 Correct 305 ms 39740 KB Output is correct
53 Correct 278 ms 39760 KB Output is correct
54 Correct 306 ms 39604 KB Output is correct
55 Correct 273 ms 39760 KB Output is correct
56 Correct 288 ms 39592 KB Output is correct
57 Correct 249 ms 39780 KB Output is correct
58 Correct 266 ms 43368 KB Output is correct
59 Correct 286 ms 43860 KB Output is correct
60 Correct 288 ms 42924 KB Output is correct
61 Correct 343 ms 42776 KB Output is correct
62 Correct 161 ms 56660 KB Output is correct
63 Correct 171 ms 56656 KB Output is correct
64 Correct 164 ms 56660 KB Output is correct
65 Correct 152 ms 56800 KB Output is correct
66 Correct 230 ms 48536 KB Output is correct
67 Correct 221 ms 48328 KB Output is correct
68 Correct 229 ms 48588 KB Output is correct
69 Correct 273 ms 48328 KB Output is correct
70 Correct 272 ms 48588 KB Output is correct
71 Correct 239 ms 48328 KB Output is correct
72 Correct 227 ms 48448 KB Output is correct
73 Correct 244 ms 47472 KB Output is correct
74 Correct 204 ms 48332 KB Output is correct
75 Correct 224 ms 48328 KB Output is correct
76 Correct 328 ms 47112 KB Output is correct
77 Correct 347 ms 45072 KB Output is correct
78 Correct 440 ms 50312 KB Output is correct
79 Correct 403 ms 48200 KB Output is correct
80 Correct 446 ms 55340 KB Output is correct
81 Correct 409 ms 51684 KB Output is correct
82 Correct 428 ms 51812 KB Output is correct
83 Correct 423 ms 48576 KB Output is correct
84 Correct 419 ms 54420 KB Output is correct
85 Correct 413 ms 52876 KB Output is correct
86 Correct 425 ms 48236 KB Output is correct
87 Correct 408 ms 49816 KB Output is correct
88 Correct 380 ms 51028 KB Output is correct
89 Correct 359 ms 46876 KB Output is correct
90 Correct 369 ms 46900 KB Output is correct
91 Correct 341 ms 49208 KB Output is correct
92 Correct 362 ms 47900 KB Output is correct
93 Correct 367 ms 47500 KB Output is correct
94 Correct 355 ms 47012 KB Output is correct
95 Correct 341 ms 48488 KB Output is correct
96 Correct 337 ms 46928 KB Output is correct
97 Correct 362 ms 48980 KB Output is correct