Submission #1004849

# Submission time Handle Problem Language Result Execution time Memory
1004849 2024-06-21T19:13:15 Z Unforgettablepl Capital City (JOI20_capital_city) C++17
100 / 100
350 ms 45204 KB
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define int long long

vector<int> adj[200001];
bool processed[200001];
int siz[200001];
bool current[200001];
int par[200001];
int visitedcol[200001];
int col[200001];
vector<int> nodes[200001];

void set_all(int x,int p,bool set_val){
    if(processed[x])return;
    current[x] = set_val;
    par[x] = p;
    for(int&i:adj[x])if(i!=p)set_all(i,x,set_val);
}

int cnt(int x,int p){
    if(processed[x])return 0;
    int ans = 1;
    for(int&i:adj[x])if(i!=p)ans+=cnt(i,x);
    return ans;
}

int centroid;

int centroidcalc(int x,int p,int tot){
    if(processed[x])return 0;
    siz[x] = 1;
    bool works = true;
    for(int&i:adj[x])if(i!=p){
        int res = centroidcalc(i,x,tot);
        siz[x]+=res;
        if(res>tot/2)works=false;
    }
    if(tot-siz[x]>tot/2)works=false;
    if(works){
        centroid = x;
        return 1e15;
    }
    return siz[x];
}

