Submission #217169

# Submission time Handle Problem Language Result Execution time Memory
217169 2020-03-29T07:15:14 Z rama_pang Capital City (JOI20_capital_city) C++14
100 / 100
898 ms 38524 KB
#include <bits/stdc++.h>
using namespace std;

class CapitalCity {
 
 private:

  int N, K;
  vector<int> color;

  vector<vector<int>> adj;
  vector<vector<int>> color_adj;
  
  class Visited {

   private:

    int visited_mark = 1;
    vector<int> visited;

   public:

    Visited() {}

    void Initialize(int n) {
      visited.resize(n);
    }

    void Clear() {
      visited_mark++;
    }

    bool IsIt(int n) {
      return n == -1 || visited[n] == visited_mark;
    }

    void Mark(int n) {
      if (n != -1) visited[n] = visited_mark;
    }

  };

  Visited visited, reachable;

  vector<int> parent;
  vector<int> sz;
  vector<bool> deleted;

  void Dfs(int n, bool r = true) {
    if (r) {
      assert(!deleted[n]);
      parent[n] = -1;
      reachable.Clear();
      reachable.Mark(n);
    }

    sz[n] = 1;
    for (auto &i : adj[n]) if (i != parent[n] && !deleted[i]) {
      reachable.Mark(i);
      parent[i] = n;
      Dfs(i, false);
      sz[n] += sz[i];
    }
  }

  int SolveColor(int root, int k) {
    assert(color[root] == k);
    Dfs(root);

    queue<int> q;
    visited.Clear();

    for (auto &c : color_adj[k]) {
      if (reachable.IsIt(c)) {
        q.emplace(c);
        visited.Mark(c);
      } else {
        return -1;
      }
    }

    int ans = 0;

    while (!q.empty()) {
      int u = q.front();
      q.pop();
      if (!visited.IsIt(parent[u])) {
        ans++;
        for (auto &c : color_adj[color[parent[u]]]) {
          if (reachable.IsIt(c)) {
            q.emplace(c);
            visited.Mark(c);
          } else {
            return -1;
          }
        }
      }
    }

    return ans;
  }

  int Centroid(int n) {
    Dfs(n);
    int cur = n;
    while (true) {
      pair<int, int> mx = {-1, -1};
      for (auto &i : adj[cur]) if (i != parent[cur] && !deleted[i]) {
        mx = max(mx, {sz[i], i});
      }
      if (mx.first * 2 <= sz[n]) {
        return cur;
      }
      cur = mx.second;
    }
  }

  int CentroidDecomposition() {
    set<int> can;
    for (int i = 0; i < N; i++) {
      can.emplace(i);
    }

    auto Delete = [&](int x) {
      for (auto &c : color_adj[x]) {
        can.erase(c);
        deleted[c] = true;
      }
    };
    
    int ans = N - 1;

    while (!can.empty()) {
      int u = *begin(can);
      u = Centroid(u);
      int current = SolveColor(u, color[u]);
      if (current != -1) ans = min(ans, current);
      Delete(color[u]);
    }

    return ans;
  }

  void Init() {
    visited.Initialize(N);
    reachable.Initialize(N);

    parent.resize(N);
    sz.resize(N);
    deleted.resize(N);
  }

 public:

  void Read() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    cin >> N >> K;

    adj.resize(N);
    color.resize(N);
    color_adj.resize(K);

    for (int i = 0; i + 1 < N; i++) {
      int u, v;
      cin >> u >> v;
      u--, v--;
      adj[u].emplace_back(v);
      adj[v].emplace_back(u);
    }

    for (int i = 0; i < N; i++) {
      cin >> color[i];
      color[i]--;
      color_adj[color[i]].emplace_back(i);
    }
  }

  int Solve() {
    Init();
    return CentroidDecomposition();
  }

};

