Submission #366303

# Submission time Handle Problem Language Result Execution time Memory
366303 2021-02-13T20:31:26 Z watemus Mergers (JOI19_mergers) C++17
100 / 100
1208 ms 127408 KB
//
// Created by watemus on 13.02.2021.
//

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define FF first
#define SS second

using ll = long long;
using ld = long double;

#define int ll

template<typename T>
using vec = std::vector<T>;

template<typename T>
using uset = std::unordered_set<T>;

template<typename T1, typename T2>
using umap = std::unordered_map<T1, T2>;

constexpr ll INFL = 1000000000000000069;
constexpr int INFI = 1000000069;
const ld PI = acos(-1);

#ifdef LOCAL
std::mt19937 rnd(69);
#else
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
#endif

vec<pair<int, int>> DD = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

#ifdef LOCAL
#else
#endif

const int N = 5e5 + 10;

vec<pair<int, int>> g[N];
int tin[N], tup[N];
int usd[N + N];
int br[N + N];

int dfs_timer = 0;

void dfs(int u, int p) {
  usd[u] = 1;
  tin[u] = tup[u] = dfs_timer++;
  for (auto [v, id] : g[u]) {
    if (id == p) continue;
    if (usd[v]) {
      tup[u] = min(tup[u], tin[v]);
    } else {
      dfs(v, id);
      if (tin[u] < tup[v]) {
        br[id] = 1;
      }
      tup[u] = min(tup[u], tup[v]);
    }
  }
}

int clr[N];

void color(int u, int c) {
  usd[u] = 1;
  clr[u] = c;
  for (auto [v, id] : g[u]) {
    if (!br[id] && !usd[v]) {
      color(v, c);
    }
  }
}

