Submission #219880

# Submission time Handle Problem Language Result Execution time Memory
219880 2020-04-06T16:06:20 Z rama_pang Mergers (JOI19_mergers) C++14
10 / 100
3000 ms 14056 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(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;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:71:8: warning: variable 'Centroid' set but not used [-Wunused-but-set-variable]
   auto Centroid = [&](int r) {
        ^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 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 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 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 4 ms 384 KB Output is correct
24 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 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 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 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 4 ms 384 KB Output is correct
24 Correct 4 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Incorrect 14 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 4 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 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 4 ms 384 KB Output is correct
24 Correct 4 ms 384 KB Output is correct
25 Correct 4 ms 384 KB Output is correct
26 Correct 57 ms 9576 KB Output is correct
27 Correct 73 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 Incorrect 5 ms 288 KB Output isn't correct
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 67 ms 9576 KB Output is correct
2 Execution timed out 3081 ms 14056 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 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 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 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 4 ms 384 KB Output is correct
24 Correct 4 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Incorrect 14 ms 640 KB Output isn't correct
27 Halted 0 ms 0 KB -