답안 #379709

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379709 2021-03-19T04:17:27 Z VROOM_VARUN Mergers (JOI19_mergers) C++14
100 / 100
2428 ms 158200 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() {
  // #ifndef ONLINE_JUDGE
  // freopen("mergers.in", "r", stdin);
  // freopen("mergers.out", "w", stdout);
  // #endif
  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) {
      // debug(u, v, x.x, x.y);
      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);

  //   [u, v, x.x, x.y]: 39 0 86 44
  // [u, v, x.x, x.y]: 39 0 90 68
  // if (n >= 100) {
  //   debug(dep[86], dep[44], dep[90], dep[68]);
  //   debug(lcaa.tin[86], lcaa.tout[86]);
  //   debug(lcaa.tin[44], lcaa.tout[44]);
  //   debug(lcaa.tin[90], lcaa.tout[90]);
  //   debug(lcaa.tin[68], lcaa.tout[68]);
  //   debug(lcaa.getLCA(44, 68));
  // }

  // debug(vals);
  // debug(mx);
  // debug(deg);

  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 384 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 384 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 384 KB Output is correct
21 Correct 1 ms 384 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 384 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 384 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 384 KB Output is correct
21 Correct 1 ms 384 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 4 ms 1024 KB Output is correct
28 Correct 4 ms 1260 KB Output is correct
29 Correct 4 ms 1132 KB Output is correct
30 Correct 3 ms 1016 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 3 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 1152 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 3 ms 1132 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 3 ms 1024 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 384 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 384 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 384 KB Output is correct
21 Correct 1 ms 384 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 23388 KB Output is correct
27 Correct 150 ms 23136 KB Output is correct
28 Correct 3 ms 1024 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 151 ms 23008 KB Output is correct
32 Correct 3 ms 1004 KB Output is correct
33 Correct 299 ms 28876 KB Output is correct
34 Correct 160 ms 23136 KB Output is correct
35 Correct 3 ms 1004 KB Output is correct
36 Correct 239 ms 23776 KB Output is correct
37 Correct 3 ms 1004 KB Output is correct
38 Correct 4 ms 1004 KB Output is correct
39 Correct 103 ms 23264 KB Output is correct
40 Correct 3 ms 1132 KB Output is correct
41 Correct 143 ms 22880 KB Output is correct
42 Correct 270 ms 24848 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 300 ms 28896 KB Output is correct
45 Correct 252 ms 26080 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 105 ms 21936 KB Output is correct
2 Correct 125 ms 27028 KB Output is correct
3 Correct 4 ms 1004 KB Output is correct
4 Correct 4 ms 1004 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 392 KB Output is correct
7 Correct 4 ms 1004 KB Output is correct
8 Correct 197 ms 24160 KB Output is correct
9 Correct 4 ms 1044 KB Output is correct
10 Correct 177 ms 23264 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 235 ms 23392 KB Output is correct
13 Correct 207 ms 25568 KB Output is correct
14 Correct 189 ms 28412 KB Output is correct
15 Correct 112 ms 23380 KB Output is correct
16 Correct 3 ms 1132 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 147 ms 28124 KB Output is correct
19 Correct 273 ms 31456 KB Output is correct
20 Correct 4 ms 1132 KB Output is correct
21 Correct 2 ms 364 KB Output is correct
22 Correct 135 ms 25820 KB Output is correct
23 Correct 4 ms 1040 KB Output is correct
24 Correct 213 ms 23900 KB Output is correct
25 Correct 250 ms 30432 KB Output is correct
26 Correct 3 ms 1132 KB Output is correct
27 Correct 5 ms 1172 KB Output is correct
28 Correct 8 ms 1004 KB Output is correct
29 Correct 3 ms 1004 KB Output is correct
30 Correct 5 ms 1132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 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 384 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 384 KB Output is correct
21 Correct 1 ms 384 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 4 ms 1024 KB Output is correct
28 Correct 4 ms 1260 KB Output is correct
29 Correct 4 ms 1132 KB Output is correct
30 Correct 3 ms 1016 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 3 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 1152 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 3 ms 1132 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 3 ms 1024 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 23388 KB Output is correct
57 Correct 150 ms 23136 KB Output is correct
58 Correct 3 ms 1024 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 151 ms 23008 KB Output is correct
62 Correct 3 ms 1004 KB Output is correct
63 Correct 299 ms 28876 KB Output is correct
64 Correct 160 ms 23136 KB Output is correct
65 Correct 3 ms 1004 KB Output is correct
66 Correct 239 ms 23776 KB Output is correct
67 Correct 3 ms 1004 KB Output is correct
68 Correct 4 ms 1004 KB Output is correct
69 Correct 103 ms 23264 KB Output is correct
70 Correct 3 ms 1132 KB Output is correct
71 Correct 143 ms 22880 KB Output is correct
72 Correct 270 ms 24848 KB Output is correct
73 Correct 1 ms 364 KB Output is correct
74 Correct 300 ms 28896 KB Output is correct
75 Correct 252 ms 26080 KB Output is correct
76 Correct 3 ms 1004 KB Output is correct
77 Correct 3 ms 1004 KB Output is correct
78 Correct 105 ms 21936 KB Output is correct
79 Correct 125 ms 27028 KB Output is correct
80 Correct 4 ms 1004 KB Output is correct
81 Correct 4 ms 1004 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 392 KB Output is correct
84 Correct 4 ms 1004 KB Output is correct
85 Correct 197 ms 24160 KB Output is correct
86 Correct 4 ms 1044 KB Output is correct
87 Correct 177 ms 23264 KB Output is correct
88 Correct 1 ms 364 KB Output is correct
89 Correct 235 ms 23392 KB Output is correct
90 Correct 207 ms 25568 KB Output is correct
91 Correct 189 ms 28412 KB Output is correct
92 Correct 112 ms 23380 KB Output is correct
93 Correct 3 ms 1132 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 147 ms 28124 KB Output is correct
96 Correct 273 ms 31456 KB Output is correct
97 Correct 4 ms 1132 KB Output is correct
98 Correct 2 ms 364 KB Output is correct
99 Correct 135 ms 25820 KB Output is correct
100 Correct 4 ms 1040 KB Output is correct
101 Correct 213 ms 23900 KB Output is correct
102 Correct 250 ms 30432 KB Output is correct
103 Correct 3 ms 1132 KB Output is correct
104 Correct 5 ms 1172 KB Output is correct
105 Correct 8 ms 1004 KB Output is correct
106 Correct 3 ms 1004 KB Output is correct
107 Correct 5 ms 1132 KB Output is correct
108 Correct 848 ms 125192 KB Output is correct
109 Correct 2319 ms 125600 KB Output is correct
110 Correct 2428 ms 136116 KB Output is correct
111 Correct 2203 ms 158200 KB Output is correct
112 Correct 2142 ms 151248 KB Output is correct
113 Correct 1075 ms 140380 KB Output is correct
114 Correct 1087 ms 113768 KB Output is correct
115 Correct 1079 ms 111188 KB Output is correct
116 Correct 1318 ms 119000 KB Output is correct
117 Correct 1330 ms 141012 KB Output is correct
118 Correct 1305 ms 107860 KB Output is correct
119 Correct 1313 ms 134572 KB Output is correct
120 Correct 1929 ms 145552 KB Output is correct
121 Correct 1332 ms 134432 KB Output is correct
122 Correct 1352 ms 123532 KB Output is correct
123 Correct 831 ms 135844 KB Output is correct
124 Correct 1645 ms 131648 KB Output is correct