Submission #368117

# Submission time Handle Problem Language Result Execution time Memory
368117 2021-02-19T15:12:21 Z two_sides Capital City (JOI20_capital_city) C++17
100 / 100
819 ms 38764 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 200005;

int siz[N], col[N], cnt[N], par[N];
vector <int> adj[N], own[N]; int res;
bool ban[N], vis[N], mark[N];

void DFS1(int u, int p) {
    siz[u] = 1; mark[col[u]] = 0;
    cnt[col[u]] = vis[u] = 0;
    for (int v : adj[u])
        if (v != p && !ban[v]) {
            DFS1(v, u);
            siz[u] += siz[v];
        }
}

int findCen(int u) {
    int c = u, p = 0;
    while (true) {
        bool flag = false;
        for (int v : adj[c])
            if (v != p && !ban[v] &&
            siz[v] * 2 >= siz[u]) {
                p = c; c = v;
                flag = true; break;
            }
        if (!flag) break;
    }
    return c;
}

void DFS2(int u) {
    cnt[col[u]]++;
    for (int v : adj[u])
        if (v != par[u] && !ban[v]) {
            par[v] = u; DFS2(v);
        }
}

int BFS(int u) {
    int cur = 0;
    queue <int> que; que.push(u);
    while (que.size()) {
        u = que.front(); que.pop();
        if (mark[col[u]]) continue;
        mark[col[u]] = 1; cur++;
        if (cnt[col[u]] !=
        int(own[col[u]].size()))
            return 1000000000;
        for (int v : own[col[u]])
            while (par[v] && !vis[v]) {
                que.push(v); vis[v] = 1;
                v = par[v];
            }
    }
    return cur;
}

