Submission #235711

# Submission time Handle Problem Language Result Execution time Memory
235711 2020-05-29T12:55:41 Z shashwatchandra Capital City (JOI20_capital_city) C++17
100 / 100
1202 ms 55272 KB
/*input
12 4
7 9
1 3
4 6
2 4
10 12
1 2
2 10
11 1
2 8
5 3
6 7
3
1
1
2
4
3
3
2
2
3
4
4
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long 
#define double long double
#define f first
#define s second
#define mp make_pair
#define pb push_back

#define RE(i,n) for (int i = 1; i <= n; i++)
#define RED(i,n) for (int i = n; i > 0; i--)
#define REPS(i,n) for(int i = 1; (i*i) <= n; i++)
#define REP(i,n) for (int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REPD(i,n) for (int i = n-1; i >= 0; i--)
#define FORD(i,a,b) for (int i = a; i >= b; i--)

#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;
#define debug(x) cout << x << endl;
#define debug2(x,y) cout << x << " " << y << endl;
#define debug3(x,y,z) cout << x << " " << y << " " << z << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int INF = 1e18+1;
const int MOD = 1e9+7;
const double PI = 3.14159265358979323846264338;

int raise(int a,int n,int m = MOD){
  if(n == 0)return 1;
  if(n == 1)return a;
  int x = 1;
    x *= raise(a,n/2,m);
    x %= m;
    x *= x;
    x %= m;
    if(n%2)x*= a;
    x %= m;
    return x;
}

int floor1(int n,int k){
    if(n%k == 0 || n >= 0)return n/k;
    return (n/k)-1;
}

int ceil1(int n,int k){
    return floor1(n+k-1,k);
}

const int N = 2e5+1;
int n;
int k;
int col[N];
vector<int> all[N];
bool broken[N];
int freq[N];
int par[N];
set<int> adj[N];
int nn = 0;
int sub[N];
bool thisdone[N];
bool fine = 1;
int ans = INF;
int cur;
queue<int> q;

void dfs1(int u,int p){
    nn++;
    freq[col[u]]++;
    sub[u] = 1;
    for(int v:adj[u]){
      if(v == p)continue;
      dfs1(v,u);
      sub[u] += sub[v];
    }
}

void closecheck(int u,int p){
    if(freq[col[u]] != all[col[u]].size())broken[col[u]] = 1;
    par[u] = p;
    for(int v:adj[u]){
      if(v == p)continue;
      closecheck(v,u);
    }
}

void dellnow(int u,int p){
    freq[col[u]]--;
    thisdone[col[u]] = 0;
    for(int v:adj[u]){
      if(v == p)continue;
      dellnow(v,u);
    }
}

int findcen(int u,int p){
    for(int v:adj[u]){
      if(v == p)continue;
      if(sub[v] > nn/2)return findcen(v,u);
    }
    return u;
}


void addcolor(int c){
    if(broken[c]){fine = 0;return;}
    if(thisdone[c])return;
    thisdone[c] = 1;
    cur++;
    for(int v:all[c]){
      q.push(v);
    }
}

void cendfs(int u,int p = -1){
    nn = 0;
    dfs1(u,-1);
    int centroid = findcen(u,-1);
    // check here
    closecheck(centroid,-1);
    //cout << centroid << endl;
    fine = 1;
    cur = 0;
    addcolor(col[centroid]);
    while(!q.empty()){
      int x= q.front();
      q.pop();
      if(par[x] != -1)addcolor(col[par[x]]);
    }
    //cout << fine << " " << cur << endl;
    if(fine)ans = min(ans,cur);
    // 
    dellnow(centroid,-1);
    for(int x:adj[centroid]){
      adj[x].erase(centroid);
      cendfs(x,centroid);
    }
}

void solve(){
    cin >> n >> k;
    RE(i,n-1){
      int a,b;cin >> a >> b;
      adj[a].insert(b);
      adj[b].insert(a);
    }
    RE(i,n){
      cin >> col[i];
      all[col[i]].pb(i);
    }
    cendfs(1,-1);
    cout << ans-1;
}

signed main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  //freopen(".in","r",stdin);freopen(".out","w",stdout);
  int t = 1;
  //cin >> t;
  while(t--){
    solve();
  }
  return 0;
}

Compilation message

capital_city.cpp: In function 'void closecheck(long long int, long long int)':
capital_city.cpp:120:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(freq[col[u]] != all[col[u]].size())broken[col[u]] = 1;
        ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 13 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 13 ms 14464 KB Output is correct
10 Correct 13 ms 14336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 13 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 13 ms 14464 KB Output is correct
10 Correct 13 ms 14336 KB Output is correct
11 Correct 15 ms 14720 KB Output is correct
12 Correct 15 ms 14720 KB Output is correct
13 Correct 15 ms 14720 KB Output is correct
14 Correct 15 ms 14720 KB Output is correct
15 Correct 15 ms 14720 KB Output is correct
16 Correct 16 ms 14720 KB Output is correct
17 Correct 15 ms 14720 KB Output is correct
18 Correct 15 ms 14848 KB Output is correct
19 Correct 15 ms 14848 KB Output is correct
20 Correct 15 ms 14848 KB Output is correct
21 Correct 15 ms 14848 KB Output is correct
22 Correct 15 ms 14848 KB Output is correct
23 Correct 16 ms 14848 KB Output is correct
24 Correct 16 ms 14848 KB Output is correct
25 Correct 16 ms 14848 KB Output is correct
26 Correct 19 ms 14848 KB Output is correct
27 Correct 16 ms 14848 KB Output is correct
28 Correct 17 ms 14848 KB Output is correct
29 Correct 16 ms 14720 KB Output is correct
30 Correct 15 ms 14720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1072 ms 51180 KB Output is correct
2 Correct 310 ms 51320 KB Output is correct
3 Correct 1061 ms 51064 KB Output is correct
4 Correct 309 ms 51576 KB Output is correct
5 Correct 1038 ms 49060 KB Output is correct
6 Correct 318 ms 55272 KB Output is correct
7 Correct 1043 ms 52980 KB Output is correct
8 Correct 322 ms 54788 KB Output is correct
9 Correct 1167 ms 51988 KB Output is correct
10 Correct 1158 ms 50552 KB Output is correct
11 Correct 1173 ms 52372 KB Output is correct
12 Correct 1161 ms 53752 KB Output is correct
13 Correct 1139 ms 50156 KB Output is correct
14 Correct 1178 ms 54008 KB Output is correct
15 Correct 1177 ms 53700 KB Output is correct
16 Correct 1150 ms 50808 KB Output is correct
17 Correct 1148 ms 51192 KB Output is correct
18 Correct 1140 ms 51560 KB Output is correct
19 Correct 1202 ms 53060 KB Output is correct
20 Correct 1185 ms 54396 KB Output is correct
21 Correct 13 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 13 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14464 KB Output is correct
9 Correct 13 ms 14464 KB Output is correct
10 Correct 13 ms 14336 KB Output is correct
11 Correct 15 ms 14720 KB Output is correct
12 Correct 15 ms 14720 KB Output is correct
13 Correct 15 ms 14720 KB Output is correct
14 Correct 15 ms 14720 KB Output is correct
15 Correct 15 ms 14720 KB Output is correct
16 Correct 16 ms 14720 KB Output is correct
17 Correct 15 ms 14720 KB Output is correct
18 Correct 15 ms 14848 KB Output is correct
19 Correct 15 ms 14848 KB Output is correct
20 Correct 15 ms 14848 KB Output is correct
21 Correct 15 ms 14848 KB Output is correct
22 Correct 15 ms 14848 KB Output is correct
23 Correct 16 ms 14848 KB Output is correct
24 Correct 16 ms 14848 KB Output is correct
25 Correct 16 ms 14848 KB Output is correct
26 Correct 19 ms 14848 KB Output is correct
27 Correct 16 ms 14848 KB Output is correct
28 Correct 17 ms 14848 KB Output is correct
29 Correct 16 ms 14720 KB Output is correct
30 Correct 15 ms 14720 KB Output is correct
31 Correct 1072 ms 51180 KB Output is correct
32 Correct 310 ms 51320 KB Output is correct
33 Correct 1061 ms 51064 KB Output is correct
34 Correct 309 ms 51576 KB Output is correct
35 Correct 1038 ms 49060 KB Output is correct
36 Correct 318 ms 55272 KB Output is correct
37 Correct 1043 ms 52980 KB Output is correct
38 Correct 322 ms 54788 KB Output is correct
39 Correct 1167 ms 51988 KB Output is correct
40 Correct 1158 ms 50552 KB Output is correct
41 Correct 1173 ms 52372 KB Output is correct
42 Correct 1161 ms 53752 KB Output is correct
43 Correct 1139 ms 50156 KB Output is correct
44 Correct 1178 ms 54008 KB Output is correct
45 Correct 1177 ms 53700 KB Output is correct
46 Correct 1150 ms 50808 KB Output is correct
47 Correct 1148 ms 51192 KB Output is correct
48 Correct 1140 ms 51560 KB Output is correct
49 Correct 1202 ms 53060 KB Output is correct
50 Correct 1185 ms 54396 KB Output is correct
51 Correct 13 ms 14464 KB Output is correct
52 Correct 815 ms 45404 KB Output is correct
53 Correct 916 ms 45552 KB Output is correct
54 Correct 866 ms 45432 KB Output is correct
55 Correct 918 ms 45432 KB Output is correct
56 Correct 894 ms 45460 KB Output is correct
57 Correct 872 ms 45456 KB Output is correct
58 Correct 918 ms 46968 KB Output is correct
59 Correct 796 ms 47224 KB Output is correct
60 Correct 1010 ms 46840 KB Output is correct
61 Correct 1125 ms 46456 KB Output is correct
62 Correct 308 ms 55160 KB Output is correct
63 Correct 328 ms 55124 KB Output is correct
64 Correct 330 ms 54944 KB Output is correct
65 Correct 313 ms 55092 KB Output is correct
66 Correct 739 ms 50104 KB Output is correct
67 Correct 732 ms 49900 KB Output is correct
68 Correct 747 ms 50152 KB Output is correct
69 Correct 742 ms 50028 KB Output is correct
70 Correct 722 ms 50028 KB Output is correct
71 Correct 732 ms 49872 KB Output is correct
72 Correct 739 ms 49900 KB Output is correct
73 Correct 721 ms 49516 KB Output is correct
74 Correct 754 ms 50092 KB Output is correct
75 Correct 985 ms 49896 KB Output is correct
76 Correct 1199 ms 49272 KB Output is correct
77 Correct 1058 ms 48124 KB Output is correct
78 Correct 1157 ms 50984 KB Output is correct
79 Correct 1139 ms 49656 KB Output is correct
80 Correct 1135 ms 54252 KB Output is correct
81 Correct 1190 ms 52072 KB Output is correct
82 Correct 1160 ms 52024 KB Output is correct
83 Correct 1130 ms 50088 KB Output is correct
84 Correct 1179 ms 53496 KB Output is correct
85 Correct 1171 ms 52696 KB Output is correct
86 Correct 1173 ms 49784 KB Output is correct
87 Correct 1193 ms 50932 KB Output is correct
88 Correct 1069 ms 51320 KB Output is correct
89 Correct 1043 ms 49144 KB Output is correct
90 Correct 1010 ms 49124 KB Output is correct
91 Correct 1030 ms 50356 KB Output is correct
92 Correct 1079 ms 49528 KB Output is correct
93 Correct 1074 ms 49512 KB Output is correct
94 Correct 1059 ms 48972 KB Output is correct
95 Correct 1032 ms 49980 KB Output is correct
96 Correct 1055 ms 49016 KB Output is correct
97 Correct 1065 ms 50356 KB Output is correct