답안 #220081

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
220081 2020-04-06T23:27:08 Z rama_pang Mergers (JOI19_mergers) C++14
24 / 100
100 ms 18680 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[y] = x;
    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 + 1);
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].emplace_back(v);
    adj[v].emplace_back(u);
    edges.emplace_back(u, v);
  }

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


  vector<int> par(N + 1);
  vector<int> depth(N + 1);
  DisjointSet group(N + 1);

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

  auto Unite = [&](int a, int b) {
    a = group.Find(a);
    b = group.Find(b);
    while (a != b) {
      if (depth[a] > depth[b]) swap(a, b);
      group.Union(par[b], b);
      b = group.Find(b);
    }
  };

  Dfs(1);
  for (int k = 0; k < K; k++) {
    sort(begin(colorList[k]), end(colorList[k]), [&](int a, int b) { return depth[a] > depth[b]; });
    for (int i = 1; i < (int) colorList[k].size(); i++) {
      Unite(colorList[k][0], colorList[k][i]);
    }
  }

  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 = 1; i <= N; i++) {
    if (group.IsRoot(i) && degree[i] == 1) {
      ans++;
    }
  }

  cout << ((ans + 1) / 2) << "\n";
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 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 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 5 ms 384 KB Output is correct
12 Correct 4 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 5 ms 384 KB Output is correct
19 Correct 4 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 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 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 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 5 ms 384 KB Output is correct
12 Correct 4 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 5 ms 384 KB Output is correct
19 Correct 4 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 4 ms 384 KB Output is correct
25 Correct 4 ms 384 KB Output is correct
26 Correct 6 ms 768 KB Output is correct
27 Correct 6 ms 640 KB Output is correct
28 Correct 7 ms 896 KB Output is correct
29 Correct 6 ms 768 KB Output is correct
30 Correct 6 ms 640 KB Output is correct
31 Correct 4 ms 384 KB Output is correct
32 Correct 7 ms 896 KB Output is correct
33 Correct 4 ms 384 KB Output is correct
34 Correct 6 ms 640 KB Output is correct
35 Correct 6 ms 768 KB Output is correct
36 Correct 6 ms 640 KB Output is correct
37 Correct 7 ms 640 KB Output is correct
38 Correct 5 ms 384 KB Output is correct
39 Correct 6 ms 768 KB Output is correct
40 Correct 6 ms 640 KB Output is correct
41 Correct 6 ms 640 KB Output is correct
42 Correct 6 ms 640 KB Output is correct
43 Correct 6 ms 768 KB Output is correct
44 Correct 5 ms 384 KB Output is correct
45 Correct 6 ms 768 KB Output is correct
46 Runtime error 9 ms 1280 KB Execution killed with signal 11 (could be triggered by violating memory limits)
47 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 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 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 5 ms 384 KB Output is correct
12 Correct 4 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 5 ms 384 KB Output is correct
19 Correct 4 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 4 ms 384 KB Output is correct
25 Correct 4 ms 384 KB Output is correct
26 Correct 54 ms 10600 KB Output is correct
27 Correct 76 ms 10220 KB Output is correct
28 Correct 6 ms 640 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 4 ms 384 KB Output is correct
31 Correct 87 ms 10540 KB Output is correct
32 Correct 6 ms 640 KB Output is correct
33 Correct 88 ms 15856 KB Output is correct
34 Correct 75 ms 10224 KB Output is correct
35 Correct 6 ms 640 KB Output is correct
36 Correct 81 ms 10732 KB Output is correct
37 Correct 6 ms 640 KB Output is correct
38 Correct 6 ms 640 KB Output is correct
39 Correct 54 ms 10476 KB Output is correct
40 Correct 6 ms 768 KB Output is correct
41 Correct 74 ms 10092 KB Output is correct
42 Correct 84 ms 12012 KB Output is correct
43 Correct 5 ms 384 KB Output is correct
44 Correct 90 ms 16108 KB Output is correct
45 Correct 87 ms 13344 KB Output is correct
46 Correct 6 ms 640 KB Output is correct
47 Correct 6 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 9064 KB Output is correct
2 Correct 66 ms 14056 KB Output is correct
3 Correct 7 ms 768 KB Output is correct
4 Correct 6 ms 640 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 6 ms 640 KB Output is correct
8 Correct 89 ms 12780 KB Output is correct
9 Correct 7 ms 640 KB Output is correct
10 Correct 77 ms 10476 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 83 ms 10476 KB Output is correct
13 Correct 88 ms 12780 KB Output is correct
14 Correct 85 ms 15512 KB Output is correct
15 Correct 55 ms 10476 KB Output is correct
16 Correct 6 ms 768 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 72 ms 15212 KB Output is correct
19 Correct 100 ms 18680 KB Output is correct
20 Runtime error 9 ms 1280 KB Execution killed with signal 11 (could be triggered by violating memory limits)
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 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 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 5 ms 384 KB Output is correct
12 Correct 4 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 5 ms 384 KB Output is correct
19 Correct 4 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 4 ms 384 KB Output is correct
25 Correct 4 ms 384 KB Output is correct
26 Correct 6 ms 768 KB Output is correct
27 Correct 6 ms 640 KB Output is correct
28 Correct 7 ms 896 KB Output is correct
29 Correct 6 ms 768 KB Output is correct
30 Correct 6 ms 640 KB Output is correct
31 Correct 4 ms 384 KB Output is correct
32 Correct 7 ms 896 KB Output is correct
33 Correct 4 ms 384 KB Output is correct
34 Correct 6 ms 640 KB Output is correct
35 Correct 6 ms 768 KB Output is correct
36 Correct 6 ms 640 KB Output is correct
37 Correct 7 ms 640 KB Output is correct
38 Correct 5 ms 384 KB Output is correct
39 Correct 6 ms 768 KB Output is correct
40 Correct 6 ms 640 KB Output is correct
41 Correct 6 ms 640 KB Output is correct
42 Correct 6 ms 640 KB Output is correct
43 Correct 6 ms 768 KB Output is correct
44 Correct 5 ms 384 KB Output is correct
45 Correct 6 ms 768 KB Output is correct
46 Runtime error 9 ms 1280 KB Execution killed with signal 11 (could be triggered by violating memory limits)
47 Halted 0 ms 0 KB -