Submission #428696

# Submission time Handle Problem Language Result Execution time Memory
428696 2021-06-15T13:54:08 Z talant117408 Capital City (JOI20_capital_city) C++17
100 / 100
1002 ms 43584 KB
/*
    Code written by Talant I.D.
*/
 
#include <bits/stdc++.h>
//~ #include <ext/pb_ds/assoc_container.hpp>
 
//~ using namespace __gnu_pbds;
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
//~ typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
 
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
 
const int mod = 1e9+7;
 
ll mode(ll a) {
    while (a < 0) {
        a += mod;
    }
    return a % mod;
}
 
ll subt(ll a, ll b) {
    return mode(mode(a)-mode(b));
}
 
ll add(ll a, ll b) {
    return mode(mode(a)+mode(b));
}
 
ll mult(ll a, ll b) {
    return mode(mode(a)*mode(b));
}
 
ll binpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b&1) res = mult(res, a);
        a = mult(a, a);
        b >>= 1;
    }
    return res;
}

const int N = 2e5+7;
vector <int> graph[N], byColor[N];
int par[N], color[N], ind[N], timer, saizu[N], vis[N];
int ans, root_size, n;

void find_size(int v, int p) {
    saizu[v] = 1;
    for (auto to : graph[v]) {
        if (vis[to] || to == p) continue;
        find_size(to, v);
        saizu[v] += saizu[to];
    }
}

int find_centroid(int v, int p) {
    for (auto to : graph[v]) {
        if (vis[to] || to == p) continue;
        if (saizu[to]*2 > root_size) {
            return find_centroid(to, v);
        }
    }
    return v;
}

void create_tree(int v, int p) {
    ind[v] = timer;
    par[v] = p;
    for (auto to : graph[v]) {
        if (vis[to] || to == p) continue;
        create_tree(to, v);
    }
}

void init_centroid(int u) {
    find_size(u, u);
    root_size = saizu[u];
    int centroid = find_centroid(u, u);
    vis[centroid] = 1;
    create_tree(centroid, centroid);
    
    queue <int> K;
    set <int> colors;
    bool outside = 1;
    colors.insert(color[centroid]);
    for (auto to : byColor[color[centroid]]) {
        K.push(to);
        if (ind[to] != timer) {
            outside = 0;
            break;
        }
    }
    while (sz(K)) {
        if (!outside) {
            break;
        }
        int v = K.front(); K.pop();
        if (ind[v] != timer) {
            outside = 0;
            break;
        }
        if (color[par[v]] != color[v]) {
            auto it = colors.lb(color[par[v]]);
            if (it == colors.end() || *it != color[par[v]]) {
                colors.insert(color[par[v]]);
                for (auto to : byColor[color[par[v]]]) {
                    K.push(to);
                    if (ind[to] != timer) {
                        outside = 0;
                        break;
                    }
                }
            }
        }
    }
    if (outside) {
        ans = min(ans, sz(colors)-1);
    }
    for (auto to : graph[centroid]) {
        if (!vis[to]) {
            timer++;
            init_centroid(to);
        }
    }
}

