Submission #218072

# Submission time Handle Problem Language Result Execution time Memory
218072 2020-04-01T00:58:58 Z achibasadzishvili Capital City (JOI20_capital_city) C++14
100 / 100
1039 ms 39704 KB
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define N 200005
using namespace std;
ll ans,ch[N],p[N],siz,center,fi[N],fix[N],ra[N],raod[N],c[N],n,k;
vector<ll>ver,v[N],fer[N];
queue<ll>q;
void calc(ll x,ll par){
    ch[x] = 1;
    siz++;
    for(int i=0; i<v[x].size(); i++)
        if(v[x][i] != par && !fix[v[x][i]]){
            calc(v[x][i] , x);
            ch[x] += ch[v[x][i]];
        }
}
void findcenter(ll x,ll par){
    if(center)return;
    for(int i=0; i<v[x].size(); i++)
        if(!fix[v[x][i]] && v[x][i] != par && ch[v[x][i]] >= (siz / 2))
            findcenter(v[x][i] , x);
    if(!center)center = x;
}
void go(ll x,ll par){
    ra[c[x]]++;
    fer[c[x]].pb(x);
    p[x] = par;
    ver.pb(x);
    for(int i=0; i<v[x].size(); i++)
        if(v[x][i] != par && !fix[v[x][i]])
            go(v[x][i] , x);
}
void solve(ll x){
    siz = 0;
    calc(x , 0);
    center = 0;
    findcenter(x , 0);
    x = center;
    ver.clear();
    go(x , 0);
    while(q.size())q.pop();
    for(int i=0; i<ver.size(); i++){
        if(c[ver[i]] == c[x]){
            fi[ver[i]] = 1;
            q.push(ver[i]);
        }
    }
    bool yep = true;
    ll val = 0;
    while(q.size()){
        int t = q.front();
        q.pop();
        if(ra[c[t]] != raod[c[t]]){
            yep = false;
            break;
        }
        if(fi[p[t]] == 0 && p[t] != 0){
            val++;
            for(int i=0; i<fer[c[p[t]]].size(); i++){
                int cur = fer[c[p[t]]][i];
                fi[cur] = 1;
                q.push(cur);
            }
        }
    }
    if(yep){
        ans = min(ans , val);
    }
    for(int i=0; i<ver.size(); i++){
        ra[c[ver[i]]] = 0;
        fer[c[ver[i]]].clear();
        fi[ver[i]] = 0;
    }
    fix[x] = 1;
    for(int i=0; i<v[x].size(); i++)
        if(!fix[v[x][i]])
            solve(v[x][i]);
}
int main(){
    ios::sync_with_stdio(false);
    cin >> n >> k;
    ans = k;
    for(int i=1; i<n; i++){
        ll x,y;
        cin >> x >> y;
        v[x].pb(y);
        v[y].pb(x);
    }
    
    for(int i=1; i<=n; i++){
        cin >> c[i];
        raod[c[i]]++;
    }
    
    solve(1);
    
    cout << ans << '\n';
    
    return 0;
}

Compilation message

capital_city.cpp: In function 'void calc(long long int, long long int)':
capital_city.cpp:14:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++)
                  ~^~~~~~~~~~~~
capital_city.cpp: In function 'void findcenter(long long int, long long int)':
capital_city.cpp:22:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++)
                  ~^~~~~~~~~~~~
capital_city.cpp: In function 'void go(long long int, long long int)':
capital_city.cpp:32:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++)
                  ~^~~~~~~~~~~~
capital_city.cpp: In function 'void solve(long long int)':
capital_city.cpp:45:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<ver.size(); i++){
                  ~^~~~~~~~~~~
capital_city.cpp:62:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int i=0; i<fer[c[p[t]]].size(); i++){
                          ~^~~~~~~~~~~~~~~~~~~~
capital_city.cpp:72:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<ver.size(); i++){
                  ~^~~~~~~~~~~
capital_city.cpp:78:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++)
                  ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 12 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 10 ms 9856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 12 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 10 ms 9856 KB Output is correct
