답안 #240328

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
240328 2020-06-19T10:29:48 Z mhy908 수도 (JOI20_capital_city) C++14
11 / 100
3000 ms 46424 KB
#include <bits/stdc++.h>
#define eb emplace_back
const int inf=987654321;
using namespace std;
typedef long long LL;
int n, k, col[200010], siz[200010];
vector<int> link[200010], vcm[200010];
void dfs(int num, int par){
    siz[num]=1;
    vcm[col[num]].eb(num);
    for(auto i:link[num]){
        if(i==par)continue;
        dfs(i, num);
        siz[num]+=siz[i];
    }
}
bool ch[200010];
int get_centroid(int num, int par, int sz){
    bool flag=true;
    for(auto i:link[num]){
        if(ch[i])continue;
        if(siz[i]>sz/2)flag=false;
    }
    if(flag)return num;
    for(auto i:link[num]){
        if(i==par||ch[i])continue;
        int temp=siz[i];
        siz[num]=sz-siz[i];
        siz[i]=sz;
        int ret=get_centroid(i, num, sz);
        if(ret)return ret;
        siz[num]=sz;
        siz[i]=temp;
    }
    return 0;
}
int ans=inf, p[200010];
vector<int> vc[200010], tmp;
void dfs2(int num, int par){
    p[num]=par;
    tmp.eb(col[num]);
    vc[col[num]].eb(num);
    for(auto i:link[num]){
        if(i==par||ch[i])continue;
        dfs2(i, num);
    }
}
int que[200010], re, fr;
bool ch2[200010];
void make_centroidtree(int num, int par){
    int cen=get_centroid(num, 0, siz[num]), ret=0; re=0;
    ch[cen]=true;
    dfs2(cen, 0);
    if(vc[col[cen]].size()!=vcm[col[cen]].size())ret=inf;
    for(auto i:vc[col[cen]])que[++re]=i;
    ch2[col[cen]]=true;
    for(fr=1; fr<=re; fr++){
        if(ch2[col[p[que[fr]]]])continue;
        ch2[col[p[que[fr]]]]=true;
        if(vc[col[p[que[fr]]]].size()!=vcm[col[p[que[fr]]]].size())ret=inf;
        for(auto i:vc[col[p[que[fr]]]])que[++re]=i;
        ret++;
    }
    ans=min(ans, ret);
    for(auto i:tmp){
        ch2[i]=false;
        vc[i].clear();
    }
    for(auto i:link[cen]){
        if(!ch[i])make_centroidtree(i, cen);
    }
}
int main(){
    scanf("%d %d", &n, &k);
    ch2[0]=true;
    for(int i=1; i<n; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        link[a].eb(b);
        link[b].eb(a);
    }
    for(int i=1; i<=n; i++)scanf("%d", &col[i]);
    dfs(1, 0);
    make_centroidtree(1, 0);
    printf("%d", ans);
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:82:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1; i<=n; i++)scanf("%d", &col[i]);
                            ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 15 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 13 ms 14464 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 15 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 13 ms 14464 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
11 Correct 76 ms 14720 KB Output is correct
12 Correct 74 ms 14720 KB Output is correct
13 Correct 76 ms 14720 KB Output is correct
14 Correct 69 ms 14712 KB Output is correct
15 Correct 95 ms 14840 KB Output is correct
16 Correct 82 ms 14840 KB Output is correct
17 Correct 74 ms 14796 KB Output is correct
18 Correct 72 ms 14720 KB Output is correct
19 Correct 71 ms 14720 KB Output is correct
20 Correct 73 ms 14720 KB Output is correct
21 Correct 73 ms 14720 KB Output is correct
22 Correct 104 ms 14968 KB Output is correct
23 Correct 91 ms 14840 KB Output is correct
24 Correct 96 ms 14844 KB Output is correct
25 Correct 106 ms 14968 KB Output is correct
26 Correct 107 ms 14968 KB Output is correct
27 Correct 105 ms 14840 KB Output is correct
28 Correct 102 ms 14840 KB Output is correct
29 Correct 103 ms 14840 KB Output is correct
30 Correct 102 ms 15072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3077 ms 46424 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 15 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 13 ms 14464 KB Output is correct
10 Correct 13 ms 14464 KB Output is correct
11 Correct 76 ms 14720 KB Output is correct
12 Correct 74 ms 14720 KB Output is correct
13 Correct 76 ms 14720 KB Output is correct
14 Correct 69 ms 14712 KB Output is correct
15 Correct 95 ms 14840 KB Output is correct
16 Correct 82 ms 14840 KB Output is correct
17 Correct 74 ms 14796 KB Output is correct
18 Correct 72 ms 14720 KB Output is correct
19 Correct 71 ms 14720 KB Output is correct
20 Correct 73 ms 14720 KB Output is correct
21 Correct 73 ms 14720 KB Output is correct
22 Correct 104 ms 14968 KB Output is correct
23 Correct 91 ms 14840 KB Output is correct
24 Correct 96 ms 14844 KB Output is correct
25 Correct 106 ms 14968 KB Output is correct
26 Correct 107 ms 14968 KB Output is correct
27 Correct 105 ms 14840 KB Output is correct
28 Correct 102 ms 14840 KB Output is correct
29 Correct 103 ms 14840 KB Output is correct
30 Correct 102 ms 15072 KB Output is correct
31 Execution timed out 3077 ms 46424 KB Time limit exceeded
32 Halted 0 ms 0 KB -