답안 #623494

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
623494 2022-08-05T17:02:25 Z welleyth 수도 (JOI20_capital_city) C++17
100 / 100
1238 ms 46956 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define int long long
#define mp make_pair
#define pb push_back

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")

constexpr int INF = (int)2e18;
constexpr int N = (int)2e5 + 111;
constexpr int md = (int)998244353;
constexpr int mdPower = (int)1e9+7 - 1;
constexpr double eps = 1e-7;

typedef __int128 _uint;
typedef long long ll;

mt19937_64 rnd(time(nullptr));

void add(int& a,int b){
    a += b; if(a >= md) a -= md;
}
void sub(int& a,int b){
    a -= b; while(a < 0) a += md;
}
void add(__int128& a,int b){
    a += b;
}
void sub(__int128& a,int b){
    a -= b;
}

int bpow(int a,int b){
    if(b == 0)
        return 1;
    if(b % 2 == 0){
        int t = bpow(a,b>>1);
        return 1ll*t*t%md;
    }
    return 1ll * a*bpow(a,b-1) % md;
}

int inv(int a){
    return bpow(a,md-2);
}

int fac[N],invfac[N];

void init(){
    fac[0] = 1;
    for(int i = 1; i < N; i++){
        fac[i] = (fac[i-1] * i) % md;
    }
    invfac[N-1] = inv(fac[N-1]);
    for(int i = N-2; i >= 0; i--){
        invfac[i] = (invfac[i+1] * (i+1))%md;
    }
    return;
}

//int C(int n,int k){
//    if(k > n)
//        return 0;
//    return fac[n] * invfac[k] % md * invfac[n-k] % md;
//}
//
//int A(int n,int k){
//    if(k > n)
//        return 0;
//    return fac[n] * invfac[n-k] % md;
//}

vector<int> g[N];
int col[N];
vector<int> cities[N];
int sz[N];
int ans = INF;
bool used[N];
bool choosen[N];
int p[N];
int L[N];
int d[N];

void dfs1(int v,int pr = -1){
    sz[v] = 1;
    for(auto& to : g[v]){
        if(to == pr || used[to])
            continue;
        dfs1(to,v);
        sz[v] += sz[to];
    }
    return;
}

int getCentroid(int v,int n,int pr = -1){
    for(auto& to : g[v]){
        if(to == pr || used[to])
            continue;
        if(sz[to] > (n+1)/2)
            return getCentroid(to,n,v);
    }
    return v;
}

int CNT = 0;
void dfs3(int v,int pr = -1){
    L[v] = CNT;
    p[v] = pr;
    for(auto& to : g[v]){
        if(to == pr || used[to])
            continue;
        d[to] = d[v] + 1;
        dfs3(to,v);
    }
    return;
}

void solve(int v,int pr = -1){
    dfs1(v);
    int C = getCentroid(v,sz[v]);
//    cerr << "C = " << C << "\n";
    int cur_col = col[C];
    dfs1(C);
    CNT++;
    d[C] = 0;
    dfs3(C);
//    cerr << "ok\n";
    if(!choosen[cur_col]){
        set<int> added_colors;
        added_colors.insert(cur_col);
        set<pair<int,int>> q;
        bool ok = true;
        set<int> visited;
        for(auto& x : cities[cur_col]){
            q.insert(mp(-d[x],x));
            visited.insert(x);
            if(L[x] != CNT){
                ok = false;
                break;
            }
        }
        while(!q.empty() && ok){
            int v = q.begin()->second;
            q.erase(q.begin());
//            cerr << "v = " << v << "\n";
            if(choosen[col[v]]){
                ok = false;
                break;
            }
            if(!added_colors.count(col[v])){
                for(auto& x : cities[col[v]]){
                    if(!visited.count(x)){
                        q.insert(mp(-d[x],x));
                        visited.insert(x);
                    }
                    if(L[x] != CNT){
                        ok = false;
                        break;
                    }
                }
                added_colors.insert(col[v]);
            }
            if(p[v] > 0 && !visited.count(p[v])){
                q.insert(mp(-d[p[v]],p[v]));
                visited.insert(p[v]);
            }
        }
        if(ok){
            ans = min(ans,(int)added_colors.size()-1);
            choosen[cur_col] = 1;
        }
//        cerr << "v = " << v << ", centroid = " << C << "\n";
    }
    used[C] = true;
    for(auto& to : g[C]){
        if(!used[to]){
            solve(to);
        }
    }
    return;
}

