Submission #225216

# Submission time Handle Problem Language Result Execution time Memory
225216 2020-04-19T15:03:22 Z quocnguyen1012 Capital City (JOI20_capital_city) C++14
100 / 100
982 ms 40936 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 2e5 + 5, inf = 1e9;

int N, K;
vector<int> same[maxn], adj[maxn];
int sz[maxn], del[maxn], vis[maxn], cvis[maxn], col[maxn], par[maxn];
vector<int> vec;
int ret = inf, num[maxn];

void prepare(int u, int p)
{
  vec.pb(col[u]);
  par[u] = p;
  same[col[u]].pb(u);
  vis[u] = cvis[col[u]] = 0;
  for(int v : adj[u]) if(v != p && !del[v]){
    prepare(v, u);
  }
}

void findsz(int u, int p)
{
  sz[u] = 1;
  for(int v : adj[u]) if(v != p && !del[v]){
    findsz(v, u);
    sz[u] += sz[v];
  }
}

int findcen(int u, int p, int siz)
{
  for(int v : adj[u]) if(v != p && !del[v]){
    if(sz[v] > siz / 2) return findcen(v, u, siz);
  }
  return u;
}

void decomp(int u)
{
  findsz(u, u);
  u = findcen(u, u, sz[u]);
  prepare(u, u);
  del[u] = true;

  queue<int> q;
  q.push(col[u]);
  cvis[col[u]] = true;
  int cnt = 0;
  while(q.size()){
    int c = q.front(); q.pop();
    ++cnt;
    for(int v : same[c]){
      while(!vis[v]){
        vis[v] = true;
        if(!cvis[col[v]]){
          cvis[col[v]] = true;
          q.push(col[v]);
        }
        v = par[v];
      }
    }
  }
  bool flag = true;
  for(int c : vec){
    if(cvis[c] && num[c] != same[c].size())
      flag = false;
  }
  //cerr << u << ' ' << cnt << '\n';
  if(flag)
    ret = min(ret, cnt - 1);
  for(int c : vec)
    same[c].clear();
  vec.clear();
  for(int v : adj[u])
    if(!del[v])
      decomp(v);
}

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  cin >> N >> K;
  for(int i = 1; i < N; ++i){
    int u, v; cin >> u >> v;
    adj[u].eb(v); adj[v].eb(u);
  }
  for(int i = 1; i <= N; ++i){
    cin >> col[i];
    ++num[col[i]];
  }
  decomp(1);
  cout << ret;
}

Compilation message

capital_city.cpp: In function 'void decomp(int)':
capital_city.cpp:77:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(cvis[c] && num[c] != same[c].size())
                   ~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9856 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 10 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 11 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9856 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 10 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 11 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 13 ms 9984 KB Output is correct