int calc(int x){
    centroidcalc(x,0,cnt(x,0));
    set_all(centroid,0,true); // Get them into processing stage
    queue<int> q; // Queue of colours
    q.emplace(col[centroid]);
    int ans = -1;
    while(!q.empty()){
        int curr = q.front();q.pop();
        if(visitedcol[curr]==centroid)continue;
        visitedcol[curr]=centroid;
        ans++;
        for(int&i:nodes[curr]){
            if(!current[i]){
                while(!q.empty())q.pop();
                ans = 1e15;
                break;
            }
            if(par[i])q.emplace(col[par[i]]);
        }
    }
    set_all(centroid,0,false);
    processed[centroid]=true;
    for(int&i:adj[centroid])if(!processed[i])ans=min(ans,calc(i));
    return ans;
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,k;
    cin >> n >> k;
    for(int i=1;i<n;i++){
        int a,b;cin>>a>>b;
        adj[a].emplace_back(b);
        adj[b].emplace_back(a);
    }
    for(int i=1;i<=n;i++){
        cin>>col[i];
        nodes[col[i]].emplace_back(i);
    }
    siz[1]=n;
    cout << calc(1) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16476 KB Output is correct
2 Correct 3 ms 16472 KB Output is correct
3 Correct 2 ms 16476 KB Output is correct
4 Correct 2 ms 16476 KB Output is correct
5 Correct 3 ms 16476 KB Output is correct
6 Correct 2 ms 16476 KB Output is correct
7 Correct 2 ms 16476 KB Output is correct
8 Correct 2 ms 16476 KB Output is correct
9 Correct 2 ms 16476 KB Output is correct
10 Correct 2 ms 16472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16476 KB Output is correct
2 Correct 3 ms 16472 KB Output is correct
3 Correct 2 ms 16476 KB Output is correct
4 Correct 2 ms 16476 KB Output is correct
5 Correct 3 ms 16476 KB Output is correct
6 Correct 2 ms 16476 KB Output is correct
7 Correct 2 ms 16476 KB Output is correct
8 Correct 2 ms 16476 KB Output is correct
9 Correct 2 ms 16476 KB Output is correct
10 Correct 2 ms 16472 KB Output is correct
11 Correct 4 ms 16472 KB Output is correct
12 Correct 4 ms 16476 KB Output is correct
13 Correct 4 ms 16476 KB Output is correct
14 Correct 4 ms 16568 KB Output is correct
15 Correct 4 ms 16584 KB Output is correct
16 Correct 4 ms 16476 KB Output is correct
17 Correct 3 ms 16676 KB Output is correct
18 Correct 3 ms 16476 KB Output is correct
19 Correct 4 ms 16476 KB Output is correct
20 Correct 3 ms 16476 KB Output is correct
21 Correct 4 ms 16476 KB Output is correct
22 Correct 4 ms 16732 KB Output is correct
23 Correct 4 ms 16676 KB Output is correct
24 Correct 3 ms 16604 KB Output is correct
25 Correct 4 ms 16476 KB Output is correct
26 Correct 4 ms 16732 KB Output is correct
27 Correct 4 ms 16476 KB Output is correct
28 Correct 4 ms 16688 KB Output is correct
29 Correct 4 ms 16476 KB Output is correct
30 Correct 4 ms 16476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 280 ms 40788 KB Output is correct
2 Correct 148 ms 45140 KB Output is correct
3 Correct 289 ms 44372 KB Output is correct
4 Correct 151 ms 45004 KB Output is correct
5 Correct 278 ms 41428 KB Output is correct
6 Correct 137 ms 45188 KB Output is correct
7 Correct 285 ms 41936 KB Output is correct
8 Correct 144 ms 45028 KB Output is correct
9 Correct 335 ms 40732 KB Output is correct
10 Correct 348 ms 38228 KB Output is correct
11 Correct 318 ms 41040 KB Output is correct
12 Correct 350 ms 43600 KB Output is correct
13 Correct 318 ms 37712 KB Output is correct
14 Correct 303 ms 44072 KB Output is correct
15 Correct 314 ms 43600 KB Output is correct
16 Correct 301 ms 38788 KB Output is correct
17 Correct 332 ms 39252 KB Output is correct
18 Correct 296 ms 39612 KB Output is correct
19 Correct 336 ms 42576 KB Output is correct
20 Correct 299 ms 44732 KB Output is correct
21 Correct 3 ms 16476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16476 KB Output is correct
2 Correct 3 ms 16472 KB Output is correct
3 Correct 2 ms 16476 KB Output is correct
4 Correct 2 ms 16476 KB Output is correct
5 Correct 3 ms 16476 KB Output is correct
6 Correct 2 ms 16476 KB Output is correct
7 Correct 2 ms 16476 KB Output is correct
8 Correct 2 ms 16476 KB Output is correct
9 Correct 2 ms 16476 KB Output is correct
10 Correct 2 ms 16472 KB Output is correct
11 Correct 4 ms 16472 KB Output is correct
12 Correct 4 ms 16476 KB Output is correct
13 Correct 4 ms 16476 KB Output is correct
14 Correct 4 ms 16568 KB Output is correct
15 Correct 4 ms 16584 KB Output is correct
16 Correct 4 ms 16476 KB Output is correct
17 Correct 3 ms 16676 KB Output is correct
18 Correct 3 ms 16476 KB Output is correct
19 Correct 4 ms 16476 KB Output is correct
20 Correct 3 ms 16476 KB Output is correct
21 Correct 4 ms 16476 KB Output is correct
22 Correct 4 ms 16732 KB Output is correct
23 Correct 4 ms 16676 KB Output is correct
24 Correct 3 ms 16604 KB Output is correct
25 Correct 4 ms 16476 KB Output is correct
26 Correct 4 ms 16732 KB Output is correct
27 Correct 4 ms 16476 KB Output is correct
28 Correct 4 ms 16688 KB Output is correct
29 Correct 4 ms 16476 KB Output is correct
30 Correct 4 ms 16476 KB Output is correct
31 Correct 280 ms 40788 KB Output is correct
32 Correct 148 ms 45140 KB Output is correct
33 Correct 289 ms 44372 KB Output is correct
34 Correct 151 ms 45004 KB Output is correct
35 Correct 278 ms 41428 KB Output is correct
36 Correct 137 ms 45188 KB Output is correct
37 Correct 285 ms 41936 KB Output is correct
38 Correct 144 ms 45028 KB Output is correct
39 Correct 335 ms 40732 KB Output is correct
40 Correct 348 ms 38228 KB Output is correct
41 Correct 318 ms 41040 KB Output is correct
42 Correct 350 ms 43600 KB Output is correct
43 Correct 318 ms 37712 KB Output is correct
44 Correct 303 ms 44072 KB Output is correct
45 Correct 314 ms 43600 KB Output is correct
46 Correct 301 ms 38788 KB Output is correct
47 Correct 332 ms 39252 KB Output is correct
48 Correct 296 ms 39612 KB Output is correct
49 Correct 336 ms 42576 KB Output is correct
50 Correct 299 ms 44732 KB Output is correct
51 Correct 3 ms 16476 KB Output is correct
52 Correct 196 ms 31208 KB Output is correct
53 Correct 214 ms 31248 KB Output is correct
54 Correct 211 ms 31312 KB Output is correct
55 Correct 211 ms 31316 KB Output is correct
56 Correct 210 ms 31316 KB Output is correct
57 Correct 205 ms 31080 KB Output is correct
58 Correct 195 ms 33616 KB Output is correct
59 Correct 188 ms 34012 KB Output is correct
60 Correct 237 ms 32592 KB Output is correct
61 Correct 212 ms 32336 KB Output is correct
62 Correct 128 ms 45140 KB Output is correct
63 Correct 138 ms 45140 KB Output is correct
64 Correct 136 ms 45012 KB Output is correct
65 Correct 133 ms 45204 KB Output is correct
66 Correct 153 ms 38064 KB Output is correct
67 Correct 177 ms 38004 KB Output is correct
68 Correct 154 ms 38088 KB Output is correct
69 Correct 153 ms 38084 KB Output is correct
70 Correct 157 ms 38084 KB Output is correct
71 Correct 155 ms 37980 KB Output is correct
72 Correct 160 ms 38340 KB Output is correct
73 Correct 169 ms 37192 KB Output is correct
74 Correct 150 ms 38056 KB Output is correct
75 Correct 155 ms 38084 KB Output is correct
76 Correct 240 ms 36808 KB Output is correct
77 Correct 220 ms 34860 KB Output is correct
78 Correct 322 ms 38996 KB Output is correct
79 Correct 320 ms 37200 KB Output is correct
80 Correct 332 ms 44320 KB Output is correct
81 Correct 334 ms 40980 KB Output is correct
82 Correct 298 ms 40824 KB Output is correct
83 Correct 317 ms 37636 KB Output is correct
84 Correct 311 ms 43344 KB Output is correct
85 Correct 328 ms 41812 KB Output is correct
86 Correct 319 ms 37096 KB Output is correct
87 Correct 329 ms 38996 KB Output is correct
88 Correct 295 ms 40532 KB Output is correct
89 Correct 286 ms 36756 KB Output is correct
90 Correct 259 ms 36384 KB Output is correct
91 Correct 299 ms 38856 KB Output is correct
92 Correct 284 ms 37152 KB Output is correct
93 Correct 324 ms 37200 KB Output is correct
94 Correct 268 ms 36432 KB Output is correct
95 Correct 294 ms 37972 KB Output is correct
96 Correct 262 ms 36436 KB Output is correct
97 Correct 256 ms 38740 KB Output is correct