void run() {
  int n, k;
  cin >> n >> k;
  vec<vec<int>> kk(k);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].emplace_back(v, i);
    g[v].emplace_back(u, i);
  }
  int m = n - 1;
  for (int i = 0; i < n; i++) {
    int col;
    cin >> col;
    col--;
    kk[col].push_back(i);
  }
  for (int i = 0; i < k; i++) {
    for (int j = 1; j < kk[i].size(); j++) {
      g[kk[i][j]].emplace_back(kk[i][j - 1], m);
      g[kk[i][j - 1]].emplace_back(kk[i][j], m);
      m++;
    }
  }
  dfs(0, -1);
  memset(usd, 0, sizeof usd);
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (!usd[i]) {
      color(i, c);
      c++;
    }
  }
  vec<int> deg(c);
  for (int i = 0; i < n; i++) {
    for (auto [v, id] : g[i]) {
      if (br[id]) {
        deg[clr[i]]++;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < c; i++) {
    if (deg[i] == 1) {
      ans++;
    }
  }
  cout << max((ans + 1) / 2, 0LL) << '\n';
}

signed main() {
#ifdef LOCAL
  std::freopen("input.txt", "r", stdin);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif
  int t = 1;
  // cin >> t;
  while (t--) {
    run();
  }
  return 0;
}


Compilation message

mergers.cpp: In function 'void run()':
mergers.cpp:105:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |     for (int j = 1; j < kk[i].size(); j++) {
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19948 KB Output is correct
2 Correct 12 ms 19948 KB Output is correct
3 Correct 13 ms 19948 KB Output is correct
4 Correct 12 ms 19948 KB Output is correct
5 Correct 12 ms 19948 KB Output is correct
6 Correct 12 ms 19948 KB Output is correct
7 Correct 12 ms 19948 KB Output is correct
8 Correct 12 ms 19948 KB Output is correct
9 Correct 12 ms 19948 KB Output is correct
10 Correct 11 ms 19948 KB Output is correct
11 Correct 12 ms 19948 KB Output is correct
12 Correct 12 ms 19948 KB Output is correct
13 Correct 12 ms 19948 KB Output is correct
14 Correct 12 ms 19948 KB Output is correct
15 Correct 14 ms 20076 KB Output is correct
16 Correct 12 ms 19948 KB Output is correct
17 Correct 13 ms 19948 KB Output is correct
18 Correct 12 ms 19968 KB Output is correct
19 Correct 13 ms 19948 KB Output is correct
20 Correct 13 ms 19948 KB Output is correct
21 Correct 12 ms 19948 KB Output is correct
22 Correct 12 ms 19948 KB Output is correct
23 Correct 12 ms 19948 KB Output is correct
24 Correct 12 ms 20076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19948 KB Output is correct
2 Correct 12 ms 19948 KB Output is correct
3 Correct 13 ms 19948 KB Output is correct
4 Correct 12 ms 19948 KB Output is correct
5 Correct 12 ms 19948 KB Output is correct
6 Correct 12 ms 19948 KB Output is correct
7 Correct 12 ms 19948 KB Output is correct
8 Correct 12 ms 19948 KB Output is correct
9 Correct 12 ms 19948 KB Output is correct
10 Correct 11 ms 19948 KB Output is correct
11 Correct 12 ms 19948 KB Output is correct
12 Correct 12 ms 19948 KB Output is correct
13 Correct 12 ms 19948 KB Output is correct
14 Correct 12 ms 19948 KB Output is correct
15 Correct 14 ms 20076 KB Output is correct
16 Correct 12 ms 19948 KB Output is correct
17 Correct 13 ms 19948 KB Output is correct
18 Correct 12 ms 19968 KB Output is correct
19 Correct 13 ms 19948 KB Output is correct
20 Correct 13 ms 19948 KB Output is correct
21 Correct 12 ms 19948 KB Output is correct
22 Correct 12 ms 19948 KB Output is correct
23 Correct 12 ms 19948 KB Output is correct
24 Correct 12 ms 20076 KB Output is correct
25 Correct 12 ms 19948 KB Output is correct
26 Correct 14 ms 20460 KB Output is correct
27 Correct 17 ms 20460 KB Output is correct
28 Correct 15 ms 20588 KB Output is correct
29 Correct 14 ms 20460 KB Output is correct
30 Correct 15 ms 20588 KB Output is correct
31 Correct 12 ms 19948 KB Output is correct
32 Correct 14 ms 20588 KB Output is correct
33 Correct 12 ms 19948 KB Output is correct
34 Correct 14 ms 20332 KB Output is correct
35 Correct 14 ms 20460 KB Output is correct
36 Correct 15 ms 20460 KB Output is correct
37 Correct 17 ms 20460 KB Output is correct
38 Correct 12 ms 19948 KB Output is correct
39 Correct 14 ms 20588 KB Output is correct
40 Correct 16 ms 20588 KB Output is correct
41 Correct 14 ms 20480 KB Output is correct
42 Correct 14 ms 20460 KB Output is correct
43 Correct 15 ms 20588 KB Output is correct
44 Correct 12 ms 19948 KB Output is correct
45 Correct 14 ms 20460 KB Output is correct
46 Correct 14 ms 20460 KB Output is correct
47 Correct 12 ms 19948 KB Output is correct
48 Correct 15 ms 20460 KB Output is correct
49 Correct 14 ms 20460 KB Output is correct
50 Correct 14 ms 20460 KB Output is correct
51 Correct 14 ms 20408 KB Output is correct
52 Correct 14 ms 20460 KB Output is correct
53 Correct 15 ms 20460 KB Output is correct
54 Correct 18 ms 20588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19948 KB Output is correct
2 Correct 12 ms 19948 KB Output is correct
3 Correct 13 ms 19948 KB Output is correct
4 Correct 12 ms 19948 KB Output is correct
5 Correct 12 ms 19948 KB Output is correct
6 Correct 12 ms 19948 KB Output is correct
7 Correct 12 ms 19948 KB Output is correct
8 Correct 12 ms 19948 KB Output is correct
9 Correct 12 ms 19948 KB Output is correct
10 Correct 11 ms 19948 KB Output is correct
11 Correct 12 ms 19948 KB Output is correct
12 Correct 12 ms 19948 KB Output is correct
13 Correct 12 ms 19948 KB Output is correct
14 Correct 12 ms 19948 KB Output is correct
15 Correct 14 ms 20076 KB Output is correct
16 Correct 12 ms 19948 KB Output is correct
17 Correct 13 ms 19948 KB Output is correct
18 Correct 12 ms 19968 KB Output is correct
19 Correct 13 ms 19948 KB Output is correct
20 Correct 13 ms 19948 KB Output is correct
21 Correct 12 ms 19948 KB Output is correct
22 Correct 12 ms 19948 KB Output is correct
23 Correct 12 ms 19948 KB Output is correct
24 Correct 12 ms 20076 KB Output is correct
25 Correct 12 ms 19980 KB Output is correct
26 Correct 101 ms 35040 KB Output is correct
27 Correct 157 ms 40324 KB Output is correct
28 Correct 15 ms 20460 KB Output is correct
29 Correct 12 ms 19948 KB Output is correct
30 Correct 12 ms 19948 KB Output is correct
31 Correct 152 ms 35948 KB Output is correct
32 Correct 14 ms 20332 KB Output is correct
33 Correct 172 ms 41452 KB Output is correct
34 Correct 152 ms 39404 KB Output is correct
35 Correct 14 ms 20460 KB Output is correct
36 Correct 151 ms 34924 KB Output is correct
37 Correct 14 ms 20588 KB Output is correct
38 Correct 14 ms 20460 KB Output is correct
39 Correct 109 ms 34772 KB Output is correct
40 Correct 16 ms 20588 KB Output is correct
41 Correct 133 ms 40164 KB Output is correct
42 Correct 177 ms 41324 KB Output is correct
43 Correct 14 ms 19948 KB Output is correct
44 Correct 179 ms 41304 KB Output is correct
45 Correct 150 ms 41836 KB Output is correct
46 Correct 13 ms 20460 KB Output is correct
47 Correct 14 ms 20588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 34908 KB Output is correct
2 Correct 79 ms 35804 KB Output is correct
3 Correct 14 ms 20460 KB Output is correct
4 Correct 15 ms 20460 KB Output is correct
5 Correct 12 ms 19948 KB Output is correct
6 Correct 12 ms 19948 KB Output is correct
7 Correct 14 ms 20332 KB Output is correct
8 Correct 153 ms 35820 KB Output is correct
9 Correct 16 ms 20460 KB Output is correct
10 Correct 136 ms 36120 KB Output is correct
11 Correct 12 ms 19948 KB Output is correct
12 Correct 147 ms 34796 KB Output is correct
13 Correct 160 ms 35948 KB Output is correct
14 Correct 110 ms 36204 KB Output is correct
15 Correct 114 ms 34772 KB Output is correct
16 Correct 14 ms 20460 KB Output is correct
17 Correct 13 ms 19948 KB Output is correct
18 Correct 125 ms 35924 KB Output is correct
19 Correct 134 ms 39976 KB Output is correct
20 Correct 14 ms 20460 KB Output is correct
21 Correct 12 ms 19948 KB Output is correct
22 Correct 118 ms 35292 KB Output is correct
23 Correct 14 ms 20460 KB Output is correct
24 Correct 151 ms 36076 KB Output is correct
25 Correct 133 ms 39404 KB Output is correct
26 Correct 15 ms 20460 KB Output is correct
27 Correct 15 ms 20460 KB Output is correct
28 Correct 15 ms 20480 KB Output is correct
29 Correct 15 ms 20460 KB Output is correct
30 Correct 15 ms 20460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19948 KB Output is correct
2 Correct 12 ms 19948 KB Output is correct
3 Correct 13 ms 19948 KB Output is correct
4 Correct 12 ms 19948 KB Output is correct
5 Correct 12 ms 19948 KB Output is correct
6 Correct 12 ms 19948 KB Output is correct
7 Correct 12 ms 19948 KB Output is correct
8 Correct 12 ms 19948 KB Output is correct
9 Correct 12 ms 19948 KB Output is correct
10 Correct 11 ms 19948 KB Output is correct
11 Correct 12 ms 19948 KB Output is correct
12 Correct 12 ms 19948 KB Output is correct
13 Correct 12 ms 19948 KB Output is correct
14 Correct 12 ms 19948 KB Output is correct
15 Correct 14 ms 20076 KB Output is correct
16 Correct 12 ms 19948 KB Output is correct
17 Correct 13 ms 19948 KB Output is correct
18 Correct 12 ms 19968 KB Output is correct
19 Correct 13 ms 19948 KB Output is correct
20 Correct 13 ms 19948 KB Output is correct
21 Correct 12 ms 19948 KB Output is correct
22 Correct 12 ms 19948 KB Output is correct
23 Correct 12 ms 19948 KB Output is correct
24 Correct 12 ms 20076 KB Output is correct
25 Correct 12 ms 19948 KB Output is correct
26 Correct 14 ms 20460 KB Output is correct
27 Correct 17 ms 20460 KB Output is correct
28 Correct 15 ms 20588 KB Output is correct
29 Correct 14 ms 20460 KB Output is correct
30 Correct 15 ms 20588 KB Output is correct
31 Correct 12 ms 19948 KB Output is correct
32 Correct 14 ms 20588 KB Output is correct
33 Correct 12 ms 19948 KB Output is correct
34 Correct 14 ms 20332 KB Output is correct
35 Correct 14 ms 20460 KB Output is correct
36 Correct 15 ms 20460 KB Output is correct
37 Correct 17 ms 20460 KB Output is correct
38 Correct 12 ms 19948 KB Output is correct
39 Correct 14 ms 20588 KB Output is correct
40 Correct 16 ms 20588 KB Output is correct
41 Correct 14 ms 20480 KB Output is correct
42 Correct 14 ms 20460 KB Output is correct
43 Correct 15 ms 20588 KB Output is correct
44 Correct 12 ms 19948 KB Output is correct
45 Correct 14 ms 20460 KB Output is correct
46 Correct 14 ms 20460 KB Output is correct
47 Correct 12 ms 19948 KB Output is correct
48 Correct 15 ms 20460 KB Output is correct
49 Correct 14 ms 20460 KB Output is correct
50 Correct 14 ms 20460 KB Output is correct
51 Correct 14 ms 20408 KB Output is correct
52 Correct 14 ms 20460 KB Output is correct
53 Correct 15 ms 20460 KB Output is correct
54 Correct 18 ms 20588 KB Output is correct
55 Correct 12 ms 19980 KB Output is correct
56 Correct 101 ms 35040 KB Output is correct
57 Correct 157 ms 40324 KB Output is correct
58 Correct 15 ms 20460 KB Output is correct
59 Correct 12 ms 19948 KB Output is correct
60 Correct 12 ms 19948 KB Output is correct
61 Correct 152 ms 35948 KB Output is correct
62 Correct 14 ms 20332 KB Output is correct
63 Correct 172 ms 41452 KB Output is correct
64 Correct 152 ms 39404 KB Output is correct
65 Correct 14 ms 20460 KB Output is correct
66 Correct 151 ms 34924 KB Output is correct
67 Correct 14 ms 20588 KB Output is correct
68 Correct 14 ms 20460 KB Output is correct
69 Correct 109 ms 34772 KB Output is correct
70 Correct 16 ms 20588 KB Output is correct
71 Correct 133 ms 40164 KB Output is correct
72 Correct 177 ms 41324 KB Output is correct
73 Correct 14 ms 19948 KB Output is correct
74 Correct 179 ms 41304 KB Output is correct
75 Correct 150 ms 41836 KB Output is correct
76 Correct 13 ms 20460 KB Output is correct
77 Correct 14 ms 20588 KB Output is correct
78 Correct 98 ms 34908 KB Output is correct
79 Correct 79 ms 35804 KB Output is correct
80 Correct 14 ms 20460 KB Output is correct
81 Correct 15 ms 20460 KB Output is correct
82 Correct 12 ms 19948 KB Output is correct
83 Correct 12 ms 19948 KB Output is correct
84 Correct 14 ms 20332 KB Output is correct
85 Correct 153 ms 35820 KB Output is correct
86 Correct 16 ms 20460 KB Output is correct
87 Correct 136 ms 36120 KB Output is correct
88 Correct 12 ms 19948 KB Output is correct
89 Correct 147 ms 34796 KB Output is correct
90 Correct 160 ms 35948 KB Output is correct
91 Correct 110 ms 36204 KB Output is correct
92 Correct 114 ms 34772 KB Output is correct
93 Correct 14 ms 20460 KB Output is correct
94 Correct 13 ms 19948 KB Output is correct
95 Correct 125 ms 35924 KB Output is correct
96 Correct 134 ms 39976 KB Output is correct
97 Correct 14 ms 20460 KB Output is correct
98 Correct 12 ms 19948 KB Output is correct
99 Correct 118 ms 35292 KB Output is correct
100 Correct 14 ms 20460 KB Output is correct
101 Correct 151 ms 36076 KB Output is correct
102 Correct 133 ms 39404 KB Output is correct
103 Correct 15 ms 20460 KB Output is correct
104 Correct 15 ms 20460 KB Output is correct
105 Correct 15 ms 20480 KB Output is correct
106 Correct 15 ms 20460 KB Output is correct
107 Correct 15 ms 20460 KB Output is correct
108 Correct 909 ms 99080 KB Output is correct
109 Correct 993 ms 126968 KB Output is correct
110 Correct 1000 ms 127408 KB Output is correct
111 Correct 863 ms 124864 KB Output is correct
112 Correct 880 ms 114984 KB Output is correct
113 Correct 665 ms 100928 KB Output is correct
114 Correct 953 ms 116816 KB Output is correct
115 Correct 922 ms 121744 KB Output is correct
116 Correct 1208 ms 102156 KB Output is correct
117 Correct 804 ms 102500 KB Output is correct
118 Correct 1057 ms 98924 KB Output is correct
119 Correct 757 ms 102636 KB Output is correct
120 Correct 804 ms 119916 KB Output is correct
121 Correct 751 ms 102508 KB Output is correct
122 Correct 1182 ms 117592 KB Output is correct
123 Correct 548 ms 100424 KB Output is correct
124 Correct 869 ms 99180 KB Output is correct