Submission #128322

# Submission time Handle Problem Language Result Execution time Memory
128322 2019-07-10T16:55:49 Z Mohammad_Yasser Mergers (JOI19_mergers) C++14
100 / 100
1008 ms 58324 KB
#ifndef Local
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/STACK:1024000000,1024000000")
#endif

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define popCnt(x) (__builtin_popcountll(x))
typedef long long Long;

const int N = 5e5 + 5;

int color_cnt[N];
vector<int> adj[N];
int color[N];

struct DSU {
  int parent[N];

  void init() {
    for (int i = 0; i < N; ++i) {
      parent[i] = i;
    }
  }

  int getRoot(int x) {
    if (parent[x] == x) return x;
    return parent[x] = getRoot(parent[x]);
  }

  bool join(int x, int y) {
    x = getRoot(x), y = getRoot(y);
    if (x == y) return false;
    parent[x] = y;
    color_cnt[y] += color_cnt[x];
    return true;
  }

} dsu;

int node_cnt[N];

void solve(int node, int parent) {
  for (auto& child : adj[node]) {
    if (child == parent) continue;
    solve(child, node);
    if (node_cnt[child] == color_cnt[color[child]]) {
      continue;
    }
    dsu.join(color[node], color[child]);
    node_cnt[node] += node_cnt[child];
  }

  ++node_cnt[node];
  color[node] = dsu.getRoot(color[node]);

//  cout << node << " " << color[node] << " " << node_cnt[node] << " "
//    << color_cnt[color[node]] << endl;
}

int n, k;
int deg[N];

