Submission #875575

# Submission time Handle Problem Language Result Execution time Memory
875575 2023-11-20T04:49:21 Z Darren0724 Capital City (JOI20_capital_city) C++17
100 / 100
627 ms 45556 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),cnt1(N),use(N),tmp;
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 dfs2(int k){
    tmp.push_back(c[k]);
    //cout<<"dfs2:"<<k<<' '<<c[k]<<endl;
    for(int j:adj[k]){
        if(j==pa[k]||no[j])continue;
        dfs2(j);
    }
}
void solve(int p){
    pa[p]=p;
    dfs(p);
    int cen=get_cen(p,sz[p]/2);
    pa[cen]=cen;
    dfs(cen);
    //cout<<"cen:"<<cen<<' '<<c[cen]<<' '<<noc[c[cen]]<<endl;
    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;
        inq1.push_back(c[cen]);
        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]<<' '<<pa[t]<<endl;
                    if(!noc[c[t]]&&inq[c[t]]==0){
                        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;
    vector<int> tmp1;
    for(int j:adj[cen]){
        if(!no[j]){
            tmp.clear();
            dfs2(j);
            for(int j:tmp){
                if(!use[j]){
                    use[j]++;
                    cnt1[j]++;
                    tmp1.push_back(j);
                }
            }
            for(int j:tmp){
                if(use[j]){
                    use[j]--;
                }
            }
        }
    }
    for(int j:tmp1){
        //cout<<j<<' '<<cnt1[j]<<endl;
        if(cnt1[j]>=2){
            //cout<<"get out "<<j<<endl;
            noc[j]=1;
        }
        cnt1[j]=0;
    }
    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;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 17496 KB Output is correct
2 Correct 5 ms 17500 KB Output is correct
3 Correct 5 ms 17700 KB Output is correct
4 Correct 5 ms 17500 KB Output is correct
5 Correct 5 ms 17500 KB Output is correct
6 Correct 6 ms 17612 KB Output is correct
7 Correct 6 ms 17752 KB Output is correct
8 Correct 5 ms 17692 KB Output is correct
9 Correct 5 ms 17500 KB Output is correct
10 Correct 5 ms 17500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 17496 KB Output is correct
2 Correct 5 ms 17500 KB Output is correct
3 Correct 5 ms 17700 KB Output is correct
4 Correct 5 ms 17500 KB Output is correct
5 Correct 5 ms 17500 KB Output is correct
6 Correct 6 ms 17612 KB Output is correct
7 Correct 6 ms 17752 KB Output is correct
8 Correct 5 ms 17692 KB Output is correct
9 Correct 5 ms 17500 KB Output is correct
10 Correct 5 ms 17500 KB Output is correct
11 Correct 8 ms 17756 KB Output is correct
12 Correct 7 ms 17756 KB Output is correct
13 Correct 8 ms 17908 KB Output is correct
14 Correct 8 ms 17772 KB Output is correct
15 Correct 7 ms 17868 KB Output is correct
16 Correct 7 ms 17708 KB Output is correct
17 Correct 7 ms 17756 KB Output is correct
18 Correct 7 ms 17756 KB Output is correct
19 Correct 7 ms 17756 KB Output is correct
20 Correct 7 ms 17756 KB Output is correct
21 Correct 7 ms 17756 KB Output is correct
22 Correct 7 ms 17756 KB Output is correct
23 Correct 8 ms 17756 KB Output is correct
24 Correct 8 ms 17756 KB Output is correct
25 Correct 9 ms 17752 KB Output is correct
26 Correct 9 ms 17756 KB Output is correct
27 Correct 8 ms 17756 KB Output is correct
28 Correct 8 ms 17756 KB Output is correct
29 Correct 8 ms 17756 KB Output is correct
30 Correct 8 ms 17756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 448 ms 45156 KB Output is correct
2 Correct 278 ms 45536 KB Output is correct
3 Correct 448 ms 44768 KB Output is correct
4 Correct 277 ms 45368 KB Output is correct
5 Correct 447 ms 42280 KB Output is correct
6 Correct 275 ms 45312 KB Output is correct
7 Correct 431 ms 42496 KB Output is correct
8 Correct 273 ms 44708 KB Output is correct
9 Correct 582 ms 40484 KB Output is correct
10 Correct 566 ms 38272 KB Output is correct
11 Correct 547 ms 40764 KB Output is correct
12 Correct 553 ms 42944 KB Output is correct
13 Correct 541 ms 38028 KB Output is correct
14 Correct 595 ms 43120 KB Output is correct
15 Correct 559 ms 42836 KB Output is correct
16 Correct 573 ms 38864 KB Output is correct
17 Correct 551 ms 39196 KB Output is correct
18 Correct 550 ms 39352 KB Output is correct
19 Correct 568 ms 42224 KB Output is correct
20 Correct 535 ms 43576 KB Output is correct
21 Correct 5 ms 17500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 17496 KB Output is correct
2 Correct 5 ms 17500 KB Output is correct
3 Correct 5 ms 17700 KB Output is correct
4 Correct 5 ms 17500 KB Output is correct
5 Correct 5 ms 17500 KB Output is correct
6 Correct 6 ms 17612 KB Output is correct
7 Correct 6 ms 17752 KB Output is correct
8 Correct 5 ms 17692 KB Output is correct
9 Correct 5 ms 17500 KB Output is correct
10 Correct 5 ms 17500 KB Output is correct
11 Correct 8 ms 17756 KB Output is correct
12 Correct 7 ms 17756 KB Output is correct
13 Correct 8 ms 17908 KB Output is correct
14 Correct 8 ms 17772 KB Output is correct
15 Correct 7 ms 17868 KB Output is correct
16 Correct 7 ms 17708 KB Output is correct
17 Correct 7 ms 17756 KB Output is correct
18 Correct 7 ms 17756 KB Output is correct
19 Correct 7 ms 17756 KB Output is correct
20 Correct 7 ms 17756 KB Output is correct
21 Correct 7 ms 17756 KB Output is correct
22 Correct 7 ms 17756 KB Output is correct
23 Correct 8 ms 17756 KB Output is correct
24 Correct 8 ms 17756 KB Output is correct
25 Correct 9 ms 17752 KB Output is correct
26 Correct 9 ms 17756 KB Output is correct
27 Correct 8 ms 17756 KB Output is correct
28 Correct 8 ms 17756 KB Output is correct
29 Correct 8 ms 17756 KB Output is correct
30 Correct 8 ms 17756 KB Output is correct
31 Correct 448 ms 45156 KB Output is correct
32 Correct 278 ms 45536 KB Output is correct
33 Correct 448 ms 44768 KB Output is correct
34 Correct 277 ms 45368 KB Output is correct
35 Correct 447 ms 42280 KB Output is correct
36 Correct 275 ms 45312 KB Output is correct
37 Correct 431 ms 42496 KB Output is correct
38 Correct 273 ms 44708 KB Output is correct
39 Correct 582 ms 40484 KB Output is correct
40 Correct 566 ms 38272 KB Output is correct
41 Correct 547 ms 40764 KB Output is correct
42 Correct 553 ms 42944 KB Output is correct
43 Correct 541 ms 38028 KB Output is correct
44 Correct 595 ms 43120 KB Output is correct
45 Correct 559 ms 42836 KB Output is correct
46 Correct 573 ms 38864 KB Output is correct
47 Correct 551 ms 39196 KB Output is correct
48 Correct 550 ms 39352 KB Output is correct
49 Correct 568 ms 42224 KB Output is correct
50 Correct 535 ms 43576 KB Output is correct
51 Correct 5 ms 17500 KB Output is correct
52 Correct 394 ms 30768 KB Output is correct
53 Correct 387 ms 30544 KB Output is correct
54 Correct 414 ms 30508 KB Output is correct
55 Correct 381 ms 30472 KB Output is correct
56 Correct 429 ms 30496 KB Output is correct
57 Correct 406 ms 30408 KB Output is correct
58 Correct 434 ms 33448 KB Output is correct
59 Correct 378 ms 34104 KB Output is correct
60 Correct 464 ms 33860 KB Output is correct
61 Correct 467 ms 33540 KB Output is correct
62 Correct 277 ms 45556 KB Output is correct
63 Correct 279 ms 45368 KB Output is correct
64 Correct 278 ms 44760 KB Output is correct
65 Correct 289 ms 45392 KB Output is correct
66 Correct 308 ms 37632 KB Output is correct
67 Correct 337 ms 37688 KB Output is correct
68 Correct 380 ms 37772 KB Output is correct
69 Correct 309 ms 37776 KB Output is correct
70 Correct 310 ms 37636 KB Output is correct
71 Correct 339 ms 37632 KB Output is correct
72 Correct 331 ms 37632 KB Output is correct
73 Correct 326 ms 36864 KB Output is correct
74 Correct 322 ms 37756 KB Output is correct
75 Correct 325 ms 37668 KB Output is correct
76 Correct 544 ms 36632 KB Output is correct
77 Correct 507 ms 35312 KB Output is correct
78 Correct 545 ms 39100 KB Output is correct
79 Correct 588 ms 37564 KB Output is correct
80 Correct 590 ms 43324 KB Output is correct
81 Correct 620 ms 40588 KB Output is correct
82 Correct 550 ms 40504 KB Output is correct
83 Correct 627 ms 37928 KB Output is correct
84 Correct 577 ms 42712 KB Output is correct
85 Correct 599 ms 41508 KB Output is correct
86 Correct 559 ms 37612 KB Output is correct
87 Correct 582 ms 38804 KB Output is correct
88 Correct 505 ms 40076 KB Output is correct
89 Correct 514 ms 36632 KB Output is correct
90 Correct 513 ms 36112 KB Output is correct
91 Correct 555 ms 38416 KB Output is correct
92 Correct 518 ms 37384 KB Output is correct
93 Correct 580 ms 37200 KB Output is correct
94 Correct 520 ms 36748 KB Output is correct
95 Correct 488 ms 37504 KB Output is correct
96 Correct 482 ms 36560 KB Output is correct
97 Correct 500 ms 38540 KB Output is correct