답안 #676866

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
676866 2023-01-01T11:53:00 Z alvingogo 수도 (JOI20_capital_city) C++14
100 / 100
404 ms 39076 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
using namespace std;

vector<int> vis;
vector<vector<int> > vp;
struct no{
    vector<int> ch;
    int sz=1;
    int fa=-1;
    int t;
    int vz=0;
    int op=1;
    int up=0;
};
vector<no> v;
vector<int> gg;
int ans=1e9;
void dfs(int r,int f,int t){
    v[r].sz=1;
    v[r].fa=f;
    v[r].op=1;
    for(auto h:v[r].ch){
        if(h!=f && !v[h].vz){
            dfs(h,r,t);
            v[r].sz+=v[h].sz;
        }
    }
    if(t){
        gg.push_back(r);
    }
}
int fc(int r,int n){
    for(auto h:v[r].ch){
        if(h!=v[r].fa && !v[h].vz && v[h].sz>n/2){
            return fc(h,n);
        }
    }
    return r;
}
void dc(int r){
    dfs(r,r,1);
    int n=gg.size();
    int cd=fc(r,n);
    dfs(cd,cd,0);
    int mx=0;
    int flag=1;
    set<int> s;
    s.insert(v[cd].t);
    set<int> s2;
    while(s.size()){
        mx++;
        auto h=*s.begin();
        s.erase(h);
        s2.insert(h);
        for(auto y:vp[h]){
            if(!v[y].op){
                flag=0;
                break;
            }
            int p=y;
            while(!v[p].up){
                v[p].up=1;
                if(s2.find(v[p].t)==s2.end()){
                    s.insert(v[p].t);
                }
                
                p=v[p].fa;
            }
        }
        if(flag==0){
            break;
        }
    }
    if(flag){
        ans=min(ans,mx-1);
    }
    for(auto h:gg){
        v[h].op=0;
        v[h].up=0;
    }
    for(auto h:vp[v[cd].t]){
        v[h].vz=1;
    }
    vector<int> y;
    y.swap(gg);
    for(auto h:y){
        if(!v[h].vz){
            dc(h);
        }
    }
}
int main(){
    AquA;
    int n,k;
    cin >> n >> k;
    v.resize(n);
    vp.resize(k);
    for(int i=1;i<n;i++){
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        v[a].ch.push_back(b);
        v[b].ch.push_back(a);
    }
    for(int i=0;i<n;i++){
        cin >> v[i].t;
        v[i].t--;
        vp[v[i].t].push_back(i);
    }
    dc(0);
    cout << ans << "\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 2 ms 464 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 1 ms 592 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 2 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 2 ms 596 KB Output is correct
23 Correct 2 ms 616 KB Output is correct
24 Correct 3 ms 596 KB Output is correct
25 Correct 2 ms 584 KB Output is correct
26 Correct 2 ms 596 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 2 ms 612 KB Output is correct
30 Correct 2 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 337 ms 37784 KB Output is correct
2 Correct 165 ms 37024 KB Output is correct
3 Correct 317 ms 38212 KB Output is correct
4 Correct 176 ms 38944 KB Output is correct
5 Correct 331 ms 36048 KB Output is correct
6 Correct 162 ms 38888 KB Output is correct
7 Correct 316 ms 36428 KB Output is correct
8 Correct 155 ms 38424 KB Output is correct
9 Correct 360 ms 34932 KB Output is correct
10 Correct 257 ms 33024 KB Output is correct
11 Correct 287 ms 35260 KB Output is correct
12 Correct 281 ms 37936 KB Output is correct
13 Correct 288 ms 33088 KB Output is correct
14 Correct 328 ms 38140 KB Output is correct
15 Correct 280 ms 37904 KB Output is correct
16 Correct 294 ms 34124 KB Output is correct
17 Correct 278 ms 34400 KB Output is correct
18 Correct 341 ms 34516 KB Output is correct
19 Correct 269 ms 36548 KB Output is correct
20 Correct 291 ms 39076 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 2 ms 464 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 1 ms 592 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 2 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 2 ms 596 KB Output is correct
23 Correct 2 ms 616 KB Output is correct
24 Correct 3 ms 596 KB Output is correct
25 Correct 2 ms 584 KB Output is correct
26 Correct 2 ms 596 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 2 ms 612 KB Output is correct
30 Correct 2 ms 596 KB Output is correct
31 Correct 337 ms 37784 KB Output is correct
32 Correct 165 ms 37024 KB Output is correct
33 Correct 317 ms 38212 KB Output is correct
34 Correct 176 ms 38944 KB Output is correct
35 Correct 331 ms 36048 KB Output is correct
36 Correct 162 ms 38888 KB Output is correct
37 Correct 316 ms 36428 KB Output is correct
38 Correct 155 ms 38424 KB Output is correct
39 Correct 360 ms 34932 KB Output is correct
40 Correct 257 ms 33024 KB Output is correct
41 Correct 287 ms 35260 KB Output is correct
42 Correct 281 ms 37936 KB Output is correct
43 Correct 288 ms 33088 KB Output is correct
44 Correct 328 ms 38140 KB Output is correct
45 Correct 280 ms 37904 KB Output is correct
46 Correct 294 ms 34124 KB Output is correct
47 Correct 278 ms 34400 KB Output is correct
48 Correct 341 ms 34516 KB Output is correct
49 Correct 269 ms 36548 KB Output is correct
50 Correct 291 ms 39076 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 214 ms 22768 KB Output is correct
53 Correct 225 ms 23064 KB Output is correct
54 Correct 239 ms 23520 KB Output is correct
55 Correct 239 ms 23168 KB Output is correct
56 Correct 275 ms 23744 KB Output is correct
57 Correct 234 ms 23624 KB Output is correct
58 Correct 297 ms 29064 KB Output is correct
59 Correct 275 ms 28596 KB Output is correct
60 Correct 259 ms 27456 KB Output is correct
61 Correct 284 ms 27584 KB Output is correct
62 Correct 165 ms 38944 KB Output is correct
63 Correct 171 ms 38900 KB Output is correct
64 Correct 156 ms 38908 KB Output is correct
65 Correct 154 ms 38844 KB Output is correct
66 Correct 165 ms 29964 KB Output is correct
67 Correct 149 ms 29940 KB Output is correct
68 Correct 168 ms 30064 KB Output is correct
69 Correct 172 ms 30056 KB Output is correct
70 Correct 163 ms 30096 KB Output is correct
71 Correct 174 ms 30064 KB Output is correct
72 Correct 152 ms 30060 KB Output is correct
73 Correct 157 ms 29268 KB Output is correct
74 Correct 152 ms 30052 KB Output is correct
75 Correct 159 ms 30040 KB Output is correct
76 Correct 315 ms 33504 KB Output is correct
77 Correct 360 ms 32608 KB Output is correct
78 Correct 273 ms 33868 KB Output is correct
79 Correct 347 ms 32884 KB Output is correct
80 Correct 404 ms 38284 KB Output is correct
81 Correct 280 ms 35652 KB Output is correct
82 Correct 266 ms 35272 KB Output is correct
83 Correct 350 ms 33032 KB Output is correct
84 Correct 283 ms 37704 KB Output is correct
85 Correct 302 ms 35884 KB Output is correct
86 Correct 322 ms 32796 KB Output is correct
87 Correct 303 ms 34024 KB Output is correct
88 Correct 245 ms 33280 KB Output is correct
89 Correct 270 ms 30388 KB Output is correct
90 Correct 243 ms 29872 KB Output is correct
91 Correct 235 ms 32216 KB Output is correct
92 Correct 231 ms 30772 KB Output is correct
93 Correct 256 ms 31152 KB Output is correct
94 Correct 219 ms 30036 KB Output is correct
95 Correct 250 ms 31212 KB Output is correct
96 Correct 243 ms 30156 KB Output is correct
97 Correct 240 ms 31772 KB Output is correct