int countLeaves() {
  int res = 0;

  for (int i = 1; i <= n; ++i) {
    color[i] = dsu.getRoot(color[i]);
  }

  for (int i = 1; i <= n; ++i) {
    for (int v : adj[i]) {
      if (color[v] != color[i]) {
        ++deg[color[v]];
        ++deg[color[i]];
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    res += (deg[i] == 2);
  }

  return res;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cerr.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif

  cin >> n >> k;

  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  for (int i = 1; i <= n; ++i) {
    cin >> color[i];
    ++color_cnt[color[i]];
  }

  dsu.init();

  solve(1, -1);

  cout << (countLeaves() + 1) / 2 << endl;

}

Compilation message

mergers.cpp:5:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(linker, "/STACK:1024000000,1024000000")
# Verdict Execution time Memory Grader output
1 Correct 16 ms 14072 KB Output is correct
2 Correct 16 ms 14200 KB Output is correct
3 Correct 16 ms 14072 KB Output is correct
4 Correct 16 ms 14072 KB Output is correct
5 Correct 16 ms 14072 KB Output is correct
6 Correct 16 ms 14072 KB Output is correct
7 Correct 16 ms 14072 KB Output is correct
8 Correct 15 ms 14072 KB Output is correct
9 Correct 15 ms 13992 KB Output is correct
10 Correct 15 ms 14072 KB Output is correct
11 Correct 15 ms 14072 KB Output is correct
12 Correct 18 ms 13944 KB Output is correct
13 Correct 16 ms 14072 KB Output is correct
14 Correct 15 ms 14072 KB Output is correct
15 Correct 18 ms 14072 KB Output is correct
16 Correct 16 ms 14072 KB Output is correct
17 Correct 16 ms 14072 KB Output is correct
18 Correct 16 ms 14072 KB Output is correct
19 Correct 16 ms 14072 KB Output is correct
20 Correct 16 ms 14076 KB Output is correct
21 Correct 14 ms 14072 KB Output is correct
22 Correct 16 ms 14072 KB Output is correct
23 Correct 16 ms 14072 KB Output is correct
24 Correct 16 ms 14072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 14072 KB Output is correct
2 Correct 16 ms 14200 KB Output is correct
3 Correct 16 ms 14072 KB Output is correct
4 Correct 16 ms 14072 KB Output is correct
5 Correct 16 ms 14072 KB Output is correct
6 Correct 16 ms 14072 KB Output is correct
7 Correct 16 ms 14072 KB Output is correct
8 Correct 15 ms 14072 KB Output is correct
9 Correct 15 ms 13992 KB Output is correct
10 Correct 15 ms 14072 KB Output is correct
11 Correct 15 ms 14072 KB Output is correct
12 Correct 18 ms 13944 KB Output is correct
13 Correct 16 ms 14072 KB Output is correct
14 Correct 15 ms 14072 KB Output is correct
15 Correct 18 ms 14072 KB Output is correct
16 Correct 16 ms 14072 KB Output is correct
17 Correct 16 ms 14072 KB Output is correct
18 Correct 16 ms 14072 KB Output is correct
19 Correct 16 ms 14072 KB Output is correct
20 Correct 16 ms 14076 KB Output is correct
21 Correct 14 ms 14072 KB Output is correct
22 Correct 16 ms 14072 KB Output is correct
23 Correct 16 ms 14072 KB Output is correct
24 Correct 16 ms 14072 KB Output is correct
25 Correct 16 ms 14072 KB Output is correct
26 Correct 18 ms 14204 KB Output is correct
27 Correct 19 ms 14200 KB Output is correct
28 Correct 20 ms 14328 KB Output is correct
29 Correct 20 ms 14148 KB Output is correct
30 Correct 18 ms 14328 KB Output is correct
31 Correct 16 ms 14072 KB Output is correct
32 Correct 14 ms 14328 KB Output is correct
33 Correct 16 ms 14076 KB Output is correct
34 Correct 17 ms 14196 KB Output is correct
35 Correct 18 ms 14200 KB Output is correct
36 Correct 22 ms 14200 KB Output is correct
37 Correct 17 ms 14200 KB Output is correct
38 Correct 15 ms 14072 KB Output is correct
39 Correct 17 ms 14132 KB Output is correct
40 Correct 17 ms 14112 KB Output is correct
41 Correct 17 ms 14200 KB Output is correct
42 Correct 18 ms 14328 KB Output is correct
43 Correct 18 ms 14328 KB Output is correct
44 Correct 19 ms 14072 KB Output is correct
45 Correct 20 ms 14200 KB Output is correct
46 Correct 17 ms 14200 KB Output is correct
47 Correct 18 ms 14072 KB Output is correct
48 Correct 18 ms 14200 KB Output is correct
49 Correct 17 ms 14200 KB Output is correct
50 Correct 20 ms 14200 KB Output is correct
51 Correct 23 ms 14200 KB Output is correct
52 Correct 18 ms 14200 KB Output is correct
53 Correct 21 ms 14200 KB Output is correct
54 Correct 23 ms 14200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 14072 KB Output is correct
2 Correct 16 ms 14200 KB Output is correct
3 Correct 16 ms 14072 KB Output is correct
4 Correct 16 ms 14072 KB Output is correct
5 Correct 16 ms 14072 KB Output is correct
6 Correct 16 ms 14072 KB Output is correct
7 Correct 16 ms 14072 KB Output is correct
8 Correct 15 ms 14072 KB Output is correct
9 Correct 15 ms 13992 KB Output is correct
10 Correct 15 ms 14072 KB Output is correct
11 Correct 15 ms 14072 KB Output is correct
12 Correct 18 ms 13944 KB Output is correct
13 Correct 16 ms 14072 KB Output is correct
14 Correct 15 ms 14072 KB Output is correct
15 Correct 18 ms 14072 KB Output is correct
16 Correct 16 ms 14072 KB Output is correct
17 Correct 16 ms 14072 KB Output is correct
18 Correct 16 ms 14072 KB Output is correct
19 Correct 16 ms 14072 KB Output is correct
20 Correct 16 ms 14076 KB Output is correct
21 Correct 14 ms 14072 KB Output is correct
22 Correct 16 ms 14072 KB Output is correct
23 Correct 16 ms 14072 KB Output is correct
24 Correct 16 ms 14072 KB Output is correct
25 Correct 19 ms 14200 KB Output is correct
26 Correct 78 ms 18544 KB Output is correct
27 Correct 105 ms 19296 KB Output is correct
28 Correct 17 ms 14200 KB Output is correct
29 Correct 16 ms 14072 KB Output is correct
30 Correct 17 ms 14072 KB Output is correct
31 Correct 121 ms 19444 KB Output is correct
32 Correct 19 ms 14328 KB Output is correct
33 Correct 120 ms 24204 KB Output is correct
34 Correct 106 ms 19576 KB Output is correct
35 Correct 17 ms 14200 KB Output is correct
36 Correct 115 ms 19996 KB Output is correct
37 Correct 17 ms 14200 KB Output is correct
38 Correct 22 ms 14204 KB Output is correct
39 Correct 77 ms 19184 KB Output is correct
40 Correct 17 ms 14328 KB Output is correct
41 Correct 120 ms 19596 KB Output is correct
42 Correct 127 ms 21088 KB Output is correct
43 Correct 16 ms 14072 KB Output is correct
44 Correct 122 ms 24296 KB Output is correct
45 Correct 116 ms 21880 KB Output is correct
46 Correct 20 ms 14328 KB Output is correct
47 Correct 17 ms 14200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 18416 KB Output is correct
2 Correct 92 ms 19772 KB Output is correct
3 Correct 18 ms 14200 KB Output is correct
4 Correct 28 ms 14332 KB Output is correct
5 Correct 19 ms 14072 KB Output is correct
6 Correct 19 ms 14044 KB Output is correct
7 Correct 20 ms 14200 KB Output is correct
8 Correct 132 ms 19960 KB Output is correct
9 Correct 21 ms 14200 KB Output is correct
10 Correct 141 ms 19036 KB Output is correct
11 Correct 19 ms 14072 KB Output is correct
12 Correct 144 ms 19448 KB Output is correct
13 Correct 120 ms 20116 KB Output is correct
14 Correct 125 ms 20192 KB Output is correct
15 Correct 79 ms 19308 KB Output is correct
16 Correct 18 ms 14328 KB Output is correct
17 Correct 18 ms 14072 KB Output is correct
18 Correct 104 ms 20816 KB Output is correct
19 Correct 165 ms 22568 KB Output is correct
20 Correct 18 ms 14200 KB Output is correct
21 Correct 16 ms 14072 KB Output is correct
22 Correct 96 ms 20436 KB Output is correct
23 Correct 19 ms 14176 KB Output is correct
24 Correct 133 ms 18936 KB Output is correct
25 Correct 163 ms 22216 KB Output is correct
26 Correct 22 ms 14200 KB Output is correct
27 Correct 22 ms 14328 KB Output is correct
28 Correct 21 ms 14276 KB Output is correct
29 Correct 18 ms 14268 KB Output is correct
30 Correct 24 ms 14328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 14072 KB Output is correct
2 Correct 16 ms 14200 KB Output is correct
3 Correct 16 ms 14072 KB Output is correct
4 Correct 16 ms 14072 KB Output is correct
5 Correct 16 ms 14072 KB Output is correct
6 Correct 16 ms 14072 KB Output is correct
7 Correct 16 ms 14072 KB Output is correct
8 Correct 15 ms 14072 KB Output is correct
9 Correct 15 ms 13992 KB Output is correct
10 Correct 15 ms 14072 KB Output is correct
11 Correct 15 ms 14072 KB Output is correct
12 Correct 18 ms 13944 KB Output is correct
13 Correct 16 ms 14072 KB Output is correct
14 Correct 15 ms 14072 KB Output is correct
15 Correct 18 ms 14072 KB Output is correct
16 Correct 16 ms 14072 KB Output is correct
17 Correct 16 ms 14072 KB Output is correct
18 Correct 16 ms 14072 KB Output is correct
19 Correct 16 ms 14072 KB Output is correct
20 Correct 16 ms 14076 KB Output is correct
21 Correct 14 ms 14072 KB Output is correct
22 Correct 16 ms 14072 KB Output is correct
23 Correct 16 ms 14072 KB Output is correct
24 Correct 16 ms 14072 KB Output is correct
25 Correct 16 ms 14072 KB Output is correct
26 Correct 18 ms 14204 KB Output is correct
27 Correct 19 ms 14200 KB Output is correct
28 Correct 20 ms 14328 KB Output is correct
29 Correct 20 ms 14148 KB Output is correct
30 Correct 18 ms 14328 KB Output is correct
31 Correct 16 ms 14072 KB Output is correct
32 Correct 14 ms 14328 KB Output is correct
33 Correct 16 ms 14076 KB Output is correct
34 Correct 17 ms 14196 KB Output is correct
35 Correct 18 ms 14200 KB Output is correct
36 Correct 22 ms 14200 KB Output is correct
37 Correct 17 ms 14200 KB Output is correct
38 Correct 15 ms 14072 KB Output is correct
39 Correct 17 ms 14132 KB Output is correct
40 Correct 17 ms 14112 KB Output is correct
41 Correct 17 ms 14200 KB Output is correct
42 Correct 18 ms 14328 KB Output is correct
43 Correct 18 ms 14328 KB Output is correct
44 Correct 19 ms 14072 KB Output is correct
45 Correct 20 ms 14200 KB Output is correct
46 Correct 17 ms 14200 KB Output is correct
47 Correct 18 ms 14072 KB Output is correct
48 Correct 18 ms 14200 KB Output is correct
49 Correct 17 ms 14200 KB Output is correct
50 Correct 20 ms 14200 KB Output is correct
51 Correct 23 ms 14200 KB Output is correct
52 Correct 18 ms 14200 KB Output is correct
53 Correct 21 ms 14200 KB Output is correct
54 Correct 23 ms 14200 KB Output is correct
55 Correct 19 ms 14200 KB Output is correct
56 Correct 78 ms 18544 KB Output is correct
57 Correct 105 ms 19296 KB Output is correct
58 Correct 17 ms 14200 KB Output is correct
59 Correct 16 ms 14072 KB Output is correct
60 Correct 17 ms 14072 KB Output is correct
61 Correct 121 ms 19444 KB Output is correct
62 Correct 19 ms 14328 KB Output is correct
63 Correct 120 ms 24204 KB Output is correct
64 Correct 106 ms 19576 KB Output is correct
65 Correct 17 ms 14200 KB Output is correct
66 Correct 115 ms 19996 KB Output is correct
67 Correct 17 ms 14200 KB Output is correct
68 Correct 22 ms 14204 KB Output is correct
69 Correct 77 ms 19184 KB Output is correct
70 Correct 17 ms 14328 KB Output is correct
71 Correct 120 ms 19596 KB Output is correct
72 Correct 127 ms 21088 KB Output is correct
73 Correct 16 ms 14072 KB Output is correct
74 Correct 122 ms 24296 KB Output is correct
75 Correct 116 ms 21880 KB Output is correct
76 Correct 20 ms 14328 KB Output is correct
77 Correct 17 ms 14200 KB Output is correct
78 Correct 82 ms 18416 KB Output is correct
79 Correct 92 ms 19772 KB Output is correct
80 Correct 18 ms 14200 KB Output is correct
81 Correct 28 ms 14332 KB Output is correct
82 Correct 19 ms 14072 KB Output is correct
83 Correct 19 ms 14044 KB Output is correct
84 Correct 20 ms 14200 KB Output is correct
85 Correct 132 ms 19960 KB Output is correct
86 Correct 21 ms 14200 KB Output is correct
87 Correct 141 ms 19036 KB Output is correct
88 Correct 19 ms 14072 KB Output is correct
89 Correct 144 ms 19448 KB Output is correct
90 Correct 120 ms 20116 KB Output is correct
91 Correct 125 ms 20192 KB Output is correct
92 Correct 79 ms 19308 KB Output is correct
93 Correct 18 ms 14328 KB Output is correct
94 Correct 18 ms 14072 KB Output is correct
95 Correct 104 ms 20816 KB Output is correct
96 Correct 165 ms 22568 KB Output is correct
97 Correct 18 ms 14200 KB Output is correct
98 Correct 16 ms 14072 KB Output is correct
99 Correct 96 ms 20436 KB Output is correct
100 Correct 19 ms 14176 KB Output is correct
101 Correct 133 ms 18936 KB Output is correct
102 Correct 163 ms 22216 KB Output is correct
103 Correct 22 ms 14200 KB Output is correct
104 Correct 22 ms 14328 KB Output is correct
105 Correct 21 ms 14276 KB Output is correct
106 Correct 18 ms 14268 KB Output is correct
107 Correct 24 ms 14328 KB Output is correct
108 Correct 581 ms 42472 KB Output is correct
109 Correct 679 ms 50040 KB Output is correct
110 Correct 884 ms 58008 KB Output is correct
111 Correct 1008 ms 58324 KB Output is correct
112 Correct 818 ms 51916 KB Output is correct
113 Correct 630 ms 44448 KB Output is correct
114 Correct 582 ms 39672 KB Output is correct
115 Correct 587 ms 39416 KB Output is correct
116 Correct 682 ms 39876 KB Output is correct
117 Correct 740 ms 40568 KB Output is correct
118 Correct 557 ms 36284 KB Output is correct
119 Correct 760 ms 40564 KB Output is correct
120 Correct 810 ms 49808 KB Output is correct
121 Correct 760 ms 38636 KB Output is correct
122 Correct 750 ms 37236 KB Output is correct
123 Correct 526 ms 40168 KB Output is correct
124 Correct 819 ms 38232 KB Output is correct