Submission #537577

# Submission time Handle Problem Language Result Execution time Memory
537577 2022-03-15T08:52:12 Z Vladth11 Capital City (JOI20_capital_city) C++14
100 / 100
620 ms 39164 KB
/// Asta e cea mai clean + ultima sa fie de 100
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "

using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <long double, pii> muchie;

const ll NMAX = 200001;
const ll VMAX = 1000001;
const ll INF = (1LL << 60);
const ll MOD = 1000000007;
const ll BLOCK = 1000000;
const ll nr_of_bits = 21;

vector <int> v[NMAX], colors[NMAX];

int c[NMAX];
int viz[NMAX];
int instance[NMAX];
int stamp;
int added[NMAX];
int p[NMAX];
int sz[NMAX];
int minim = 2e9;
int total = 0;

void getSize(int node, int p){
    sz[node] = 1;
    for(auto x : v[node]){
        if(viz[x] || x == p) continue;
        getSize(x, node);
        sz[node] += sz[x];
    }
    total = sz[node];
}

int findCentroid(int node, int p){
    for(auto x : v[node]){
        if(x == p || viz[x]) continue;
        if(sz[x] > total / 2){
            return findCentroid(x, node);
        }
    }
    return node;
}

int getCentroid(int node){
    getSize(node, 0);
    return findCentroid(node, 0);
}

void DFS(int node, int parent){
    p[node] = parent;
    instance[node] = stamp;
    for(auto x : v[node]){
        if(x == parent) continue;
        if(viz[x]) continue;
        DFS(x, node);
    }
}