int main() {
  CapitalCity Solver;
  Solver.Read();
  cout << Solver.Solve() << "\n";
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 6 ms 640 KB Output is correct
12 Correct 6 ms 640 KB Output is correct
13 Correct 7 ms 640 KB Output is correct
14 Correct 6 ms 640 KB Output is correct
15 Correct 6 ms 640 KB Output is correct
16 Correct 6 ms 640 KB Output is correct
17 Correct 6 ms 640 KB Output is correct
18 Correct 6 ms 640 KB Output is correct
19 Correct 6 ms 640 KB Output is correct
20 Correct 6 ms 640 KB Output is correct
21 Correct 13 ms 640 KB Output is correct
22 Correct 6 ms 640 KB Output is correct
23 Correct 7 ms 640 KB Output is correct
24 Correct 7 ms 640 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 6 ms 640 KB Output is correct
27 Correct 6 ms 640 KB Output is correct
28 Correct 6 ms 640 KB Output is correct
29 Correct 6 ms 640 KB Output is correct
30 Correct 8 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 605 ms 38264 KB Output is correct
2 Correct 271 ms 38524 KB Output is correct
3 Correct 626 ms 38264 KB Output is correct
4 Correct 263 ms 38520 KB Output is correct
5 Correct 592 ms 36216 KB Output is correct
6 Correct 274 ms 38392 KB Output is correct
7 Correct 555 ms 36344 KB Output is correct
8 Correct 258 ms 38004 KB Output is correct
9 Correct 383 ms 34296 KB Output is correct
10 Correct 497 ms 32892 KB Output is correct
11 Correct 415 ms 34460 KB Output is correct
12 Correct 473 ms 35844 KB Output is correct
13 Correct 460 ms 32632 KB Output is correct
14 Correct 502 ms 36040 KB Output is correct
15 Correct 507 ms 35680 KB Output is correct
16 Correct 490 ms 33140 KB Output is correct
17 Correct 434 ms 33468 KB Output is correct
18 Correct 450 ms 33596 KB Output is correct
19 Correct 503 ms 35320 KB Output is correct
20 Correct 409 ms 36492 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 6 ms 640 KB Output is correct
12 Correct 6 ms 640 KB Output is correct
13 Correct 7 ms 640 KB Output is correct
14 Correct 6 ms 640 KB Output is correct
15 Correct 6 ms 640 KB Output is correct
16 Correct 6 ms 640 KB Output is correct
17 Correct 6 ms 640 KB Output is correct
18 Correct 6 ms 640 KB Output is correct
19 Correct 6 ms 640 KB Output is correct
20 Correct 6 ms 640 KB Output is correct
21 Correct 13 ms 640 KB Output is correct
22 Correct 6 ms 640 KB Output is correct
23 Correct 7 ms 640 KB Output is correct
24 Correct 7 ms 640 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 6 ms 640 KB Output is correct
27 Correct 6 ms 640 KB Output is correct
28 Correct 6 ms 640 KB Output is correct
29 Correct 6 ms 640 KB Output is correct
30 Correct 8 ms 640 KB Output is correct
31 Correct 605 ms 38264 KB Output is correct
32 Correct 271 ms 38524 KB Output is correct
33 Correct 626 ms 38264 KB Output is correct
34 Correct 263 ms 38520 KB Output is correct
35 Correct 592 ms 36216 KB Output is correct
36 Correct 274 ms 38392 KB Output is correct
37 Correct 555 ms 36344 KB Output is correct
38 Correct 258 ms 38004 KB Output is correct
39 Correct 383 ms 34296 KB Output is correct
40 Correct 497 ms 32892 KB Output is correct
41 Correct 415 ms 34460 KB Output is correct
42 Correct 473 ms 35844 KB Output is correct
43 Correct 460 ms 32632 KB Output is correct
44 Correct 502 ms 36040 KB Output is correct
45 Correct 507 ms 35680 KB Output is correct
46 Correct 490 ms 33140 KB Output is correct
47 Correct 434 ms 33468 KB Output is correct
48 Correct 450 ms 33596 KB Output is correct
49 Correct 503 ms 35320 KB Output is correct
50 Correct 409 ms 36492 KB Output is correct
51 Correct 4 ms 384 KB Output is correct
52 Correct 577 ms 27276 KB Output is correct
53 Correct 599 ms 27256 KB Output is correct
54 Correct 584 ms 27256 KB Output is correct
55 Correct 605 ms 27260 KB Output is correct
56 Correct 606 ms 27256 KB Output is correct
57 Correct 606 ms 27256 KB Output is correct
58 Correct 689 ms 30328 KB Output is correct
59 Correct 651 ms 30584 KB Output is correct
60 Correct 700 ms 30840 KB Output is correct
61 Correct 722 ms 30732 KB Output is correct
62 Correct 264 ms 38520 KB Output is correct
63 Correct 272 ms 38520 KB Output is correct
64 Correct 250 ms 37884 KB Output is correct
65 Correct 268 ms 38392 KB Output is correct
66 Correct 283 ms 33008 KB Output is correct
67 Correct 266 ms 32752 KB Output is correct
68 Correct 282 ms 32752 KB Output is correct
69 Correct 290 ms 33016 KB Output is correct
70 Correct 274 ms 32752 KB Output is correct
71 Correct 281 ms 32752 KB Output is correct
72 Correct 274 ms 32752 KB Output is correct
73 Correct 271 ms 32240 KB Output is correct
74 Correct 291 ms 32880 KB Output is correct
75 Correct 270 ms 32752 KB Output is correct
76 Correct 898 ms 33144 KB Output is correct
77 Correct 879 ms 32328 KB Output is correct
78 Correct 412 ms 33400 KB Output is correct
79 Correct 407 ms 32336 KB Output is correct
80 Correct 445 ms 36136 KB Output is correct
81 Correct 436 ms 34400 KB Output is correct
82 Correct 450 ms 34512 KB Output is correct
83 Correct 420 ms 32504 KB Output is correct
84 Correct 467 ms 35692 KB Output is correct
85 Correct 382 ms 34808 KB Output is correct
86 Correct 400 ms 32540 KB Output is correct
87 Correct 409 ms 33280 KB Output is correct
88 Correct 359 ms 33868 KB Output is correct
89 Correct 365 ms 31780 KB Output is correct
90 Correct 329 ms 31780 KB Output is correct
91 Correct 351 ms 33016 KB Output is correct
92 Correct 357 ms 32120 KB Output is correct
93 Correct 358 ms 32028 KB Output is correct
94 Correct 349 ms 31772 KB Output is correct
95 Correct 339 ms 32504 KB Output is correct
96 Correct 373 ms 31864 KB Output is correct
97 Correct 343 ms 32888 KB Output is correct