Submission #216829

# Submission time Handle Problem Language Result Execution time Memory
216829 2020-03-28T06:45:02 Z IOrtroiii Capital City (JOI20_capital_city) C++14
100 / 100
691 ms 36072 KB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200200;

int N, K;
vector<int> adj[MAXN];
int city[MAXN];
int par[MAXN];
vector<int> towns[MAXN];
int sz[MAXN];
bool delt[MAXN];
vector<int> curComp;
int ans = MAXN;

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

int findCentr(int v, int p, int r) {
   for (int u : adj[v]) if (u != p && !delt[u] && 2 * sz[u] >= sz[r]) {
      return findCentr(u, v, r);
   }
   return v;
}

int dfsPar(int v, int p) {
   par[v] = p;
   curComp.emplace_back(v);
   for (int u : adj[v]) if (u != p && !delt[u]) {
      dfsPar(u, v);
   }
}

int root[MAXN];

int getRoot(int v) {
   if (root[v] != v) {
      root[v] = getRoot(root[v]);
   }
   return root[v];
}

bool merge(int v, int u) {
   v = getRoot(v);
   u = getRoot(u);
   if (v == u) {
      return false;
   } else {
      root[u] = v;
      return true;
   }
}

int cnt[MAXN];

void solve(int v) {
   dfsSz(v, v);
   v = findCentr(v, v, v);
   dfsPar(v, v);
   for (int u : curComp) {
      ++cnt[city[u]];
   }
   bool good = cnt[city[v]] == int(towns[city[v]].size());
   int cans = 0;
   queue<int> q;
   if (good) {
      for (int u : towns[city[v]]) {
      q.emplace(u);
      }
      while (!q.empty() && good) {
         int u = q.front(); q.pop();
         if (merge(city[u], city[par[u]])) {
            good &= (cnt[city[par[u]]] == int(towns[city[par[u]]].size()));
            if (!good) break;
            cans++;
            for (int z : towns[city[par[u]]]) {
               q.emplace(z);
            }
         }
      }
   }
   if (good) ans = min(ans, cans);
   for (int z : curComp) {
      root[city[z]] = city[z];
      cnt[city[z]] = 0;
   }
   curComp = {};
   delt[v] = true;
   for (int u : adj[v]) if (!delt[u]) solve(u);
}

int main() {
   ios_base::sync_with_stdio(false); cin.tie(nullptr);
   cin >> N >> K;
   for (int i = 1; i < N; ++i) {
      int v, u;
      cin >> v >> u;
      adj[v].emplace_back(u);
      adj[u].emplace_back(v);
   }
   for (int v = 1; v <= N; ++v) {
      cin >> city[v];
      towns[city[v]].emplace_back(v);
   }
   iota(root + 1, root + K + 1, 1);
   solve(1);
   cout << ans << "\n";
   return 0;
}

Compilation message

