Submission #128320

# Submission time Handle Problem Language Result Execution time Memory
128320 2019-07-10T16:54:21 Z Mohammad_Yasser Mergers (JOI19_mergers) C++14
34 / 100
3000 ms 19404 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;

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;
    return true;
  }

} dsu;

bool solve(int node, int parent, int clr) {
  bool res = (color[node] == clr);

  for (auto& child : adj[node]) {
    if (child == parent) continue;
    res |= solve(child, node, clr);
  }

  if (res) {
    dsu.join(color[node], clr);
  }

  return res;
}

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];
  }

  dsu.init();

  for (int i = 1; i <= n; ++i) {
    solve(i, -1, color[i]);
  }

  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 14076 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 14076 KB Output is correct
6 Correct 18 ms 14072 KB Output is correct
7 Correct 16 ms 14072 KB Output is correct
8 Correct 16 ms 14072 KB Output is correct
9 Correct 16 ms 14072 KB Output is correct
10 Correct 16 ms 14072 KB Output is correct
11 Correct 19 ms 14072 KB Output is correct
12 Correct 16 ms 14076 KB Output is correct
13 Correct 16 ms 14072 KB Output is correct
14 Correct 16 ms 14072 KB Output is correct
15 Correct 16 ms 14072 KB Output is correct
16 Correct 16 ms 14060 KB Output is correct
17 Correct 16 ms 13992 KB Output is correct
18 Correct 16 ms 14032 KB Output is correct
19 Correct 16 ms 14076 KB Output is correct
20 Correct 16 ms 14072 KB Output is correct
21 Correct 16 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 14076 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 14076 KB Output is correct
6 Correct 18 ms 14072 KB Output is correct
7 Correct 16 ms 14072 KB Output is correct
8 Correct 16 ms 14072 KB Output is correct
9 Correct 16 ms 14072 KB Output is correct
10 Correct 16 ms 14072 KB Output is correct
11 Correct 19 ms 14072 KB Output is correct
12 Correct 16 ms 14076 KB Output is correct
13 Correct 16 ms 14072 KB Output is correct
14 Correct 16 ms 14072 KB Output is correct
15 Correct 16 ms 14072 KB Output is correct
16 Correct 16 ms 14060 KB Output is correct
17 Correct 16 ms 13992 KB Output is correct
18 Correct 16 ms 14032 KB Output is correct
19 Correct 16 ms 14076 KB Output is correct
20 Correct 16 ms 14072 KB Output is correct
21 Correct 16 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 237 ms 14244 KB Output is correct
27 Correct 264 ms 14300 KB Output is correct
28 Correct 210 ms 14460 KB Output is correct
29 Correct 224 ms 14148 KB Output is correct
30 Correct 233 ms 14200 KB Output is correct
31 Correct 16 ms 14068 KB Output is correct
32 Correct 256 ms 14456 KB Output is correct
33 Correct 16 ms 14072 KB Output is correct
34 Correct 73 ms 14200 KB Output is correct
35 Correct 226 ms 14244 KB Output is correct
36 Correct 235 ms 14244 KB Output is correct
37 Correct 232 ms 14236 KB Output is correct
38 Correct 16 ms 14044 KB Output is correct
39 Correct 238 ms 14200 KB Output is correct
40 Correct 242 ms 14200 KB Output is correct
41 Correct 253 ms 14236 KB Output is correct
42 Correct 73 ms 14200 KB Output is correct
43 Correct 264 ms 14456 KB Output is correct
44 Correct 18 ms 14200 KB Output is correct
45 Correct 229 ms 14328 KB Output is correct
46 Correct 236 ms 14244 KB Output is correct
47 Correct 16 ms 14072 KB Output is correct
48 Correct 244 ms 14304 KB Output is correct
49 Correct 79 ms 14200 KB Output is correct
50 Correct 219 ms 14296 KB Output is correct
51 Correct 242 ms 14200 KB Output is correct
52 Correct 82 ms 14328 KB Output is correct
53 Correct 73 ms 14300 KB Output is correct
54 Correct 241 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 14076 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 14076 KB Output is correct
6 Correct 18 ms 14072 KB Output is correct
7 Correct 16 ms 14072 KB Output is correct
8 Correct 16 ms 14072 KB Output is correct
9 Correct 16 ms 14072 KB Output is correct
10 Correct 16 ms 14072 KB Output is correct
11 Correct 19 ms 14072 KB Output is correct
12 Correct 16 ms 14076 KB Output is correct
13 Correct 16 ms 14072 KB Output is correct
14 Correct 16 ms 14072 KB Output is correct
15 Correct 16 ms 14072 KB Output is correct
16 Correct 16 ms 14060 KB Output is correct
17 Correct 16 ms 13992 KB Output is correct
18 Correct 16 ms 14032 KB Output is correct
19 Correct 16 ms 14076 KB Output is correct
20 Correct 16 ms 14072 KB Output is correct
21 Correct 16 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 17 ms 14072 KB Output is correct
26 Execution timed out 3097 ms 19404 KB Time limit exceeded
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3066 ms 17940 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 14072 KB Output is correct
2 Correct 16 ms 14076 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 14076 KB Output is correct
6 Correct 18 ms 14072 KB Output is correct
7 Correct 16 ms 14072 KB Output is correct
8 Correct 16 ms 14072 KB Output is correct
9 Correct 16 ms 14072 KB Output is correct
10 Correct 16 ms 14072 KB Output is correct
11 Correct 19 ms 14072 KB Output is correct
12 Correct 16 ms 14076 KB Output is correct
13 Correct 16 ms 14072 KB Output is correct
14 Correct 16 ms 14072 KB Output is correct
15 Correct 16 ms 14072 KB Output is correct
16 Correct 16 ms 14060 KB Output is correct
17 Correct 16 ms 13992 KB Output is correct
18 Correct 16 ms 14032 KB Output is correct
19 Correct 16 ms 14076 KB Output is correct
20 Correct 16 ms 14072 KB Output is correct
21 Correct 16 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 237 ms 14244 KB Output is correct
27 Correct 264 ms 14300 KB Output is correct
28 Correct 210 ms 14460 KB Output is correct
29 Correct 224 ms 14148 KB Output is correct
30 Correct 233 ms 14200 KB Output is correct
31 Correct 16 ms 14068 KB Output is correct
32 Correct 256 ms 14456 KB Output is correct
33 Correct 16 ms 14072 KB Output is correct
34 Correct 73 ms 14200 KB Output is correct
35 Correct 226 ms 14244 KB Output is correct
36 Correct 235 ms 14244 KB Output is correct
37 Correct 232 ms 14236 KB Output is correct
38 Correct 16 ms 14044 KB Output is correct
39 Correct 238 ms 14200 KB Output is correct
40 Correct 242 ms 14200 KB Output is correct
41 Correct 253 ms 14236 KB Output is correct
42 Correct 73 ms 14200 KB Output is correct
43 Correct 264 ms 14456 KB Output is correct
44 Correct 18 ms 14200 KB Output is correct
45 Correct 229 ms 14328 KB Output is correct
46 Correct 236 ms 14244 KB Output is correct
47 Correct 16 ms 14072 KB Output is correct
48 Correct 244 ms 14304 KB Output is correct
49 Correct 79 ms 14200 KB Output is correct
50 Correct 219 ms 14296 KB Output is correct
51 Correct 242 ms 14200 KB Output is correct
52 Correct 82 ms 14328 KB Output is correct
53 Correct 73 ms 14300 KB Output is correct
54 Correct 241 ms 14200 KB Output is correct
55 Correct 17 ms 14072 KB Output is correct
56 Execution timed out 3097 ms 19404 KB Time limit exceeded
57 Halted 0 ms 0 KB -