답안 #220083

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
220083 2020-04-06T23:28:32 Z rama_pang Mergers (JOI19_mergers) C++14
24 / 100
100 ms 17132 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);
    }
  };

  depth[1] = 1;
  par[1] = 0;
  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 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 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 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 5 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 5 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 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 4 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 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 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 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 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 5 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 5 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 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 4 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 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 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 6 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 6 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 6 ms 640 KB Output is correct
38 Correct 5 ms 384 KB Output is correct
39 Correct 7 ms 640 KB Output is correct
40 Correct 7 ms 640 KB Output is correct
41 Correct 6 ms 640 KB Output is correct
42 Correct 6 ms 768 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 1408 KB Execution killed with signal 11 (could be triggered by violating memory limits)
47 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 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 5 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 5 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 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 4 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 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 55 ms 9320 KB Output is correct
27 Correct 74 ms 9068 KB Output is correct
28 Correct 6 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 75 ms 9068 KB Output is correct
32 Correct 6 ms 640 KB Output is correct
33 Correct 86 ms 14700 KB Output is correct
34 Correct 84 ms 9068 KB Output is correct
35 Correct 6 ms 640 KB Output is correct
36 Correct 92 ms 9580 KB Output is correct
37 Correct 6 ms 640 KB Output is correct
38 Correct 6 ms 640 KB Output is correct
39 Correct 56 ms 9312 KB Output is correct
40 Correct 6 ms 768 KB Output is correct
41 Correct 74 ms 9072 KB Output is correct
42 Correct 92 ms 10860 KB Output is correct
43 Correct 4 ms 384 KB Output is correct
44 Correct 100 ms 15084 KB Output is correct
45 Correct 80 ms 12012 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 56 ms 9336 KB Output is correct
2 Correct 66 ms 14312 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 85 ms 11244 KB Output is correct
9 Correct 6 ms 640 KB Output is correct
10 Correct 78 ms 9068 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 79 ms 9068 KB Output is correct
13 Correct 97 ms 11244 KB Output is correct
14 Correct 83 ms 14060 KB Output is correct
15 Correct 55 ms 9332 KB Output is correct
16 Correct 6 ms 768 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 76 ms 13804 KB Output is correct
19 Correct 95 ms 17132 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 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 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 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 5 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 5 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 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 4 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 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 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 6 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 6 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 6 ms 640 KB Output is correct
38 Correct 5 ms 384 KB Output is correct
39 Correct 7 ms 640 KB Output is correct
40 Correct 7 ms 640 KB Output is correct
41 Correct 6 ms 640 KB Output is correct
42 Correct 6 ms 768 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 1408 KB Execution killed with signal 11 (could be triggered by violating memory limits)
47 Halted 0 ms 0 KB -