Submission #829450

# Submission time Handle Problem Language Result Execution time Memory
829450 2023-08-18T10:51:18 Z Cookie Capital City (JOI20_capital_city) C++14
100 / 100
507 ms 32956 KB
#include<bits/stdc++.h>
#include<fstream>
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
//ifstream fin("FEEDING.INP");
//ofstream fout("FEEDING.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>

const ll mod = 1e9 + 7;
const int mxn = 2e5 + 5, mxr = 25e3, sq = 500, mxv = 1e6 + 5, mxvv = 130, pr = 31, inf = 1e9;
int n, k;
int sz[mxn + 1];
vt<int>adj[mxn + 1], tocol[mxn + 1];
int pa[mxn + 1], a[mxn + 1], cnt[mxn + 1], ans = inf;
bool vis[mxn + 1], vis2[mxn + 1];
vt<int>comp;
int dfs(int s, int pre){
    
    sz[s] = 1;
    for(auto i: adj[s]){
        if(i != pre && !vis[i]){
            sz[s] += dfs(i, s);
        }
    }
    return(sz[s]);
}
int centroid(int s, int pre, int need){
    for(auto i: adj[s]){
        if(i != pre && !vis[i]){
            if(sz[i] * 2 > need)return(centroid(i, s, need));
        }
    }
    return(s);
}
void go(int s, int pre){
    pa[s] = pre; comp.pb(s);
    for(auto i: adj[s]){
        if(i != pre && !vis[i]){
            go(i, s);
        }
    }
}
void solve(int s){
    comp.clear();
    int c = centroid(s, -1, dfs(s, -1));
    go(c, -1);
   
    vis[c] = 1;
    
    for(auto i: comp){
        tocol[a[i]].pb(i);
    }
    queue<int>q;
    vis2[a[c]] = 1;
    q.push(a[c]);
    int need = 1; bool ok = 1;
    while(!q.empty()){
        int nw = q.front(); q.pop();
        if(cnt[nw] != sz(tocol[nw])){
            ok = 0; break;
        }
        for(auto i: tocol[nw]){
            int to = pa[i];
            if(to != -1){
                if(!vis2[a[to]]){
                    vis2[a[to]] = 1;  need++;
                    q.push(a[to]);
                
                }
            }
        }
    }
    for(auto i: comp){
        tocol[a[i]].clear(); vis2[a[i]] = 0; 
    }
    if(ok)ans = min(ans, need - 1);
    for(auto i: adj[c]){
        if(!vis[i]){
            solve(i);
        }
    }
    
}
int main(){
     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> k;
    for(int i = 1; i < n; i++){
        int u, v; cin >> u >> v;
        adj[u].pb(v); adj[v].pb(u);
    }
    for(int i = 1; i <= n; i++){
        cin >> a[i]; cnt[a[i]]++;
    }
    solve(1);
    cout << ans;
    return(0);
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9720 KB Output is correct
4 Correct 5 ms 9720 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 6 ms 9720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9720 KB Output is correct
4 Correct 5 ms 9720 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 6 ms 9720 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 5 ms 9864 KB Output is correct
13 Correct 7 ms 9812 KB Output is correct
14 Correct 5 ms 9812 KB Output is correct
15 Correct 5 ms 9912 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 6 ms 9912 KB Output is correct
18 Correct 5 ms 9812 KB Output is correct
19 Correct 5 ms 9868 KB Output is correct
20 Correct 7 ms 9812 KB Output is correct
21 Correct 6 ms 9812 KB Output is correct
22 Correct 6 ms 9940 KB Output is correct
23 Correct 8 ms 9840 KB Output is correct
24 Correct 6 ms 9812 KB Output is correct
25 Correct 6 ms 9812 KB Output is correct
26 Correct 6 ms 9940 KB Output is correct
27 Correct 8 ms 9920 KB Output is correct
28 Correct 6 ms 9812 KB Output is correct
29 Correct 6 ms 9812 KB Output is correct
30 Correct 6 ms 9812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 277 ms 32620 KB Output is correct
2 Correct 143 ms 32956 KB Output is correct
3 Correct 286 ms 32520 KB Output is correct
4 Correct 166 ms 32824 KB Output is correct
5 Correct 291 ms 31200 KB Output is correct
6 Correct 142 ms 32824 KB Output is correct
7 Correct 386 ms 31216 KB Output is correct
8 Correct 148 ms 32612 KB Output is correct
9 Correct 353 ms 29836 KB Output is correct
10 Correct 370 ms 28860 KB Output is correct
11 Correct 357 ms 30032 KB Output is correct
12 Correct 341 ms 31076 KB Output is correct
13 Correct 337 ms 28624 KB Output is correct
14 Correct 348 ms 31212 KB Output is correct
15 Correct 383 ms 31092 KB Output is correct
16 Correct 361 ms 29100 KB Output is correct
17 Correct 361 ms 29356 KB Output is correct
18 Correct 331 ms 29376 KB Output is correct
19 Correct 411 ms 30632 KB Output is correct
20 Correct 404 ms 31532 KB Output is correct
21 Correct 4 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9720 KB Output is correct
4 Correct 5 ms 9720 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 6 ms 9720 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 5 ms 9864 KB Output is correct
13 Correct 7 ms 9812 KB Output is correct
14 Correct 5 ms 9812 KB Output is correct
15 Correct 5 ms 9912 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 6 ms 9912 KB Output is correct
18 Correct 5 ms 9812 KB Output is correct
19 Correct 5 ms 9868 KB Output is correct
20 Correct 7 ms 9812 KB Output is correct
21 Correct 6 ms 9812 KB Output is correct
22 Correct 6 ms 9940 KB Output is correct
23 Correct 8 ms 9840 KB Output is correct
24 Correct 6 ms 9812 KB Output is correct
25 Correct 6 ms 9812 KB Output is correct
26 Correct 6 ms 9940 KB Output is correct
27 Correct 8 ms 9920 KB Output is correct
28 Correct 6 ms 9812 KB Output is correct
29 Correct 6 ms 9812 KB Output is correct
30 Correct 6 ms 9812 KB Output is correct
31 Correct 277 ms 32620 KB Output is correct
32 Correct 143 ms 32956 KB Output is correct
33 Correct 286 ms 32520 KB Output is correct
34 Correct 166 ms 32824 KB Output is correct
35 Correct 291 ms 31200 KB Output is correct
36 Correct 142 ms 32824 KB Output is correct
37 Correct 386 ms 31216 KB Output is correct
38 Correct 148 ms 32612 KB Output is correct
39 Correct 353 ms 29836 KB Output is correct
40 Correct 370 ms 28860 KB Output is correct
41 Correct 357 ms 30032 KB Output is correct
42 Correct 341 ms 31076 KB Output is correct
43 Correct 337 ms 28624 KB Output is correct
44 Correct 348 ms 31212 KB Output is correct
45 Correct 383 ms 31092 KB Output is correct
46 Correct 361 ms 29100 KB Output is correct
47 Correct 361 ms 29356 KB Output is correct
48 Correct 331 ms 29376 KB Output is correct
49 Correct 411 ms 30632 KB Output is correct
50 Correct 404 ms 31532 KB Output is correct
51 Correct 4 ms 9684 KB Output is correct
52 Correct 253 ms 24720 KB Output is correct
53 Correct 261 ms 24652 KB Output is correct
54 Correct 281 ms 24692 KB Output is correct
55 Correct 255 ms 24752 KB Output is correct
56 Correct 298 ms 24612 KB Output is correct
57 Correct 265 ms 24712 KB Output is correct
58 Correct 267 ms 26940 KB Output is correct
59 Correct 223 ms 26944 KB Output is correct
60 Correct 281 ms 27136 KB Output is correct
61 Correct 279 ms 26932 KB Output is correct
62 Correct 142 ms 32832 KB Output is correct
63 Correct 144 ms 32784 KB Output is correct
64 Correct 142 ms 32564 KB Output is correct
65 Correct 142 ms 32784 KB Output is correct
66 Correct 181 ms 29128 KB Output is correct
67 Correct 182 ms 29032 KB Output is correct
68 Correct 222 ms 29088 KB Output is correct
69 Correct 173 ms 29140 KB Output is correct
70 Correct 188 ms 28992 KB Output is correct
71 Correct 174 ms 28996 KB Output is correct
72 Correct 189 ms 29008 KB Output is correct
73 Correct 178 ms 28736 KB Output is correct
74 Correct 174 ms 29120 KB Output is correct
75 Correct 185 ms 29112 KB Output is correct
76 Correct 237 ms 28780 KB Output is correct
77 Correct 280 ms 28072 KB Output is correct
78 Correct 343 ms 29248 KB Output is correct
79 Correct 394 ms 28580 KB Output is correct
80 Correct 406 ms 31296 KB Output is correct
81 Correct 386 ms 29880 KB Output is correct
82 Correct 343 ms 29924 KB Output is correct
83 Correct 385 ms 28628 KB Output is correct
84 Correct 416 ms 31016 KB Output is correct
85 Correct 379 ms 30336 KB Output is correct
86 Correct 507 ms 28588 KB Output is correct
87 Correct 446 ms 29116 KB Output is correct
88 Correct 376 ms 29628 KB Output is correct
89 Correct 349 ms 28128 KB Output is correct
90 Correct 339 ms 27892 KB Output is correct
91 Correct 372 ms 28932 KB Output is correct
92 Correct 347 ms 28412 KB Output is correct
93 Correct 400 ms 28292 KB Output is correct
94 Correct 305 ms 27964 KB Output is correct
95 Correct 392 ms 28548 KB Output is correct
96 Correct 316 ms 28104 KB Output is correct
97 Correct 340 ms 28920 KB Output is correct