11 Correct 12 ms 9984 KB Output is correct
12 Correct 12 ms 9984 KB Output is correct
13 Correct 12 ms 9984 KB Output is correct
14 Correct 12 ms 9984 KB Output is correct
15 Correct 12 ms 10112 KB Output is correct
16 Correct 12 ms 9984 KB Output is correct
17 Correct 12 ms 9984 KB Output is correct
18 Correct 12 ms 9984 KB Output is correct
19 Correct 12 ms 10112 KB Output is correct
20 Correct 12 ms 9984 KB Output is correct
21 Correct 11 ms 10112 KB Output is correct
22 Correct 12 ms 10112 KB Output is correct
23 Correct 13 ms 9980 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 13 ms 9984 KB Output is correct
26 Correct 13 ms 9984 KB Output is correct
27 Correct 16 ms 9984 KB Output is correct
28 Correct 12 ms 9984 KB Output is correct
29 Correct 15 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1039 ms 39192 KB Output is correct
2 Correct 287 ms 39472 KB Output is correct
3 Correct 1014 ms 39268 KB Output is correct
4 Correct 293 ms 39704 KB Output is correct
5 Correct 985 ms 37220 KB Output is correct
6 Correct 285 ms 39524 KB Output is correct
7 Correct 974 ms 37476 KB Output is correct
8 Correct 310 ms 39284 KB Output is correct
9 Correct 980 ms 36044 KB Output is correct
10 Correct 1002 ms 34532 KB Output is correct
11 Correct 978 ms 36196 KB Output is correct
12 Correct 982 ms 37732 KB Output is correct
13 Correct 966 ms 34148 KB Output is correct
14 Correct 991 ms 38124 KB Output is correct
15 Correct 989 ms 37732 KB Output is correct
16 Correct 971 ms 34916 KB Output is correct
17 Correct 974 ms 35172 KB Output is correct
18 Correct 973 ms 35300 KB Output is correct
19 Correct 980 ms 36964 KB Output is correct
20 Correct 989 ms 38372 KB Output is correct
21 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 12 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 10 ms 9856 KB Output is correct
11 Correct 12 ms 9984 KB Output is correct
12 Correct 12 ms 9984 KB Output is correct
13 Correct 12 ms 9984 KB Output is correct
14 Correct 12 ms 9984 KB Output is correct
15 Correct 12 ms 10112 KB Output is correct
16 Correct 12 ms 9984 KB Output is correct
17 Correct 12 ms 9984 KB Output is correct
18 Correct 12 ms 9984 KB Output is correct
19 Correct 12 ms 10112 KB Output is correct
20 Correct 12 ms 9984 KB Output is correct
21 Correct 11 ms 10112 KB Output is correct
22 Correct 12 ms 10112 KB Output is correct
23 Correct 13 ms 9980 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 13 ms 9984 KB Output is correct
26 Correct 13 ms 9984 KB Output is correct
27 Correct 16 ms 9984 KB Output is correct
28 Correct 12 ms 9984 KB Output is correct
29 Correct 15 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
31 Correct 1039 ms 39192 KB Output is correct
32 Correct 287 ms 39472 KB Output is correct
33 Correct 1014 ms 39268 KB Output is correct
34 Correct 293 ms 39704 KB Output is correct
35 Correct 985 ms 37220 KB Output is correct
36 Correct 285 ms 39524 KB Output is correct
37 Correct 974 ms 37476 KB Output is correct
38 Correct 310 ms 39284 KB Output is correct
39 Correct 980 ms 36044 KB Output is correct
40 Correct 1002 ms 34532 KB Output is correct
41 Correct 978 ms 36196 KB Output is correct
42 Correct 982 ms 37732 KB Output is correct
43 Correct 966 ms 34148 KB Output is correct
44 Correct 991 ms 38124 KB Output is correct
45 Correct 989 ms 37732 KB Output is correct
46 Correct 971 ms 34916 KB Output is correct
47 Correct 974 ms 35172 KB Output is correct
48 Correct 973 ms 35300 KB Output is correct
49 Correct 980 ms 36964 KB Output is correct
50 Correct 989 ms 38372 KB Output is correct
51 Correct 10 ms 9728 KB Output is correct
52 Correct 643 ms 30948 KB Output is correct
53 Correct 688 ms 30820 KB Output is correct
54 Correct 668 ms 30820 KB Output is correct
55 Correct 695 ms 30960 KB Output is correct
56 Correct 688 ms 30948 KB Output is correct
57 Correct 667 ms 30948 KB Output is correct
58 Correct 753 ms 32612 KB Output is correct
59 Correct 755 ms 32872 KB Output is correct
60 Correct 991 ms 32484 KB Output is correct
61 Correct 933 ms 32104 KB Output is correct
62 Correct 288 ms 39524 KB Output is correct
63 Correct 289 ms 39524 KB Output is correct
64 Correct 293 ms 39500 KB Output is correct
65 Correct 288 ms 39396 KB Output is correct
66 Correct 532 ms 34916 KB Output is correct
67 Correct 517 ms 34832 KB Output is correct
68 Correct 522 ms 34792 KB Output is correct
69 Correct 537 ms 34912 KB Output is correct
70 Correct 530 ms 34792 KB Output is correct
71 Correct 538 ms 34916 KB Output is correct
72 Correct 547 ms 34912 KB Output is correct
73 Correct 530 ms 34276 KB Output is correct
74 Correct 556 ms 34792 KB Output is correct
75 Correct 550 ms 34792 KB Output is correct
76 Correct 899 ms 35048 KB Output is correct
77 Correct 933 ms 34020 KB Output is correct
78 Correct 982 ms 35048 KB Output is correct
79 Correct 979 ms 33764 KB Output is correct
80 Correct 981 ms 38120 KB Output is correct
81 Correct 987 ms 36076 KB Output is correct
82 Correct 1006 ms 36068 KB Output is correct
83 Correct 988 ms 34152 KB Output is correct
84 Correct 999 ms 37604 KB Output is correct
85 Correct 981 ms 36708 KB Output is correct
86 Correct 963 ms 33764 KB Output is correct
87 Correct 994 ms 35044 KB Output is correct
88 Correct 899 ms 35940 KB Output is correct
89 Correct 888 ms 33764 KB Output is correct
90 Correct 903 ms 33740 KB Output is correct
91 Correct 911 ms 35172 KB Output is correct
92 Correct 883 ms 34148 KB Output is correct
93 Correct 874 ms 34152 KB Output is correct
94 Correct 914 ms 33740 KB Output is correct
95 Correct 911 ms 34532 KB Output is correct
96 Correct 917 ms 33680 KB Output is correct
97 Correct 886 ms 34916 KB Output is correct