12 Correct 13 ms 9904 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 9984 KB Output is correct
16 Correct 16 ms 9984 KB Output is correct
17 Correct 13 ms 9984 KB Output is correct
18 Correct 12 ms 9984 KB Output is correct
19 Correct 12 ms 9984 KB Output is correct
20 Correct 16 ms 9984 KB Output is correct
21 Correct 12 ms 9984 KB Output is correct
22 Correct 12 ms 10112 KB Output is correct
23 Correct 13 ms 9984 KB Output is correct
24 Correct 13 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 12 ms 10112 KB Output is correct
28 Correct 13 ms 9984 KB Output is correct
29 Correct 13 ms 9984 KB Output is correct
30 Correct 16 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 905 ms 40428 KB Output is correct
2 Correct 290 ms 40784 KB Output is correct
3 Correct 894 ms 40168 KB Output is correct
4 Correct 295 ms 40812 KB Output is correct
5 Correct 982 ms 37756 KB Output is correct
6 Correct 280 ms 40788 KB Output is correct
7 Correct 843 ms 37992 KB Output is correct
8 Correct 316 ms 40336 KB Output is correct
9 Correct 898 ms 36204 KB Output is correct
10 Correct 883 ms 34152 KB Output is correct
11 Correct 878 ms 36720 KB Output is correct
12 Correct 888 ms 38576 KB Output is correct
13 Correct 890 ms 33768 KB Output is correct
14 Correct 885 ms 38764 KB Output is correct
15 Correct 913 ms 38504 KB Output is correct
16 Correct 890 ms 34668 KB Output is correct
17 Correct 873 ms 35048 KB Output is correct
18 Correct 863 ms 35048 KB Output is correct
19 Correct 877 ms 37612 KB Output is correct
20 Correct 909 ms 39528 KB Output is correct
21 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9856 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 10 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 11 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 13 ms 9984 KB Output is correct
12 Correct 13 ms 9904 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 9984 KB Output is correct
16 Correct 16 ms 9984 KB Output is correct
17 Correct 13 ms 9984 KB Output is correct
18 Correct 12 ms 9984 KB Output is correct
19 Correct 12 ms 9984 KB Output is correct
20 Correct 16 ms 9984 KB Output is correct
21 Correct 12 ms 9984 KB Output is correct
22 Correct 12 ms 10112 KB Output is correct
23 Correct 13 ms 9984 KB Output is correct
24 Correct 13 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 12 ms 10112 KB Output is correct
28 Correct 13 ms 9984 KB Output is correct
29 Correct 13 ms 9984 KB Output is correct
30 Correct 16 ms 9984 KB Output is correct
31 Correct 905 ms 40428 KB Output is correct
32 Correct 290 ms 40784 KB Output is correct
33 Correct 894 ms 40168 KB Output is correct
34 Correct 295 ms 40812 KB Output is correct
35 Correct 982 ms 37756 KB Output is correct
36 Correct 280 ms 40788 KB Output is correct
37 Correct 843 ms 37992 KB Output is correct
38 Correct 316 ms 40336 KB Output is correct
39 Correct 898 ms 36204 KB Output is correct
40 Correct 883 ms 34152 KB Output is correct
41 Correct 878 ms 36720 KB Output is correct
42 Correct 888 ms 38576 KB Output is correct
43 Correct 890 ms 33768 KB Output is correct
44 Correct 885 ms 38764 KB Output is correct
45 Correct 913 ms 38504 KB Output is correct
46 Correct 890 ms 34668 KB Output is correct
47 Correct 873 ms 35048 KB Output is correct
48 Correct 863 ms 35048 KB Output is correct
49 Correct 877 ms 37612 KB Output is correct
50 Correct 909 ms 39528 KB Output is correct
51 Correct 10 ms 9728 KB Output is correct
52 Correct 548 ms 26196 KB Output is correct
53 Correct 620 ms 26092 KB Output is correct
54 Correct 593 ms 26092 KB Output is correct
55 Correct 616 ms 26220 KB Output is correct
56 Correct 610 ms 26332 KB Output is correct
57 Correct 589 ms 26092 KB Output is correct
58 Correct 626 ms 29676 KB Output is correct
59 Correct 596 ms 30060 KB Output is correct
60 Correct 751 ms 29676 KB Output is correct
61 Correct 785 ms 29416 KB Output is correct
62 Correct 289 ms 40812 KB Output is correct
63 Correct 293 ms 40896 KB Output is correct
64 Correct 295 ms 40552 KB Output is correct
65 Correct 301 ms 40936 KB Output is correct
66 Correct 438 ms 33900 KB Output is correct
67 Correct 440 ms 33644 KB Output is correct
68 Correct 438 ms 33772 KB Output is correct
69 Correct 459 ms 33900 KB Output is correct
70 Correct 441 ms 33768 KB Output is correct
71 Correct 432 ms 33808 KB Output is correct
72 Correct 445 ms 33772 KB Output is correct
73 Correct 438 ms 33128 KB Output is correct
74 Correct 449 ms 33768 KB Output is correct
75 Correct 450 ms 33724 KB Output is correct
76 Correct 673 ms 32876 KB Output is correct
77 Correct 678 ms 31340 KB Output is correct
78 Correct 850 ms 34920 KB Output is correct
79 Correct 852 ms 33260 KB Output is correct
80 Correct 838 ms 39016 KB Output is correct
81 Correct 850 ms 36144 KB Output is correct
82 Correct 842 ms 36332 KB Output is correct
83 Correct 848 ms 33640 KB Output is correct
84 Correct 847 ms 38380 KB Output is correct
85 Correct 886 ms 37124 KB Output is correct
86 Correct 849 ms 33388 KB Output is correct
87 Correct 843 ms 34892 KB Output is correct
88 Correct 783 ms 35612 KB Output is correct
89 Correct 754 ms 32488 KB Output is correct
90 Correct 759 ms 32232 KB Output is correct
91 Correct 760 ms 34152 KB Output is correct
92 Correct 769 ms 33128 KB Output is correct
93 Correct 745 ms 33080 KB Output is correct
94 Correct 754 ms 32348 KB Output is correct
95 Correct 743 ms 33644 KB Output is correct
96 Correct 768 ms 32520 KB Output is correct
97 Correct 755 ms 34028 KB Output is correct