void DFS3(int u) {
    DFS1(u, 0); u = findCen(u);
    par[u] = 0; ban[u] = 1; DFS2(u);
    res = min(res, BFS(u));
    for (int v : adj[u])
        if (!ban[v]) DFS3(v);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, k; cin >> n >> k;
    for (int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }
    for (int i = 1; i <= n; i++) {
        cin >> col[i];
        own[col[i]].emplace_back(i);
    }
    res = k; DFS3(1); cout << res - 1;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9836 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9836 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
11 Correct 9 ms 9964 KB Output is correct
12 Correct 10 ms 9964 KB Output is correct
13 Correct 9 ms 9964 KB Output is correct
14 Correct 8 ms 9964 KB Output is correct
15 Correct 8 ms 9964 KB Output is correct
16 Correct 8 ms 9964 KB Output is correct
17 Correct 8 ms 9964 KB Output is correct
18 Correct 8 ms 9964 KB Output is correct
19 Correct 8 ms 9964 KB Output is correct
20 Correct 8 ms 9964 KB Output is correct
21 Correct 8 ms 9964 KB Output is correct
22 Correct 8 ms 10092 KB Output is correct
23 Correct 9 ms 9964 KB Output is correct
24 Correct 9 ms 9964 KB Output is correct
25 Correct 11 ms 9964 KB Output is correct
26 Correct 9 ms 9964 KB Output is correct
27 Correct 9 ms 10092 KB Output is correct
28 Correct 9 ms 9964 KB Output is correct
29 Correct 9 ms 9964 KB Output is correct
30 Correct 9 ms 9964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 614 ms 38124 KB Output is correct
2 Correct 208 ms 38508 KB Output is correct
3 Correct 657 ms 38156 KB Output is correct
4 Correct 212 ms 38636 KB Output is correct
5 Correct 591 ms 35564 KB Output is correct
6 Correct 225 ms 38764 KB Output is correct
7 Correct 606 ms 35836 KB Output is correct
8 Correct 212 ms 38260 KB Output is correct
9 Correct 695 ms 34084 KB Output is correct
10 Correct 689 ms 32108 KB Output is correct
11 Correct 760 ms 34680 KB Output is correct
12 Correct 720 ms 36712 KB Output is correct
13 Correct 706 ms 31852 KB Output is correct
14 Correct 782 ms 36920 KB Output is correct
15 Correct 692 ms 36764 KB Output is correct
16 Correct 819 ms 32684 KB Output is correct
17 Correct 759 ms 33004 KB Output is correct
18 Correct 702 ms 33360 KB Output is correct
19 Correct 703 ms 35972 KB Output is correct
20 Correct 718 ms 37832 KB Output is correct
21 Correct 7 ms 9836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9836 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
11 Correct 9 ms 9964 KB Output is correct
12 Correct 10 ms 9964 KB Output is correct
13 Correct 9 ms 9964 KB Output is correct
14 Correct 8 ms 9964 KB Output is correct
15 Correct 8 ms 9964 KB Output is correct
16 Correct 8 ms 9964 KB Output is correct
17 Correct 8 ms 9964 KB Output is correct
18 Correct 8 ms 9964 KB Output is correct
19 Correct 8 ms 9964 KB Output is correct
20 Correct 8 ms 9964 KB Output is correct
21 Correct 8 ms 9964 KB Output is correct
22 Correct 8 ms 10092 KB Output is correct
23 Correct 9 ms 9964 KB Output is correct
24 Correct 9 ms 9964 KB Output is correct
25 Correct 11 ms 9964 KB Output is correct
26 Correct 9 ms 9964 KB Output is correct
27 Correct 9 ms 10092 KB Output is correct
28 Correct 9 ms 9964 KB Output is correct
29 Correct 9 ms 9964 KB Output is correct
30 Correct 9 ms 9964 KB Output is correct
31 Correct 614 ms 38124 KB Output is correct
32 Correct 208 ms 38508 KB Output is correct
33 Correct 657 ms 38156 KB Output is correct
34 Correct 212 ms 38636 KB Output is correct
35 Correct 591 ms 35564 KB Output is correct
36 Correct 225 ms 38764 KB Output is correct
37 Correct 606 ms 35836 KB Output is correct
38 Correct 212 ms 38260 KB Output is correct
39 Correct 695 ms 34084 KB Output is correct
40 Correct 689 ms 32108 KB Output is correct
41 Correct 760 ms 34680 KB Output is correct
42 Correct 720 ms 36712 KB Output is correct
43 Correct 706 ms 31852 KB Output is correct
44 Correct 782 ms 36920 KB Output is correct
45 Correct 692 ms 36764 KB Output is correct
46 Correct 819 ms 32684 KB Output is correct
47 Correct 759 ms 33004 KB Output is correct
48 Correct 702 ms 33360 KB Output is correct
49 Correct 703 ms 35972 KB Output is correct
50 Correct 718 ms 37832 KB Output is correct
51 Correct 7 ms 9836 KB Output is correct
52 Correct 481 ms 24732 KB Output is correct
53 Correct 542 ms 24712 KB Output is correct
54 Correct 515 ms 24440 KB Output is correct
55 Correct 526 ms 24516 KB Output is correct
56 Correct 525 ms 24648 KB Output is correct
57 Correct 504 ms 24684 KB Output is correct
58 Correct 538 ms 27500 KB Output is correct
59 Correct 547 ms 28012 KB Output is correct
60 Correct 633 ms 27372 KB Output is correct
61 Correct 611 ms 27120 KB Output is correct
62 Correct 214 ms 38636 KB Output is correct
63 Correct 213 ms 38508 KB Output is correct
64 Correct 210 ms 38380 KB Output is correct
65 Correct 212 ms 38636 KB Output is correct
66 Correct 376 ms 31716 KB Output is correct
67 Correct 403 ms 31844 KB Output is correct
68 Correct 379 ms 31716 KB Output is correct
69 Correct 397 ms 31860 KB Output is correct
70 Correct 392 ms 31588 KB Output is correct
71 Correct 396 ms 31716 KB Output is correct
72 Correct 380 ms 31588 KB Output is correct
73 Correct 381 ms 31296 KB Output is correct
74 Correct 414 ms 31844 KB Output is correct
75 Correct 405 ms 31864 KB Output is correct
76 Correct 605 ms 30700 KB Output is correct
77 Correct 578 ms 29164 KB Output is correct
78 Correct 697 ms 33132 KB Output is correct
79 Correct 704 ms 31560 KB Output is correct
80 Correct 741 ms 37252 KB Output is correct
81 Correct 740 ms 34284 KB Output is correct
82 Correct 743 ms 34384 KB Output is correct
83 Correct 734 ms 31764 KB Output is correct
84 Correct 731 ms 36520 KB Output is correct
85 Correct 745 ms 35384 KB Output is correct
86 Correct 737 ms 31340 KB Output is correct
87 Correct 695 ms 32748 KB Output is correct
88 Correct 591 ms 33684 KB Output is correct
89 Correct 586 ms 30380 KB Output is correct
90 Correct 620 ms 30468 KB Output is correct
91 Correct 606 ms 32484 KB Output is correct
92 Correct 603 ms 31292 KB Output is correct
93 Correct 600 ms 30828 KB Output is correct
94 Correct 635 ms 30484 KB Output is correct
95 Correct 633 ms 31772 KB Output is correct
96 Correct 614 ms 30624 KB Output is correct
97 Correct 587 ms 32176 KB Output is correct