답안 #359152

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
359152 2021-01-26T11:48:33 Z Nima_Naderi 수도 (JOI20_capital_city) C++14
100 / 100
1157 ms 52708 KB
///In the name of GOD
//#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MXN = 4e5 + 10;
ll n, k, ans = 1e9;
ll A[MXN], sub[MXN], dad[MXN], Cnt[MXN], All[MXN];
vector<ll> adj[MXN], Ver[MXN], vec, col;
bool hide[MXN], bad[MXN], mark[MXN], vis[MXN];
void plant(ll u, ll par){
    sub[u] = 1;
    for(auto v : adj[u]){
        if(v != par && !hide[v]){
            plant(v, u), sub[u] += sub[v];
        }
    }
}
void Pre(ll u, ll par, ll dt){
    dad[u] = par, mark[u] = 0;
    if(dt == +1 && Cnt[A[u]] == 0) col.push_back(A[u]);
    if(dt == +1) Cnt[A[u]] ++;
    else         Cnt[A[u]] = 0;
    for(auto v : adj[u]){
        if(v != par && !hide[v]) Pre(v, u, dt);
    }
}
ll CRD(ll u, ll par, ll val){
    for(auto v : adj[u]){
        if(v == par || hide[v]) continue;
        if(sub[v] >= val) return CRD(v, u, val);
    }
    return u;
}
void DMS(ll u, ll h){
    plant(u, -1);
    ll cent = CRD(u, -1, (sub[u] + 1) / 2);//attention
    col.clear(), vec.clear();
    Pre(cent, -1, +1);
    for(auto c : col){
        if(Cnt[c] != All[c]) bad[c] = 1;
        else                 bad[c] = 0;
        vis[c] = 0;
    }
    if(!bad[A[cent]]){
        vec.push_back(cent);
        ll tot = 0, pnt = 0; bool ok = 1;
        while(pnt < vec.size()){
            ll now = vec[pnt]; pnt ++;
            if(mark[now] || now == -1) continue;
            ll vr = now;
            while(vr != -1 && !mark[vr]){
                mark[vr] = 1;
                if(!vis[A[vr]]){
                    tot ++; vis[A[vr]] = 1;
                    ok &= (!bad[A[vr]]);
                    if(!ok) break;
                    for(auto X : Ver[A[vr]]){
                        if(X == vr){
                            assert(mark[X]);
                            continue;
                        }
                        assert(!mark[X]);
                        vec.push_back(X);
                    }
                }
                if(!ok) break;
                vr = dad[vr];
            }
            if(!ok) break;
        }
        for(auto c : col) vis[c] = 0;
        if(ok) ans = min(ans, tot);
    }
    col.clear(), Pre(cent, -1, -1);
    hide[cent] = 1;
    for(auto v : adj[cent]){
        if(!hide[v]) DMS(v, h + 1);
    }
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    cin >> n >> k;
    for(int i = 1; i < n; i ++){
        ll u, v; cin >> u >> v;
        adj[u].push_back(v), adj[v].push_back(u);
    }
    for(int i = 1; i <= n; i ++){
        cin >> A[i], All[A[i]] ++;
        Ver[A[i]].push_back(i);
    }
    DMS(1, 0);
    cout << ans - 1 << '\n';
    return 0;
}
/*!
    HE'S AN INSTIGATOR,
    ENEMY ELIMINATOR,
    AND WHEN HE KNOCKS YOU BETTER
    YOU BETTER LET HIM IN.
*/
//! N.N

Compilation message