void solve(){
    int n,k;
    cin >> n >> k;

    for(int i = 1; i < n; i++){
        int a,b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }

    for(int i = 1; i <= n; i++){
        cin >> col[i];
        cities[col[i]].pb(i);
    }

    solve(1);

    assert(ans != INF);
    cout << ans << "\n";

    return;
}

signed main(){
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    init();
    int tests = 1;
//    cin >> tests;
    for(int test = 1; test <= tests; test++){
//        cerr << "test = " << test << "\n";
        solve();
    }
    return 0;
}
/**

**/
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12904 KB Output is correct
2 Correct 8 ms 12884 KB Output is correct
3 Correct 9 ms 12884 KB Output is correct
4 Correct 9 ms 12836 KB Output is correct
5 Correct 9 ms 12828 KB Output is correct
6 Correct 9 ms 12884 KB Output is correct
7 Correct 8 ms 12816 KB Output is correct
8 Correct 8 ms 12884 KB Output is correct
9 Correct 8 ms 12884 KB Output is correct
10 Correct 8 ms 12896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12904 KB Output is correct
2 Correct 8 ms 12884 KB Output is correct
3 Correct 9 ms 12884 KB Output is correct
4 Correct 9 ms 12836 KB Output is correct
5 Correct 9 ms 12828 KB Output is correct
6 Correct 9 ms 12884 KB Output is correct
7 Correct 8 ms 12816 KB Output is correct
8 Correct 8 ms 12884 KB Output is correct
9 Correct 8 ms 12884 KB Output is correct
10 Correct 8 ms 12896 KB Output is correct
11 Correct 12 ms 13132 KB Output is correct
12 Correct 11 ms 13140 KB Output is correct
13 Correct 11 ms 13140 KB Output is correct
14 Correct 13 ms 13140 KB Output is correct
15 Correct 13 ms 13020 KB Output is correct
16 Correct 14 ms 13040 KB Output is correct
17 Correct 10 ms 13140 KB Output is correct
18 Correct 10 ms 13160 KB Output is correct
19 Correct 10 ms 13144 KB Output is correct
20 Correct 10 ms 13132 KB Output is correct
21 Correct 11 ms 13192 KB Output is correct
22 Correct 11 ms 13012 KB Output is correct
23 Correct 11 ms 13056 KB Output is correct
24 Correct 11 ms 13136 KB Output is correct
25 Correct 11 ms 13140 KB Output is correct
26 Correct 12 ms 13224 KB Output is correct
27 Correct 11 ms 13096 KB Output is correct
28 Correct 12 ms 13140 KB Output is correct
29 Correct 14 ms 13140 KB Output is correct
30 Correct 12 ms 13172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 586 ms 35996 KB Output is correct
2 Correct 235 ms 36108 KB Output is correct
3 Correct 663 ms 35756 KB Output is correct
4 Correct 230 ms 36160 KB Output is correct
5 Correct 611 ms 36204 KB Output is correct
6 Correct 265 ms 36392 KB Output is correct
7 Correct 682 ms 37736 KB Output is correct
8 Correct 278 ms 40144 KB Output is correct
9 Correct 979 ms 41004 KB Output is correct
10 Correct 946 ms 40172 KB Output is correct
11 Correct 1030 ms 40932 KB Output is correct
12 Correct 976 ms 42076 KB Output is correct
13 Correct 996 ms 39944 KB Output is correct
14 Correct 1078 ms 42068 KB Output is correct
15 Correct 976 ms 42060 KB Output is correct
16 Correct 1000 ms 40244 KB Output is correct
17 Correct 987 ms 40992 KB Output is correct
18 Correct 1029 ms 40464 KB Output is correct
19 Correct 999 ms 41932 KB Output is correct
20 Correct 935 ms 42184 KB Output is correct
21 Correct 8 ms 12884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12904 KB Output is correct
2 Correct 8 ms 12884 KB Output is correct
3 Correct 9 ms 12884 KB Output is correct
4 Correct 9 ms 12836 KB Output is correct
5 Correct 9 ms 12828 KB Output is correct
6 Correct 9 ms 12884 KB Output is correct
7 Correct 8 ms 12816 KB Output is correct
8 Correct 8 ms 12884 KB Output is correct
9 Correct 8 ms 12884 KB Output is correct
10 Correct 8 ms 12896 KB Output is correct
11 Correct 12 ms 13132 KB Output is correct
12 Correct 11 ms 13140 KB Output is correct
13 Correct 11 ms 13140 KB Output is correct
14 Correct 13 ms 13140 KB Output is correct
15 Correct 13 ms 13020 KB Output is correct
16 Correct 14 ms 13040 KB Output is correct
17 Correct 10 ms 13140 KB Output is correct
18 Correct 10 ms 13160 KB Output is correct
19 Correct 10 ms 13144 KB Output is correct
20 Correct 10 ms 13132 KB Output is correct
21 Correct 11 ms 13192 KB Output is correct
22 Correct 11 ms 13012 KB Output is correct
23 Correct 11 ms 13056 KB Output is correct
24 Correct 11 ms 13136 KB Output is correct
25 Correct 11 ms 13140 KB Output is correct
26 Correct 12 ms 13224 KB Output is correct
27 Correct 11 ms 13096 KB Output is correct
28 Correct 12 ms 13140 KB Output is correct
29 Correct 14 ms 13140 KB Output is correct
30 Correct 12 ms 13172 KB Output is correct
31 Correct 586 ms 35996 KB Output is correct
32 Correct 235 ms 36108 KB Output is correct
33 Correct 663 ms 35756 KB Output is correct
34 Correct 230 ms 36160 KB Output is correct
35 Correct 611 ms 36204 KB Output is correct
36 Correct 265 ms 36392 KB Output is correct
37 Correct 682 ms 37736 KB Output is correct
38 Correct 278 ms 40144 KB Output is correct
39 Correct 979 ms 41004 KB Output is correct
40 Correct 946 ms 40172 KB Output is correct
41 Correct 1030 ms 40932 KB Output is correct
42 Correct 976 ms 42076 KB Output is correct
43 Correct 996 ms 39944 KB Output is correct
44 Correct 1078 ms 42068 KB Output is correct
45 Correct 976 ms 42060 KB Output is correct
46 Correct 1000 ms 40244 KB Output is correct
47 Correct 987 ms 40992 KB Output is correct
48 Correct 1029 ms 40464 KB Output is correct
49 Correct 999 ms 41932 KB Output is correct
50 Correct 935 ms 42184 KB Output is correct
51 Correct 8 ms 12884 KB Output is correct
52 Correct 1018 ms 46684 KB Output is correct
53 Correct 1022 ms 46760 KB Output is correct
54 Correct 1039 ms 46956 KB Output is correct
55 Correct 1032 ms 46708 KB Output is correct
56 Correct 1034 ms 46804 KB Output is correct
57 Correct 1016 ms 46708 KB Output is correct
58 Correct 834 ms 37288 KB Output is correct
59 Correct 671 ms 33820 KB Output is correct
60 Correct 681 ms 32716 KB Output is correct
61 Correct 867 ms 34360 KB Output is correct
62 Correct 222 ms 36316 KB Output is correct
63 Correct 233 ms 36164 KB Output is correct
64 Correct 255 ms 38676 KB Output is correct
65 Correct 229 ms 36556 KB Output is correct
66 Correct 529 ms 44276 KB Output is correct
67 Correct 475 ms 44096 KB Output is correct
68 Correct 486 ms 44316 KB Output is correct
69 Correct 461 ms 44228 KB Output is correct
70 Correct 498 ms 44156 KB Output is correct
71 Correct 476 ms 44212 KB Output is correct
72 Correct 461 ms 44204 KB Output is correct
73 Correct 460 ms 43968 KB Output is correct
74 Correct 443 ms 44092 KB Output is correct
75 Correct 501 ms 44356 KB Output is correct
76 Correct 1238 ms 37644 KB Output is correct
77 Correct 1206 ms 36972 KB Output is correct
78 Correct 915 ms 40208 KB Output is correct
79 Correct 987 ms 39688 KB Output is correct
80 Correct 935 ms 42336 KB Output is correct
81 Correct 967 ms 41276 KB Output is correct
82 Correct 1020 ms 41036 KB Output is correct
83 Correct 1016 ms 39948 KB Output is correct
84 Correct 977 ms 41924 KB Output is correct
85 Correct 951 ms 41828 KB Output is correct
86 Correct 964 ms 39476 KB Output is correct
87 Correct 1026 ms 40544 KB Output is correct
88 Correct 831 ms 40564 KB Output is correct
89 Correct 814 ms 38792 KB Output is correct
90 Correct 810 ms 39176 KB Output is correct
91 Correct 842 ms 39228 KB Output is correct
92 Correct 825 ms 39044 KB Output is correct
93 Correct 865 ms 39152 KB Output is correct
94 Correct 925 ms 39196 KB Output is correct
95 Correct 875 ms 39804 KB Output is correct
96 Correct 838 ms 39040 KB Output is correct
97 Correct 783 ms 39884 KB Output is correct