int main() {
    do_not_disturb
    
    int k;
    cin >> n >> k;
    ans = k+1;
    for (int i = 0; i < n-1; i++) {
        int x, y;
        cin >> x >> y;
        graph[x].pb(y);
        graph[y].pb(x);
    }
    for (int i = 1; i <= n; i++) {
        cin >> color[i];
        byColor[color[i]].pb(i);
    }
    
    init_centroid(1);
    cout << ans;
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 8 ms 9676 KB Output is correct
4 Correct 7 ms 9676 KB Output is correct
5 Correct 9 ms 9724 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 6 ms 9636 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 7 ms 9716 KB Output is correct
10 Correct 7 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 8 ms 9676 KB Output is correct
4 Correct 7 ms 9676 KB Output is correct
5 Correct 9 ms 9724 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 6 ms 9636 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 7 ms 9716 KB Output is correct
10 Correct 7 ms 9676 KB Output is correct
11 Correct 9 ms 9804 KB Output is correct
12 Correct 8 ms 9804 KB Output is correct
13 Correct 8 ms 9844 KB Output is correct
14 Correct 10 ms 9860 KB Output is correct
15 Correct 10 ms 9828 KB Output is correct
16 Correct 9 ms 9804 KB Output is correct
17 Correct 8 ms 9888 KB Output is correct
18 Correct 9 ms 9860 KB Output is correct
19 Correct 7 ms 9932 KB Output is correct
20 Correct 7 ms 9960 KB Output is correct
21 Correct 8 ms 9904 KB Output is correct
22 Correct 8 ms 9932 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 8 ms 9932 KB Output is correct
25 Correct 8 ms 9932 KB Output is correct
26 Correct 8 ms 10060 KB Output is correct
27 Correct 12 ms 10012 KB Output is correct
28 Correct 10 ms 9976 KB Output is correct
29 Correct 9 ms 9968 KB Output is correct
30 Correct 9 ms 9932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 649 ms 38156 KB Output is correct
2 Correct 255 ms 42280 KB Output is correct
3 Correct 797 ms 41504 KB Output is correct
4 Correct 250 ms 42324 KB Output is correct
5 Correct 698 ms 39160 KB Output is correct
6 Correct 245 ms 42392 KB Output is correct
7 Correct 814 ms 40180 KB Output is correct
8 Correct 285 ms 43408 KB Output is correct
9 Correct 865 ms 39420 KB Output is correct
10 Correct 910 ms 36992 KB Output is correct
11 Correct 874 ms 39880 KB Output is correct
12 Correct 904 ms 42512 KB Output is correct
13 Correct 790 ms 36556 KB Output is correct
14 Correct 860 ms 42844 KB Output is correct
15 Correct 894 ms 42460 KB Output is correct
16 Correct 1002 ms 37504 KB Output is correct
17 Correct 894 ms 38020 KB Output is correct
18 Correct 831 ms 38128 KB Output is correct
19 Correct 883 ms 41372 KB Output is correct
20 Correct 951 ms 43584 KB Output is correct
21 Correct 6 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 8 ms 9676 KB Output is correct
4 Correct 7 ms 9676 KB Output is correct
5 Correct 9 ms 9724 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 6 ms 9636 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 7 ms 9716 KB Output is correct
10 Correct 7 ms 9676 KB Output is correct
11 Correct 9 ms 9804 KB Output is correct
12 Correct 8 ms 9804 KB Output is correct
13 Correct 8 ms 9844 KB Output is correct
14 Correct 10 ms 9860 KB Output is correct
15 Correct 10 ms 9828 KB Output is correct
16 Correct 9 ms 9804 KB Output is correct
17 Correct 8 ms 9888 KB Output is correct
18 Correct 9 ms 9860 KB Output is correct
19 Correct 7 ms 9932 KB Output is correct
20 Correct 7 ms 9960 KB Output is correct
21 Correct 8 ms 9904 KB Output is correct
22 Correct 8 ms 9932 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 8 ms 9932 KB Output is correct
25 Correct 8 ms 9932 KB Output is correct
26 Correct 8 ms 10060 KB Output is correct
27 Correct 12 ms 10012 KB Output is correct
28 Correct 10 ms 9976 KB Output is correct
29 Correct 9 ms 9968 KB Output is correct
30 Correct 9 ms 9932 KB Output is correct
31 Correct 649 ms 38156 KB Output is correct
32 Correct 255 ms 42280 KB Output is correct
33 Correct 797 ms 41504 KB Output is correct
34 Correct 250 ms 42324 KB Output is correct
35 Correct 698 ms 39160 KB Output is correct
36 Correct 245 ms 42392 KB Output is correct
37 Correct 814 ms 40180 KB Output is correct
38 Correct 285 ms 43408 KB Output is correct
39 Correct 865 ms 39420 KB Output is correct
40 Correct 910 ms 36992 KB Output is correct
41 Correct 874 ms 39880 KB Output is correct
42 Correct 904 ms 42512 KB Output is correct
43 Correct 790 ms 36556 KB Output is correct
44 Correct 860 ms 42844 KB Output is correct
45 Correct 894 ms 42460 KB Output is correct
46 Correct 1002 ms 37504 KB Output is correct
47 Correct 894 ms 38020 KB Output is correct
48 Correct 831 ms 38128 KB Output is correct
49 Correct 883 ms 41372 KB Output is correct
50 Correct 951 ms 43584 KB Output is correct
51 Correct 6 ms 9728 KB Output is correct
52 Correct 696 ms 26304 KB Output is correct
53 Correct 778 ms 26296 KB Output is correct
54 Correct 745 ms 26228 KB Output is correct
55 Correct 672 ms 26312 KB Output is correct
56 Correct 672 ms 26232 KB Output is correct
57 Correct 610 ms 26304 KB Output is correct
58 Correct 661 ms 30588 KB Output is correct
59 Correct 643 ms 29760 KB Output is correct
60 Correct 740 ms 28700 KB Output is correct
61 Correct 855 ms 28892 KB Output is correct
62 Correct 252 ms 42244 KB Output is correct
63 Correct 247 ms 42300 KB Output is correct
64 Correct 280 ms 43268 KB Output is correct
65 Correct 273 ms 42400 KB Output is correct
66 Correct 429 ms 34240 KB Output is correct
67 Correct 508 ms 34060 KB Output is correct
68 Correct 433 ms 34184 KB Output is correct
69 Correct 450 ms 34208 KB Output is correct
70 Correct 533 ms 34104 KB Output is correct
71 Correct 438 ms 34008 KB Output is correct
72 Correct 461 ms 34036 KB Output is correct
73 Correct 522 ms 33288 KB Output is correct
74 Correct 425 ms 34212 KB Output is correct
75 Correct 512 ms 34124 KB Output is correct
76 Correct 843 ms 34956 KB Output is correct
77 Correct 859 ms 33632 KB Output is correct
78 Correct 822 ms 37940 KB Output is correct
79 Correct 788 ms 36000 KB Output is correct
80 Correct 833 ms 43144 KB Output is correct
81 Correct 903 ms 39492 KB Output is correct
82 Correct 832 ms 39756 KB Output is correct
83 Correct 847 ms 36356 KB Output is correct
84 Correct 856 ms 42288 KB Output is correct
85 Correct 884 ms 40596 KB Output is correct
86 Correct 801 ms 35956 KB Output is correct
87 Correct 761 ms 37668 KB Output is correct
88 Correct 710 ms 37696 KB Output is correct
89 Correct 625 ms 33716 KB Output is correct
90 Correct 699 ms 33564 KB Output is correct
91 Correct 752 ms 35944 KB Output is correct
92 Correct 748 ms 34504 KB Output is correct
93 Correct 693 ms 34260 KB Output is correct
94 Correct 688 ms 33612 KB Output is correct
95 Correct 729 ms 35104 KB Output is correct
96 Correct 665 ms 33740 KB Output is correct
97 Correct 774 ms 35744 KB Output is correct