void Centroid(int node){
    int centroid = getCentroid(node);
    viz[centroid] = 1;
    stamp++;
    instance[centroid] = stamp;
    for(auto x : v[centroid]){
        if(viz[x])
            continue;
        DFS(x, centroid);
    }
    queue <int> q;
    int ok = 1;
    q.push(c[centroid]);
    added[c[centroid]] = stamp;
    int cnt = 1;
    while(q.size()){
        int color = q.front();
        q.pop();
        for(auto x : colors[color]){
            if(instance[x] != stamp){
                ok = 0;
                break;
            }
            if(x != centroid && added[c[p[x]]] != stamp){
                q.push(c[p[x]]);
                added[c[p[x]]] = stamp;
                cnt++;
            }
        }
        if(!ok)
            break;
    }
    if(ok){
        minim = min(minim, cnt - 1);
    }
    for(auto x : v[centroid]){
        if(viz[x]) continue;
        Centroid(x);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, i, k;
    cin >> n >> k;
    for(i = 2; i <= n; i++){
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    for(i = 1; i <= n; i++){
        cin >> c[i];
        colors[c[i]].push_back(i);
    }
    Centroid(1);
    cout << minim;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9744 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9744 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9824 KB Output is correct
12 Correct 7 ms 9816 KB Output is correct
13 Correct 7 ms 9884 KB Output is correct
14 Correct 7 ms 9812 KB Output is correct
15 Correct 7 ms 9812 KB Output is correct
16 Correct 7 ms 9896 KB Output is correct
17 Correct 9 ms 9940 KB Output is correct
18 Correct 9 ms 9940 KB Output is correct
19 Correct 7 ms 9940 KB Output is correct
20 Correct 8 ms 9940 KB Output is correct
21 Correct 7 ms 9960 KB Output is correct
22 Correct 9 ms 9940 KB Output is correct
23 Correct 9 ms 9940 KB Output is correct
24 Correct 7 ms 9912 KB Output is correct
25 Correct 8 ms 9940 KB Output is correct
26 Correct 7 ms 10016 KB Output is correct
27 Correct 7 ms 9940 KB Output is correct
28 Correct 7 ms 9940 KB Output is correct
29 Correct 9 ms 9940 KB Output is correct
30 Correct 8 ms 9896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 586 ms 38560 KB Output is correct
2 Correct 178 ms 39032 KB Output is correct
3 Correct 515 ms 38244 KB Output is correct
4 Correct 168 ms 38944 KB Output is correct
5 Correct 553 ms 35236 KB Output is correct
6 Correct 173 ms 38928 KB Output is correct
7 Correct 474 ms 35576 KB Output is correct
8 Correct 156 ms 38512 KB Output is correct
9 Correct 517 ms 33676 KB Output is correct
10 Correct 590 ms 31152 KB Output is correct
11 Correct 539 ms 34184 KB Output is correct
12 Correct 528 ms 36600 KB Output is correct
13 Correct 535 ms 30680 KB Output is correct
14 Correct 613 ms 37056 KB Output is correct
15 Correct 539 ms 36856 KB Output is correct
16 Correct 559 ms 31760 KB Output is correct
17 Correct 558 ms 32332 KB Output is correct
18 Correct 620 ms 32532 KB Output is correct
19 Correct 577 ms 35652 KB Output is correct
20 Correct 552 ms 37960 KB Output is correct
21 Correct 7 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9744 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9824 KB Output is correct
12 Correct 7 ms 9816 KB Output is correct
13 Correct 7 ms 9884 KB Output is correct
14 Correct 7 ms 9812 KB Output is correct
15 Correct 7 ms 9812 KB Output is correct
16 Correct 7 ms 9896 KB Output is correct
17 Correct 9 ms 9940 KB Output is correct
18 Correct 9 ms 9940 KB Output is correct
19 Correct 7 ms 9940 KB Output is correct
20 Correct 8 ms 9940 KB Output is correct
21 Correct 7 ms 9960 KB Output is correct
22 Correct 9 ms 9940 KB Output is correct
23 Correct 9 ms 9940 KB Output is correct
24 Correct 7 ms 9912 KB Output is correct
25 Correct 8 ms 9940 KB Output is correct
26 Correct 7 ms 10016 KB Output is correct
27 Correct 7 ms 9940 KB Output is correct
28 Correct 7 ms 9940 KB Output is correct
29 Correct 9 ms 9940 KB Output is correct
30 Correct 8 ms 9896 KB Output is correct
31 Correct 586 ms 38560 KB Output is correct
32 Correct 178 ms 39032 KB Output is correct
33 Correct 515 ms 38244 KB Output is correct
34 Correct 168 ms 38944 KB Output is correct
35 Correct 553 ms 35236 KB Output is correct
36 Correct 173 ms 38928 KB Output is correct
37 Correct 474 ms 35576 KB Output is correct
38 Correct 156 ms 38512 KB Output is correct
39 Correct 517 ms 33676 KB Output is correct
40 Correct 590 ms 31152 KB Output is correct
41 Correct 539 ms 34184 KB Output is correct
42 Correct 528 ms 36600 KB Output is correct
43 Correct 535 ms 30680 KB Output is correct
44 Correct 613 ms 37056 KB Output is correct
45 Correct 539 ms 36856 KB Output is correct
46 Correct 559 ms 31760 KB Output is correct
47 Correct 558 ms 32332 KB Output is correct
48 Correct 620 ms 32532 KB Output is correct
49 Correct 577 ms 35652 KB Output is correct
50 Correct 552 ms 37960 KB Output is correct
51 Correct 7 ms 9684 KB Output is correct
52 Correct 419 ms 21772 KB Output is correct
53 Correct 466 ms 21772 KB Output is correct
54 Correct 423 ms 21768 KB Output is correct
55 Correct 433 ms 21732 KB Output is correct
56 Correct 418 ms 21740 KB Output is correct
57 Correct 379 ms 21772 KB Output is correct
58 Correct 407 ms 25460 KB Output is correct
59 Correct 360 ms 25804 KB Output is correct
60 Correct 425 ms 25092 KB Output is correct
61 Correct 473 ms 24860 KB Output is correct
62 Correct 169 ms 39164 KB Output is correct
63 Correct 167 ms 38944 KB Output is correct
64 Correct 172 ms 38732 KB Output is correct
65 Correct 174 ms 38960 KB Output is correct
66 Correct 302 ms 30328 KB Output is correct
67 Correct 293 ms 30148 KB Output is correct
68 Correct 301 ms 30392 KB Output is correct
69 Correct 290 ms 30284 KB Output is correct
70 Correct 365 ms 30340 KB Output is correct
71 Correct 310 ms 30332 KB Output is correct
72 Correct 333 ms 30384 KB Output is correct
73 Correct 362 ms 29444 KB Output is correct
74 Correct 287 ms 30336 KB Output is correct
75 Correct 316 ms 30404 KB Output is correct
76 Correct 501 ms 29080 KB Output is correct
77 Correct 492 ms 27348 KB Output is correct
78 Correct 548 ms 32280 KB Output is correct
79 Correct 519 ms 30208 KB Output is correct
80 Correct 565 ms 37436 KB Output is correct
81 Correct 535 ms 33840 KB Output is correct
82 Correct 536 ms 33968 KB Output is correct
83 Correct 610 ms 30604 KB Output is correct
84 Correct 552 ms 36576 KB Output is correct
85 Correct 536 ms 35020 KB Output is correct
86 Correct 585 ms 30064 KB Output is correct
87 Correct 542 ms 32044 KB Output is correct
88 Correct 445 ms 33060 KB Output is correct
89 Correct 458 ms 29048 KB Output is correct
90 Correct 508 ms 28908 KB Output is correct
91 Correct 463 ms 31468 KB Output is correct
92 Correct 500 ms 29896 KB Output is correct
93 Correct 435 ms 29568 KB Output is correct
94 Correct 457 ms 29024 KB Output is correct
95 Correct 465 ms 30464 KB Output is correct
96 Correct 502 ms 29244 KB Output is correct
97 Correct 472 ms 31276 KB Output is correct