답안 #537566

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
537566 2022-03-15T08:46:26 Z Vladth11 수도 (JOI20_capital_city) C++14
11 / 100
3000 ms 38504 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];
    }
}

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;
            }
            if(x != centroid && added[c[p[x]]] != stamp){
                q.push(c[p[x]]);
                added[c[p[x]]] = stamp;
                cnt++;
            }
        }
    }
    if(ok){
        minim = min(minim, cnt - 1);
    }
    for(auto x : v[node]){
        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 7 ms 9724 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 6 ms 9720 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 8 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9724 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 6 ms 9720 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 8 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9716 KB Output is correct
11 Correct 102 ms 11340 KB Output is correct
12 Correct 110 ms 11404 KB Output is correct
13 Correct 101 ms 11356 KB Output is correct
14 Correct 101 ms 11328 KB Output is correct
15 Correct 54 ms 11424 KB Output is correct
16 Correct 72 ms 11360 KB Output is correct
17 Correct 61 ms 11444 KB Output is correct
18 Correct 73 ms 11408 KB Output is correct
19 Correct 59 ms 11340 KB Output is correct
20 Correct 58 ms 11332 KB Output is correct
21 Correct 73 ms 11436 KB Output is correct
22 Correct 59 ms 11456 KB Output is correct
23 Correct 89 ms 11444 KB Output is correct
24 Correct 63 ms 11444 KB Output is correct
25 Correct 104 ms 11388 KB Output is correct
26 Correct 107 ms 11376 KB Output is correct
27 Correct 104 ms 11424 KB Output is correct
28 Correct 81 ms 11444 KB Output is correct
29 Correct 83 ms 11340 KB Output is correct
30 Correct 82 ms 11372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3051 ms 38504 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9724 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 6 ms 9720 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 8 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9716 KB Output is correct
11 Correct 102 ms 11340 KB Output is correct
12 Correct 110 ms 11404 KB Output is correct
13 Correct 101 ms 11356 KB Output is correct
14 Correct 101 ms 11328 KB Output is correct
15 Correct 54 ms 11424 KB Output is correct
16 Correct 72 ms 11360 KB Output is correct
17 Correct 61 ms 11444 KB Output is correct
18 Correct 73 ms 11408 KB Output is correct
19 Correct 59 ms 11340 KB Output is correct
20 Correct 58 ms 11332 KB Output is correct
21 Correct 73 ms 11436 KB Output is correct
22 Correct 59 ms 11456 KB Output is correct
23 Correct 89 ms 11444 KB Output is correct
24 Correct 63 ms 11444 KB Output is correct
25 Correct 104 ms 11388 KB Output is correct
26 Correct 107 ms 11376 KB Output is correct
27 Correct 104 ms 11424 KB Output is correct
28 Correct 81 ms 11444 KB Output is correct
29 Correct 83 ms 11340 KB Output is correct
30 Correct 82 ms 11372 KB Output is correct
31 Execution timed out 3051 ms 38504 KB Time limit exceeded
32 Halted 0 ms 0 KB -