Submission #219878

# Submission time Handle Problem Language Result Execution time Memory
219878 2020-04-06T16:04:58 Z rama_pang Mergers (JOI19_mergers) C++14
10 / 100
168 ms 16340 KB
#include <bits/stdc++.h>
using namespace std;

class DisjointSet {
 private:
  int n;
  vector<int> p;

 public:
  DisjointSet(int n) : n(n) {
    p.resize(n);
    iota(begin(p), end(p), 0);
  }

  int Find(int x) {
    return p[x] == x ? x : p[x] = Find(p[x]);
  }

  bool Union(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x == y) return false;
    p[x] = y;
    return true;
  }

  bool IsRoot(int x) {
    return p[x] == x;
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int N, K;
  cin >> N >> K;

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

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

  vector<int> sz(N, 0);
  vector<int> par(N, -1);
  vector<bool> visColor(K, false);
  DisjointSet group(N);

  function<void(int)> Dfs = [&](int n) {
    sz[n] = 1;
    for (auto i : adj[n]) if (i != par[n] && !visColor[color[i]]) {
      par[i] = n;
      Dfs(i);
      sz[n] += sz[i];
    }
  };

  auto Centroid = [&](int r) {
    par[r] = -1;
    Dfs(r);
    int Size = sz[r];

    while (true) {
      pair<int, int> mx = {-1, -1};
      for (auto i : adj[r]) if (i != par[r] && !visColor[color[i]]) {
        mx = max(mx, {sz[i], i});
      }
      if (mx.first * 2 <= Size) {
        return r;
      }
      r = mx.second;
    }
  };

  auto Solve = [&](int r) {
    par[r] = -1;
    Dfs(r);

    queue<int> q;

    visColor[color[r]] = true;
    for (auto i : colorList[color[r]]) {
      q.emplace(i);
      group.Union(i, r);
    }

    while (!q.empty()) {
      int p = par[q.front()];
      q.pop();

      if (p != -1 && !visColor[color[p]]) {
        visColor[color[p]] = true;
        for (auto i : colorList[color[p]]) {
          q.emplace(i);
          group.Union(r, i);
        }
      } else if (p != -1 && visColor[color[p]]) {
        group.Union(r, p); 
      }
    }
  };

  auto CentroidDecomposition = [&]() {
    for (int i = 0; i < N; i++) {
      while (!visColor[color[i]]) {
        Solve(Centroid(i));
      }
    }
  };

  CentroidDecomposition();

  vector<int> degree(N, 0);
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    tie(u, v) = edges[i];
    if (group.Find(u) != group.Find(v)) {
      degree[group.Find(u)]++;
      degree[group.Find(v)]++;
    }
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (group.IsRoot(i) && degree[i] == 1) {
      ans++;
    }
  }

  cout << ((ans + 1) / 2) << "\n";
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 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 5 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 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
22 Correct 4 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 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 5 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 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
22 Correct 4 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 4 ms 384 KB Output is correct
26 Incorrect 8 ms 640 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 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 5 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 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
22 Correct 4 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 4 ms 384 KB Output is correct
26 Correct 58 ms 9448 KB Output is correct
27 Correct 83 ms 9324 KB Output is correct
28 Correct 7 ms 640 KB Output is correct
29 Correct 4 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 138 ms 9196 KB Output is correct
32 Correct 6 ms 640 KB Output is correct
33 Correct 120 ms 16340 KB Output is correct
34 Correct 92 ms 9452 KB Output is correct
35 Correct 7 ms 640 KB Output is correct
36 Correct 168 ms 9968 KB Output is correct
37 Correct 6 ms 640 KB Output is correct
38 Correct 6 ms 640 KB Output is correct
39 Correct 60 ms 9452 KB Output is correct
40 Correct 6 ms 896 KB Output is correct
41 Correct 80 ms 9196 KB Output is correct
42 Correct 120 ms 12400 KB Output is correct
43 Incorrect 5 ms 384 KB Output isn't correct
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 56 ms 9448 KB Output is correct
2 Correct 110 ms 14416 KB Output is correct
3 Incorrect 8 ms 768 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 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 5 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 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
22 Correct 4 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 4 ms 384 KB Output is correct
26 Incorrect 8 ms 640 KB Output isn't correct
27 Halted 0 ms 0 KB -