답안 #379711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379711 2021-03-19T04:18:43 Z VROOM_VARUN Mergers (JOI19_mergers) C++14
100 / 100
2252 ms 151252 KB
/*
ID: varunra2
LANG: C++
TASK: mergers
*/

#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions

int n, k;

VVI adj;
VII edg;
VI color;
VVI nodes;
VI dep;
VI par;
VI mx;
VI deg;

void init() {
  adj.resize(n);
  color.resize(n);
  nodes.resize(k);
  dep.resize(n);
  mx.resize(n);
  deg.assign(n, 0);
}

struct lca {
  const int bits = 20;
  VVI lft;
  VI tin;
  VI tout;
  int tmr = 0;

  void dfs(int u, int v) {
    tin[u] = tmr++;
    lft[u][0] = v;
    for (int i = 1; i < bits; i++) {
      int to = lft[u][i - 1];
      if (to == -1)
        lft[u][i] = -1;
      else
        lft[u][i] = lft[to][i - 1];
    }
    trav(x, adj[u]) {
      if (x == v) continue;
      dfs(x, u);
    }
    tout[u] = tmr++;
  }

  void init() {
    tin.resize(n);
    tout.resize(n);
    lft.resize(n);
    trav(x, lft) x.resize(bits);
    dfs(0, -1);
  }

  bool isAnc(int u, int v) {
    if (u == -1) return true;
    if (v == -1) return false;
    return tin[u] <= tin[v] and tout[u] >= tout[v];
  }

  int getLCA(int u, int v) {
    if (isAnc(u, v)) return u;
    if (isAnc(v, u)) return v;
    for (int i = bits - 1; i >= 0; i--) {
      if (!isAnc(lft[u][i], v)) {
        u = lft[u][i];
      }
    }
    return lft[u][0];
  }

  int getKthAnc(int u, int k, bool deb = false) {
    for (int i = bits - 1; i >= 0; i--) {
      if (k & (1 << i)) {
        if (deb) debug(u, lft[u][i], i);
        u = lft[u][i];
      }
    }
    return u;
  }
} lcaa;

void dfs_dep(int u, int v) {
  if (v == -1) dep[u] = 0;
  trav(x, adj[u]) {
    if (x == v) continue;
    dep[x] = dep[u] + 1;
    dfs_dep(x, u);
  }
}

void dfs_mx(int u, int v) {
  trav(x, adj[u]) {
    if (x == v) continue;
    dfs_mx(x, u);
    mx[u] = min(mx[u], mx[x]);
  }
}

void checking(int u, int v, VI& vals) {
  for (auto& x : adj[u]) {
    if (x == v) continue;
    if (vals[x] != vals[u] and lcaa.isAnc(vals[x], vals[u])) assert(false);
  }
  trav(x, adj[u]) {
    if (x != v) checking(x, u, vals);
  }
}

void dfs_vals(int u, int v, VI& vals) {
  vals[u] = vals[vals[u]];
  for (auto& x : adj[u]) {
    if (x == v) continue;
    dfs_vals(x, u, vals);
  }
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  if (n == 1) {
    cout << "0\n";
    return 0;
  }

  init();

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].PB(v);
    adj[v].PB(u);
    edg.PB(MP(u, v));
  }

  for (int i = 0; i < n; i++) {
    int col;
    cin >> col;
    col--;
    nodes[col].PB(i);
    color[i] = col;
  }

  dfs_dep(0, -1);
  mx = dep;
  lcaa.init();

  for (int i = 0; i < k; i++) {
    int pt = nodes[i][0];
    for (int j = 1; j < sz(nodes[i]); j++) {
      int u = pt;
      int v = nodes[i][j];
      int lca = lcaa.getLCA(u, v);
      pt = lca;
    }
    for (int j = 0; j < sz(nodes[i]); j++) {
      mx[nodes[i][j]] = min(mx[nodes[i][j]], dep[pt]);
    }
  }

  dfs_mx(0, -1);

  VI vals(n);

  for (int i = 0; i < n; i++) {
    vals[i] = lcaa.getKthAnc(i, dep[i] - mx[i]);
  }

  dfs_vals(0, -1, vals);

  trav(x, edg) {
    int u = vals[x.x];
    int v = vals[x.y];
    if (u != v) {
      deg[u]++;
      deg[v]++;
    }
  }

  int ret = 0;

  for (int i = 0; i < n; i++) {
    if (deg[i] == 1) ret++;
  }

  cout << (ret + 1) / 2 << '\n';

  checking(0, -1, vals);

  return 0;
}