capital_city.cpp: In function 'int dfsPar(int, int)':
capital_city.cpp:38:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9728 KB Output is correct
2 Correct 9 ms 9728 KB Output is correct
3 Correct 9 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 9 ms 9856 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9728 KB Output is correct
2 Correct 9 ms 9728 KB Output is correct
3 Correct 9 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 9 ms 9856 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 11 ms 9856 KB Output is correct
12 Correct 13 ms 9984 KB Output is correct
13 Correct 12 ms 9984 KB Output is correct
14 Correct 11 ms 9984 KB Output is correct
15 Correct 13 ms 9984 KB Output is correct
16 Correct 11 ms 9984 KB Output is correct
17 Correct 11 ms 9984 KB Output is correct
18 Correct 11 ms 9984 KB Output is correct
19 Correct 12 ms 9984 KB Output is correct
20 Correct 11 ms 9984 KB Output is correct
21 Correct 11 ms 9984 KB Output is correct
22 Correct 12 ms 10112 KB Output is correct
23 Correct 11 ms 9984 KB Output is correct
24 Correct 13 ms 10112 KB Output is correct
25 Correct 12 ms 9984 KB Output is correct
26 Correct 12 ms 9984 KB Output is correct
27 Correct 12 ms 9984 KB Output is correct
28 Correct 12 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 611 ms 35568 KB Output is correct
2 Correct 268 ms 35948 KB Output is correct
3 Correct 615 ms 35320 KB Output is correct
4 Correct 268 ms 35948 KB Output is correct
5 Correct 608 ms 32748 KB Output is correct
6 Correct 279 ms 36072 KB Output is correct
7 Correct 609 ms 33008 KB Output is correct
8 Correct 280 ms 35564 KB Output is correct
9 Correct 677 ms 31176 KB Output is correct
10 Correct 672 ms 29292 KB Output is correct
11 Correct 679 ms 31840 KB Output is correct
12 Correct 688 ms 33640 KB Output is correct
13 Correct 688 ms 28912 KB Output is correct
14 Correct 675 ms 33840 KB Output is correct
15 Correct 677 ms 33516 KB Output is correct
16 Correct 664 ms 29712 KB Output is correct
17 Correct 672 ms 30056 KB Output is correct
18 Correct 673 ms 30184 KB Output is correct
19 Correct 668 ms 32784 KB Output is correct
20 Correct 669 ms 34540 KB Output is correct
21 Correct 11 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9728 KB Output is correct
2 Correct 9 ms 9728 KB Output is correct
3 Correct 9 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 9 ms 9856 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 11 ms 9856 KB Output is correct
12 Correct 13 ms 9984 KB Output is correct
13 Correct 12 ms 9984 KB Output is correct
14 Correct 11 ms 9984 KB Output is correct
15 Correct 13 ms 9984 KB Output is correct
16 Correct 11 ms 9984 KB Output is correct
17 Correct 11 ms 9984 KB Output is correct
18 Correct 11 ms 9984 KB Output is correct
19 Correct 12 ms 9984 KB Output is correct
20 Correct 11 ms 9984 KB Output is correct
21 Correct 11 ms 9984 KB Output is correct
22 Correct 12 ms 10112 KB Output is correct
23 Correct 11 ms 9984 KB Output is correct
24 Correct 13 ms 10112 KB Output is correct
25 Correct 12 ms 9984 KB Output is correct
26 Correct 12 ms 9984 KB Output is correct
27 Correct 12 ms 9984 KB Output is correct
28 Correct 12 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
31 Correct 611 ms 35568 KB Output is correct
32 Correct 268 ms 35948 KB Output is correct
33 Correct 615 ms 35320 KB Output is correct
34 Correct 268 ms 35948 KB Output is correct
35 Correct 608 ms 32748 KB Output is correct
36 Correct 279 ms 36072 KB Output is correct
37 Correct 609 ms 33008 KB Output is correct
38 Correct 280 ms 35564 KB Output is correct
39 Correct 677 ms 31176 KB Output is correct
40 Correct 672 ms 29292 KB Output is correct
41 Correct 679 ms 31840 KB Output is correct
42 Correct 688 ms 33640 KB Output is correct
43 Correct 688 ms 28912 KB Output is correct
44 Correct 675 ms 33840 KB Output is correct
45 Correct 677 ms 33516 KB Output is correct
46 Correct 664 ms 29712 KB Output is correct
47 Correct 672 ms 30056 KB Output is correct
48 Correct 673 ms 30184 KB Output is correct
49 Correct 668 ms 32784 KB Output is correct
50 Correct 669 ms 34540 KB Output is correct
51 Correct 11 ms 9728 KB Output is correct
52 Correct 455 ms 21872 KB Output is correct
53 Correct 487 ms 21872 KB Output is correct
54 Correct 475 ms 21864 KB Output is correct
55 Correct 500 ms 21868 KB Output is correct
56 Correct 477 ms 21864 KB Output is correct
57 Correct 471 ms 21868 KB Output is correct
58 Correct 487 ms 24684 KB Output is correct
59 Correct 496 ms 25072 KB Output is correct
60 Correct 596 ms 24680 KB Output is correct
61 Correct 596 ms 24428 KB Output is correct
62 Correct 275 ms 35872 KB Output is correct
63 Correct 269 ms 35952 KB Output is correct
64 Correct 271 ms 35560 KB Output is correct
65 Correct 271 ms 35896 KB Output is correct
66 Correct 376 ms 28716 KB Output is correct
67 Correct 379 ms 28736 KB Output is correct
68 Correct 378 ms 28716 KB Output is correct
69 Correct 371 ms 28716 KB Output is correct
70 Correct 398 ms 28716 KB Output is correct
71 Correct 366 ms 28716 KB Output is correct
72 Correct 380 ms 28716 KB Output is correct
73 Correct 369 ms 28024 KB Output is correct
74 Correct 368 ms 28712 KB Output is correct
75 Correct 401 ms 28716 KB Output is correct
76 Correct 617 ms 27956 KB Output is correct
77 Correct 605 ms 26416 KB Output is correct
78 Correct 682 ms 29936 KB Output is correct
79 Correct 684 ms 28520 KB Output is correct
80 Correct 683 ms 34156 KB Output is correct
81 Correct 690 ms 31344 KB Output is correct
82 Correct 662 ms 31336 KB Output is correct
83 Correct 670 ms 28652 KB Output is correct
84 Correct 680 ms 33520 KB Output is correct
85 Correct 677 ms 32108 KB Output is correct
86 Correct 691 ms 28272 KB Output is correct
87 Correct 671 ms 29788 KB Output is correct
88 Correct 590 ms 30648 KB Output is correct
89 Correct 581 ms 27504 KB Output is correct
90 Correct 561 ms 27372 KB Output is correct
91 Correct 579 ms 29328 KB Output is correct
92 Correct 582 ms 28208 KB Output is correct
93 Correct 574 ms 27888 KB Output is correct
94 Correct 579 ms 27500 KB Output is correct
95 Correct 610 ms 28684 KB Output is correct
96 Correct 605 ms 27624 KB Output is correct
97 Correct 568 ms 29160 KB Output is correct