capital_city.cpp: In function 'void DMS(ll, ll)':
capital_city.cpp:49:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |         while(pnt < vec.size()){
      |               ~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 19180 KB Output is correct
2 Correct 15 ms 19180 KB Output is correct
3 Correct 13 ms 19180 KB Output is correct
4 Correct 14 ms 19180 KB Output is correct
5 Correct 13 ms 19180 KB Output is correct
6 Correct 14 ms 19180 KB Output is correct
7 Correct 14 ms 19180 KB Output is correct
8 Correct 14 ms 19180 KB Output is correct
9 Correct 13 ms 19180 KB Output is correct
10 Correct 13 ms 19180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 19180 KB Output is correct
2 Correct 15 ms 19180 KB Output is correct
3 Correct 13 ms 19180 KB Output is correct
4 Correct 14 ms 19180 KB Output is correct
5 Correct 13 ms 19180 KB Output is correct
6 Correct 14 ms 19180 KB Output is correct
7 Correct 14 ms 19180 KB Output is correct
8 Correct 14 ms 19180 KB Output is correct
9 Correct 13 ms 19180 KB Output is correct
10 Correct 13 ms 19180 KB Output is correct
11 Correct 15 ms 19436 KB Output is correct
12 Correct 15 ms 19456 KB Output is correct
13 Correct 15 ms 19436 KB Output is correct
14 Correct 15 ms 19436 KB Output is correct
15 Correct 15 ms 19436 KB Output is correct
16 Correct 15 ms 19436 KB Output is correct
17 Correct 15 ms 19436 KB Output is correct
18 Correct 15 ms 19436 KB Output is correct
19 Correct 15 ms 19436 KB Output is correct
20 Correct 15 ms 19436 KB Output is correct
21 Correct 15 ms 19436 KB Output is correct
22 Correct 15 ms 19436 KB Output is correct
23 Correct 15 ms 19436 KB Output is correct
24 Correct 16 ms 19436 KB Output is correct
25 Correct 16 ms 19436 KB Output is correct
26 Correct 16 ms 19436 KB Output is correct
27 Correct 16 ms 19436 KB Output is correct
28 Correct 16 ms 19436 KB Output is correct
29 Correct 17 ms 19436 KB Output is correct
30 Correct 16 ms 19436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 934 ms 52132 KB Output is correct
2 Correct 273 ms 52708 KB Output is correct
3 Correct 942 ms 51940 KB Output is correct
4 Correct 264 ms 52580 KB Output is correct
5 Correct 910 ms 49508 KB Output is correct
6 Correct 271 ms 52700 KB Output is correct
7 Correct 913 ms 49548 KB Output is correct
8 Correct 265 ms 52064 KB Output is correct
9 Correct 1035 ms 48624 KB Output is correct
10 Correct 1051 ms 46436 KB Output is correct
11 Correct 1071 ms 48888 KB Output is correct
12 Correct 1109 ms 50964 KB Output is correct
13 Correct 1091 ms 45800 KB Output is correct
14 Correct 1108 ms 51392 KB Output is correct
15 Correct 1127 ms 51004 KB Output is correct
16 Correct 1084 ms 47128 KB Output is correct
17 Correct 1126 ms 47520 KB Output is correct
18 Correct 1101 ms 47592 KB Output is correct
19 Correct 1109 ms 49956 KB Output is correct
20 Correct 1105 ms 51668 KB Output is correct
21 Correct 14 ms 19180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 19180 KB Output is correct
2 Correct 15 ms 19180 KB Output is correct
3 Correct 13 ms 19180 KB Output is correct
4 Correct 14 ms 19180 KB Output is correct
5 Correct 13 ms 19180 KB Output is correct
6 Correct 14 ms 19180 KB Output is correct
7 Correct 14 ms 19180 KB Output is correct
8 Correct 14 ms 19180 KB Output is correct
9 Correct 13 ms 19180 KB Output is correct
10 Correct 13 ms 19180 KB Output is correct
11 Correct 15 ms 19436 KB Output is correct
12 Correct 15 ms 19456 KB Output is correct
13 Correct 15 ms 19436 KB Output is correct
14 Correct 15 ms 19436 KB Output is correct
15 Correct 15 ms 19436 KB Output is correct
16 Correct 15 ms 19436 KB Output is correct
17 Correct 15 ms 19436 KB Output is correct
18 Correct 15 ms 19436 KB Output is correct
19 Correct 15 ms 19436 KB Output is correct
20 Correct 15 ms 19436 KB Output is correct
21 Correct 15 ms 19436 KB Output is correct
22 Correct 15 ms 19436 KB Output is correct
23 Correct 15 ms 19436 KB Output is correct
24 Correct 16 ms 19436 KB Output is correct
25 Correct 16 ms 19436 KB Output is correct
26 Correct 16 ms 19436 KB Output is correct
27 Correct 16 ms 19436 KB Output is correct
28 Correct 16 ms 19436 KB Output is correct
29 Correct 17 ms 19436 KB Output is correct
30 Correct 16 ms 19436 KB Output is correct
31 Correct 934 ms 52132 KB Output is correct
32 Correct 273 ms 52708 KB Output is correct
33 Correct 942 ms 51940 KB Output is correct
34 Correct 264 ms 52580 KB Output is correct
35 Correct 910 ms 49508 KB Output is correct
36 Correct 271 ms 52700 KB Output is correct
37 Correct 913 ms 49548 KB Output is correct
38 Correct 265 ms 52064 KB Output is correct
39 Correct 1035 ms 48624 KB Output is correct
40 Correct 1051 ms 46436 KB Output is correct
41 Correct 1071 ms 48888 KB Output is correct
42 Correct 1109 ms 50964 KB Output is correct
43 Correct 1091 ms 45800 KB Output is correct
44 Correct 1108 ms 51392 KB Output is correct
45 Correct 1127 ms 51004 KB Output is correct
46 Correct 1084 ms 47128 KB Output is correct
47 Correct 1126 ms 47520 KB Output is correct
48 Correct 1101 ms 47592 KB Output is correct
49 Correct 1109 ms 49956 KB Output is correct
50 Correct 1105 ms 51668 KB Output is correct
51 Correct 14 ms 19180 KB Output is correct
52 Correct 718 ms 40544 KB Output is correct
53 Correct 799 ms 40484 KB Output is correct
54 Correct 790 ms 40476 KB Output is correct
55 Correct 846 ms 40416 KB Output is correct
56 Correct 795 ms 40420 KB Output is correct
57 Correct 806 ms 40432 KB Output is correct
58 Correct 792 ms 43224 KB Output is correct
59 Correct 871 ms 44096 KB Output is correct
60 Correct 1051 ms 42816 KB Output is correct
61 Correct 986 ms 42476 KB Output is correct
62 Correct 318 ms 52580 KB Output is correct
63 Correct 274 ms 52564 KB Output is correct
64 Correct 268 ms 52064 KB Output is correct
65 Correct 268 ms 52524 KB Output is correct
66 Correct 592 ms 46884 KB Output is correct
67 Correct 637 ms 46688 KB Output is correct
68 Correct 632 ms 46816 KB Output is correct
69 Correct 566 ms 46792 KB Output is correct
70 Correct 599 ms 46688 KB Output is correct
71 Correct 543 ms 46816 KB Output is correct
72 Correct 567 ms 46788 KB Output is correct
73 Correct 581 ms 46084 KB Output is correct
74 Correct 533 ms 46816 KB Output is correct
75 Correct 597 ms 46816 KB Output is correct
76 Correct 922 ms 46820 KB Output is correct
77 Correct 970 ms 45028 KB Output is correct
78 Correct 1079 ms 47208 KB Output is correct
79 Correct 1106 ms 45284 KB Output is correct
80 Correct 1121 ms 50908 KB Output is correct
81 Correct 1131 ms 48260 KB Output is correct
82 Correct 1151 ms 48388 KB Output is correct
83 Correct 1088 ms 46052 KB Output is correct
84 Correct 1157 ms 50596 KB Output is correct
85 Correct 1138 ms 49636 KB Output is correct
86 Correct 1115 ms 45284 KB Output is correct
87 Correct 1140 ms 46724 KB Output is correct
88 Correct 971 ms 48300 KB Output is correct
89 Correct 908 ms 44912 KB Output is correct
90 Correct 888 ms 44760 KB Output is correct
91 Correct 937 ms 46436 KB Output is correct
92 Correct 898 ms 46088 KB Output is correct
93 Correct 925 ms 45424 KB Output is correct
94 Correct 954 ms 44964 KB Output is correct
95 Correct 975 ms 46084 KB Output is correct
96 Correct 937 ms 45540 KB Output is correct
97 Correct 947 ms 47204 KB Output is correct