Compilation message

mergers.cpp: In member function 'int lca::getKthAnc(int, int, bool)':
mergers.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
mergers.cpp:127:18: note: in expansion of macro 'debug'
  127 |         if (deb) debug(u, lft[u][i], i);
      |                  ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 3 ms 1132 KB Output is correct
27 Correct 3 ms 1004 KB Output is correct
28 Correct 4 ms 1260 KB Output is correct
29 Correct 4 ms 1132 KB Output is correct
30 Correct 4 ms 1004 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 4 ms 1260 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 3 ms 1004 KB Output is correct
35 Correct 4 ms 1132 KB Output is correct
36 Correct 3 ms 1004 KB Output is correct
37 Correct 4 ms 1132 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 4 ms 1132 KB Output is correct
40 Correct 3 ms 1004 KB Output is correct
41 Correct 3 ms 1004 KB Output is correct
42 Correct 3 ms 1132 KB Output is correct
43 Correct 3 ms 1132 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 4 ms 1260 KB Output is correct
46 Correct 4 ms 1132 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 3 ms 1004 KB Output is correct
49 Correct 3 ms 1132 KB Output is correct
50 Correct 4 ms 1260 KB Output is correct
51 Correct 3 ms 1004 KB Output is correct
52 Correct 3 ms 1004 KB Output is correct
53 Correct 3 ms 1132 KB Output is correct
54 Correct 3 ms 1004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 93 ms 22896 KB Output is correct
27 Correct 146 ms 22624 KB Output is correct
28 Correct 4 ms 1004 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 152 ms 22496 KB Output is correct
32 Correct 3 ms 1004 KB Output is correct
33 Correct 269 ms 27876 KB Output is correct
34 Correct 152 ms 22240 KB Output is correct
35 Correct 3 ms 1004 KB Output is correct
36 Correct 219 ms 22752 KB Output is correct
37 Correct 3 ms 1004 KB Output is correct
38 Correct 3 ms 1004 KB Output is correct
39 Correct 101 ms 22496 KB Output is correct
40 Correct 3 ms 1132 KB Output is correct
41 Correct 146 ms 22112 KB Output is correct
42 Correct 237 ms 24032 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 267 ms 28000 KB Output is correct
45 Correct 237 ms 25184 KB Output is correct
46 Correct 3 ms 1004 KB Output is correct
47 Correct 3 ms 1004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 22492 KB Output is correct
2 Correct 111 ms 27484 KB Output is correct
3 Correct 3 ms 1132 KB Output is correct
4 Correct 3 ms 1004 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 3 ms 1004 KB Output is correct
8 Correct 198 ms 24416 KB Output is correct
9 Correct 3 ms 1004 KB Output is correct
10 Correct 169 ms 22112 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 203 ms 22112 KB Output is correct
13 Correct 184 ms 24288 KB Output is correct
14 Correct 171 ms 27104 KB Output is correct
15 Correct 101 ms 22368 KB Output is correct
16 Correct 3 ms 1132 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 135 ms 26848 KB Output is correct
19 Correct 275 ms 30176 KB Output is correct
20 Correct 4 ms 1260 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 120 ms 24796 KB Output is correct
23 Correct 3 ms 1004 KB Output is correct
24 Correct 197 ms 22752 KB Output is correct
25 Correct 251 ms 29064 KB Output is correct
26 Correct 3 ms 1132 KB Output is correct
27 Correct 4 ms 1260 KB Output is correct
28 Correct 3 ms 1020 KB Output is correct
29 Correct 3 ms 1004 KB Output is correct
30 Correct 3 ms 1132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 3 ms 1132 KB Output is correct
27 Correct 3 ms 1004 KB Output is correct
28 Correct 4 ms 1260 KB Output is correct
29 Correct 4 ms 1132 KB Output is correct
30 Correct 4 ms 1004 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 4 ms 1260 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 3 ms 1004 KB Output is correct
35 Correct 4 ms 1132 KB Output is correct
36 Correct 3 ms 1004 KB Output is correct
37 Correct 4 ms 1132 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 4 ms 1132 KB Output is correct
40 Correct 3 ms 1004 KB Output is correct
41 Correct 3 ms 1004 KB Output is correct
42 Correct 3 ms 1132 KB Output is correct
43 Correct 3 ms 1132 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 4 ms 1260 KB Output is correct
46 Correct 4 ms 1132 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 3 ms 1004 KB Output is correct
49 Correct 3 ms 1132 KB Output is correct
50 Correct 4 ms 1260 KB Output is correct
51 Correct 3 ms 1004 KB Output is correct
52 Correct 3 ms 1004 KB Output is correct
53 Correct 3 ms 1132 KB Output is correct
54 Correct 3 ms 1004 KB Output is correct
55 Correct 1 ms 364 KB Output is correct
56 Correct 93 ms 22896 KB Output is correct
57 Correct 146 ms 22624 KB Output is correct
58 Correct 4 ms 1004 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 152 ms 22496 KB Output is correct
62 Correct 3 ms 1004 KB Output is correct
63 Correct 269 ms 27876 KB Output is correct
64 Correct 152 ms 22240 KB Output is correct
65 Correct 3 ms 1004 KB Output is correct
66 Correct 219 ms 22752 KB Output is correct
67 Correct 3 ms 1004 KB Output is correct
68 Correct 3 ms 1004 KB Output is correct
69 Correct 101 ms 22496 KB Output is correct
70 Correct 3 ms 1132 KB Output is correct
71 Correct 146 ms 22112 KB Output is correct
72 Correct 237 ms 24032 KB Output is correct
73 Correct 1 ms 364 KB Output is correct
74 Correct 267 ms 28000 KB Output is correct
75 Correct 237 ms 25184 KB Output is correct
76 Correct 3 ms 1004 KB Output is correct
77 Correct 3 ms 1004 KB Output is correct
78 Correct 94 ms 22492 KB Output is correct
79 Correct 111 ms 27484 KB Output is correct
80 Correct 3 ms 1132 KB Output is correct
81 Correct 3 ms 1004 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 3 ms 1004 KB Output is correct
85 Correct 198 ms 24416 KB Output is correct
86 Correct 3 ms 1004 KB Output is correct
87 Correct 169 ms 22112 KB Output is correct
88 Correct 1 ms 364 KB Output is correct
89 Correct 203 ms 22112 KB Output is correct
90 Correct 184 ms 24288 KB Output is correct
91 Correct 171 ms 27104 KB Output is correct
92 Correct 101 ms 22368 KB Output is correct
93 Correct 3 ms 1132 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 135 ms 26848 KB Output is correct
96 Correct 275 ms 30176 KB Output is correct
97 Correct 4 ms 1260 KB Output is correct
98 Correct 1 ms 364 KB Output is correct
99 Correct 120 ms 24796 KB Output is correct
100 Correct 3 ms 1004 KB Output is correct
101 Correct 197 ms 22752 KB Output is correct
102 Correct 251 ms 29064 KB Output is correct
103 Correct 3 ms 1132 KB Output is correct
104 Correct 4 ms 1260 KB Output is correct
105 Correct 3 ms 1020 KB Output is correct
106 Correct 3 ms 1004 KB Output is correct
107 Correct 3 ms 1132 KB Output is correct
108 Correct 845 ms 117960 KB Output is correct
109 Correct 2241 ms 121556 KB Output is correct
110 Correct 2252 ms 130516 KB Output is correct
111 Correct 2090 ms 151252 KB Output is correct
112 Correct 1981 ms 143124 KB Output is correct
113 Correct 965 ms 131160 KB Output is correct
114 Correct 1036 ms 107224 KB Output is correct
115 Correct 1041 ms 107220 KB Output is correct
116 Correct 1248 ms 112600 KB Output is correct
117 Correct 1260 ms 132808 KB Output is correct
118 Correct 1084 ms 106196 KB Output is correct
119 Correct 1272 ms 132820 KB Output is correct
120 Correct 1856 ms 143748 KB Output is correct
121 Correct 1268 ms 132692 KB Output is correct
122 Correct 1295 ms 121696 KB Output is correct
123 Correct 858 ms 133960 KB Output is correct
124 Correct 1576 ms 129816 KB Output is correct