답안 #873934

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
873934 2023-11-16T03:18:37 Z Darren0724 수도 (JOI20_capital_city) C++17
1 / 100
479 ms 42844 KB
#include <bits/stdc++.h>
using namespace std;
const int INF=1e9;
const int N=200005;
int n,m;
int ans=INF;
vector<int> c(N),sz(N),adj[N],can(N),no(N),pa(N),vis(N),c1[N],inq(N),noc(N);
void dfs(int k){
    sz[k]=1;
    for(int j:adj[k]){
        if(j==pa[k]||no[j])continue;
        pa[j]=k;
        dfs(j);
        sz[k]+=sz[j];
    }
}
int get_cen(int k,int goal){
    for(int j:adj[k]){
        if(j==pa[k]||no[j])continue;
        if(sz[j]>goal){
            return get_cen(j,goal);
        }
    }
    return k;
}
void solve(int p){
    pa[p]=p;
    dfs(p);
    int cen=get_cen(p,sz[p]/2);
    pa[cen]=cen;
    dfs(cen);
    queue<int> q;
    int flag=1,cnt=0;
    if(!noc[c[cen]]){
        vector<int> vis1,inq1;
        vis1.push_back(cen);
        vis[cen]=1;
        q.push(c[cen]);
        inq[c[cen]]=1;
        noc[c[cen]]=1;
        while(q.size()){
            int p=q.front();
            q.pop();
            //cout<<"p:"<<p<<endl;
            cnt++;
            for(int j:c1[p]){
                int t=j;
                while(!vis[t]){
                    //cout<<"t:"<<t<<' '<<c[t]<<' '<<noc[c[t]]<<endl;
                    if(!noc[c[t]]){
                        noc[c[t]]=1;
                        inq[c[t]]=1;
                        inq1.push_back(c[t]);
                        q.push(c[t]);
                    }
                    else if(inq[c[t]]==0){
                        flag=0;
                    }
                    vis[t]=1;
                    vis1.push_back(t);
                    t=pa[t];
                }
            }
        }
        for(int j:vis1){
            vis[j]=0;
        }
        for(int j:inq1){
            inq[j]=0;
        }
        //cout<<"flag:"<<flag<<' '<<cnt<<endl;
        if(flag){
            ans=min(ans,cnt);
        }
    }
    noc[c[cen]]=1;
    no[cen]=1;
    for(int j:adj[cen]){
        if(!no[j]){
            solve(j);
        }
    }
}
int32_t main() {
    cin>>n>>m;
    for(int i=1;i<n;i++){
        int a,b;cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for(int i=1;i<=n;i++){
        cin>>c[i];
        c1[c[i]].push_back(i);
    }
    solve(1);
    cout<<ans-1<<endl;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 15960 KB Output is correct
2 Correct 6 ms 15964 KB Output is correct
3 Correct 5 ms 15964 KB Output is correct
4 Correct 5 ms 15964 KB Output is correct
5 Correct 5 ms 15964 KB Output is correct
6 Correct 5 ms 15964 KB Output is correct
7 Correct 5 ms 16020 KB Output is correct
8 Correct 6 ms 15964 KB Output is correct
9 Correct 5 ms 15964 KB Output is correct
10 Correct 5 ms 15964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 15960 KB Output is correct
2 Correct 6 ms 15964 KB Output is correct
3 Correct 5 ms 15964 KB Output is correct
4 Correct 5 ms 15964 KB Output is correct
5 Correct 5 ms 15964 KB Output is correct
6 Correct 5 ms 15964 KB Output is correct
7 Correct 5 ms 16020 KB Output is correct
8 Correct 6 ms 15964 KB Output is correct
9 Correct 5 ms 15964 KB Output is correct
10 Correct 5 ms 15964 KB Output is correct
11 Correct 7 ms 16220 KB Output is correct
12 Correct 8 ms 16220 KB Output is correct
13 Correct 7 ms 16220 KB Output is correct
14 Correct 6 ms 16220 KB Output is correct
15 Incorrect 7 ms 16220 KB Output isn't correct
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 470 ms 42252 KB Output is correct
2 Correct 253 ms 42812 KB Output is correct
3 Correct 479 ms 41996 KB Output is correct
4 Correct 255 ms 42844 KB Output is correct
5 Incorrect 368 ms 39872 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 15960 KB Output is correct
2 Correct 6 ms 15964 KB Output is correct
3 Correct 5 ms 15964 KB Output is correct
4 Correct 5 ms 15964 KB Output is correct
5 Correct 5 ms 15964 KB Output is correct
6 Correct 5 ms 15964 KB Output is correct
7 Correct 5 ms 16020 KB Output is correct
8 Correct 6 ms 15964 KB Output is correct
9 Correct 5 ms 15964 KB Output is correct
10 Correct 5 ms 15964 KB Output is correct
11 Correct 7 ms 16220 KB Output is correct
12 Correct 8 ms 16220 KB Output is correct
13 Correct 7 ms 16220 KB Output is correct
14 Correct 6 ms 16220 KB Output is correct
15 Incorrect 7 ms 16220 KB Output isn't correct
16 Halted 0 ms 0 KB -