답안 #537572

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
537572 2022-03-15T08:50:21 Z Vladth11 수도 (JOI20_capital_city) C++14
100 / 100
747 ms 42708 KB
#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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 8 ms 9684 KB Output is correct
4 Correct 6 ms 9736 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 8 ms 9684 KB Output is correct
4 Correct 6 ms 9736 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 7 ms 9812 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 7 ms 9812 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 9812 KB Output is correct
17 Correct 8 ms 9940 KB Output is correct
18 Correct 9 ms 9920 KB Output is correct
19 Correct 7 ms 9940 KB Output is correct
20 Correct 7 ms 9964 KB Output is correct
21 Correct 7 ms 9940 KB Output is correct
22 Correct 7 ms 9940 KB Output is correct
23 Correct 8 ms 9940 KB Output is correct
24 Correct 7 ms 9816 KB Output is correct
25 Correct 7 ms 9976 KB Output is correct
26 Correct 7 ms 9992 KB Output is correct
27 Correct 7 ms 9996 KB Output is correct
28 Correct 7 ms 9880 KB Output is correct
29 Correct 7 ms 9940 KB Output is correct
30 Correct 8 ms 9952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 539 ms 38556 KB Output is correct
2 Correct 166 ms 39100 KB Output is correct
3 Correct 567 ms 38240 KB Output is correct
4 Correct 179 ms 38956 KB Output is correct
5 Correct 539 ms 35228 KB Output is correct
6 Correct 180 ms 39416 KB Output is correct
7 Correct 511 ms 35976 KB Output is correct
8 Correct 197 ms 39144 KB Output is correct
9 Correct 605 ms 34100 KB Output is correct
10 Correct 684 ms 31616 KB Output is correct
11 Correct 701 ms 34720 KB Output is correct
12 Correct 672 ms 37140 KB Output is correct
13 Correct 747 ms 31308 KB Output is correct
14 Correct 687 ms 37468 KB Output is correct
15 Correct 656 ms 37156 KB Output is correct
16 Correct 662 ms 32184 KB Output is correct
17 Correct 633 ms 32840 KB Output is correct
18 Correct 683 ms 32952 KB Output is correct
19 Correct 730 ms 36068 KB Output is correct
20 Correct 728 ms 38344 KB Output is correct
21 Correct 6 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 8 ms 9684 KB Output is correct
4 Correct 6 ms 9736 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 7 ms 9812 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 7 ms 9812 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 9812 KB Output is correct
17 Correct 8 ms 9940 KB Output is correct
18 Correct 9 ms 9920 KB Output is correct
19 Correct 7 ms 9940 KB Output is correct
20 Correct 7 ms 9964 KB Output is correct
21 Correct 7 ms 9940 KB Output is correct
22 Correct 7 ms 9940 KB Output is correct
23 Correct 8 ms 9940 KB Output is correct
24 Correct 7 ms 9816 KB Output is correct
25 Correct 7 ms 9976 KB Output is correct
26 Correct 7 ms 9992 KB Output is correct
27 Correct 7 ms 9996 KB Output is correct
28 Correct 7 ms 9880 KB Output is correct
29 Correct 7 ms 9940 KB Output is correct
30 Correct 8 ms 9952 KB Output is correct
31 Correct 539 ms 38556 KB Output is correct
32 Correct 166 ms 39100 KB Output is correct
33 Correct 567 ms 38240 KB Output is correct
34 Correct 179 ms 38956 KB Output is correct
35 Correct 539 ms 35228 KB Output is correct
36 Correct 180 ms 39416 KB Output is correct
37 Correct 511 ms 35976 KB Output is correct
38 Correct 197 ms 39144 KB Output is correct
39 Correct 605 ms 34100 KB Output is correct
40 Correct 684 ms 31616 KB Output is correct
41 Correct 701 ms 34720 KB Output is correct
42 Correct 672 ms 37140 KB Output is correct
43 Correct 747 ms 31308 KB Output is correct
44 Correct 687 ms 37468 KB Output is correct
45 Correct 656 ms 37156 KB Output is correct
46 Correct 662 ms 32184 KB Output is correct
47 Correct 633 ms 32840 KB Output is correct
48 Correct 683 ms 32952 KB Output is correct
49 Correct 730 ms 36068 KB Output is correct
50 Correct 728 ms 38344 KB Output is correct
51 Correct 6 ms 9684 KB Output is correct
52 Correct 528 ms 25244 KB Output is correct
53 Correct 501 ms 25320 KB Output is correct
54 Correct 498 ms 25164 KB Output is correct
55 Correct 526 ms 25292 KB Output is correct
56 Correct 427 ms 25284 KB Output is correct
57 Correct 503 ms 25240 KB Output is correct
58 Correct 443 ms 29236 KB Output is correct
59 Correct 382 ms 29468 KB Output is correct
60 Correct 516 ms 28852 KB Output is correct
61 Correct 576 ms 28412 KB Output is correct
62 Correct 164 ms 42608 KB Output is correct
63 Correct 195 ms 42708 KB Output is correct
64 Correct 182 ms 42408 KB Output is correct
65 Correct 186 ms 42592 KB Output is correct
66 Correct 414 ms 34064 KB Output is correct
67 Correct 308 ms 33924 KB Output is correct
68 Correct 320 ms 34044 KB Output is correct
69 Correct 372 ms 34064 KB Output is correct
70 Correct 421 ms 33896 KB Output is correct
71 Correct 325 ms 33964 KB Output is correct
72 Correct 351 ms 34028 KB Output is correct
73 Correct 338 ms 33092 KB Output is correct
74 Correct 322 ms 34056 KB Output is correct
75 Correct 395 ms 34116 KB Output is correct
76 Correct 506 ms 32748 KB Output is correct
77 Correct 489 ms 30972 KB Output is correct
78 Correct 561 ms 36004 KB Output is correct
79 Correct 611 ms 33844 KB Output is correct
80 Correct 641 ms 40976 KB Output is correct
81 Correct 617 ms 37464 KB Output is correct
82 Correct 580 ms 37608 KB Output is correct
83 Correct 617 ms 34156 KB Output is correct
84 Correct 647 ms 40208 KB Output is correct
85 Correct 678 ms 38624 KB Output is correct
86 Correct 559 ms 33796 KB Output is correct
87 Correct 592 ms 35672 KB Output is correct
88 Correct 506 ms 36764 KB Output is correct
89 Correct 497 ms 32684 KB Output is correct
90 Correct 490 ms 32576 KB Output is correct
91 Correct 539 ms 35148 KB Output is correct
92 Correct 521 ms 33648 KB Output is correct
93 Correct 484 ms 33284 KB Output is correct
94 Correct 512 ms 32656 KB Output is correct
95 Correct 585 ms 34112 KB Output is correct
96 Correct 525 ms 32860 KB Output is correct
97 Correct 536 ms 34928 KB Output is correct