Submission #219876

# Submission time Handle Problem Language Result Execution time Memory
219876 2020-04-06T15:54:09 Z rama_pang Mergers (JOI19_mergers) C++14
10 / 100
181 ms 18668 KB
#include <bits/stdc++.h>
using namespace std;

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);

  vector<int> group(N, -1);
  
  function<void(int)> Dfs = [&](int n) {
    sz[n] = 1;
    for (auto i : adj[n]) if (i != par[n] && group[i] == -1) {
      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] && group[i] == -1) {
        mx = max(mx, {sz[i], i});
      }
      if (mx.first * 2 <= Size) {
        return r;
      }
      r = mx.second;
    }
  };

  auto Solve = [&](int r) {
    int name = r;
    vector<int> comp;

    par[r] = -1;
    Dfs(r);

    queue<int> q;

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

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

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

    for (auto i : comp) {
      group[i] = name;
    }
  };

  auto CentroidDecomposition = [&]() {
    for (int i = 0; i < N; i++) {
      while (group[i] == -1) {
        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[u] != group[v]) {
      degree[group[u]]++;
      degree[group[v]]++;
    }
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (group[i] == 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 256 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 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 5 ms 384 KB Output is correct
11 Correct 6 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 512 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 256 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 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 5 ms 384 KB Output is correct
11 Correct 6 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 512 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Incorrect 9 ms 768 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 256 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 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 5 ms 384 KB Output is correct
11 Correct 6 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 512 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 4 ms 384 KB Output is correct
26 Correct 61 ms 10984 KB Output is correct
27 Correct 101 ms 11708 KB Output is correct
28 Correct 7 ms 640 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 136 ms 10604 KB Output is correct
32 Correct 6 ms 640 KB Output is correct
33 Correct 107 ms 18668 KB Output is correct
34 Correct 97 ms 10992 KB Output is correct
35 Correct 7 ms 640 KB Output is correct
36 Correct 181 ms 11376 KB Output is correct
37 Correct 6 ms 640 KB Output is correct
38 Correct 6 ms 640 KB Output is correct
39 Correct 62 ms 10860 KB Output is correct
40 Correct 7 ms 896 KB Output is correct
41 Correct 93 ms 11372 KB Output is correct
42 Correct 131 ms 13932 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 69 ms 9452 KB Output is correct
2 Correct 104 ms 16232 KB Output is correct
3 Incorrect 9 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 256 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 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 5 ms 384 KB Output is correct
11 Correct 6 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 512 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Incorrect 9 ms 768 KB Output isn't correct
27 Halted 0 ms 0 KB -