답안 #379710

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379710 2021-03-19T04:17:37 Z VROOM_VARUN Mergers (JOI19_mergers) C++14
100 / 100
2418 ms 150508 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 268 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 0 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 2 ms 368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 268 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 0 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 2 ms 368 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 4 ms 1260 KB Output is correct
27 Correct 4 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 3 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 1044 KB Output is correct
35 Correct 4 ms 1152 KB Output is correct
36 Correct 4 ms 1024 KB Output is correct
37 Correct 5 ms 1132 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 4 ms 1092 KB Output is correct
40 Correct 4 ms 1004 KB Output is correct
41 Correct 4 ms 1004 KB Output is correct
42 Correct 5 ms 1132 KB Output is correct
43 Correct 4 ms 1260 KB Output is correct
44 Correct 2 ms 364 KB Output is correct
45 Correct 7 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 5 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 4 ms 1004 KB Output is correct
52 Correct 5 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 268 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 0 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 2 ms 368 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 106 ms 22364 KB Output is correct
27 Correct 173 ms 22240 KB Output is correct
28 Correct 6 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 173 ms 22124 KB Output is correct
32 Correct 3 ms 1004 KB Output is correct
33 Correct 301 ms 27872 KB Output is correct
34 Correct 177 ms 22240 KB Output is correct
35 Correct 3 ms 1004 KB Output is correct
36 Correct 250 ms 22752 KB Output is correct
37 Correct 3 ms 1024 KB Output is correct
38 Correct 3 ms 1004 KB Output is correct
39 Correct 106 ms 22428 KB Output is correct
40 Correct 5 ms 1132 KB Output is correct
41 Correct 231 ms 22132 KB Output is correct
42 Correct 266 ms 24032 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 299 ms 28000 KB Output is correct
45 Correct 269 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 110 ms 22492 KB Output is correct
2 Correct 121 ms 27612 KB Output is correct
3 Correct 4 ms 1132 KB Output is correct
4 Correct 8 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 186 ms 24544 KB Output is correct
9 Correct 3 ms 1004 KB Output is correct
10 Correct 165 ms 21856 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 201 ms 21856 KB Output is correct
13 Correct 196 ms 24032 KB Output is correct
14 Correct 199 ms 26848 KB Output is correct
15 Correct 102 ms 22240 KB Output is correct
16 Correct 3 ms 1132 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 142 ms 26592 KB Output is correct
19 Correct 280 ms 30048 KB Output is correct
20 Correct 4 ms 1132 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 118 ms 24412 KB Output is correct
23 Correct 4 ms 1004 KB Output is correct
24 Correct 196 ms 22496 KB Output is correct
25 Correct 238 ms 28896 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 1004 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 268 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 0 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 2 ms 368 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 4 ms 1260 KB Output is correct
27 Correct 4 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 3 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 1044 KB Output is correct
35 Correct 4 ms 1152 KB Output is correct
36 Correct 4 ms 1024 KB Output is correct
37 Correct 5 ms 1132 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 4 ms 1092 KB Output is correct
40 Correct 4 ms 1004 KB Output is correct
41 Correct 4 ms 1004 KB Output is correct
42 Correct 5 ms 1132 KB Output is correct
43 Correct 4 ms 1260 KB Output is correct
44 Correct 2 ms 364 KB Output is correct
45 Correct 7 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 5 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 4 ms 1004 KB Output is correct
52 Correct 5 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 106 ms 22364 KB Output is correct
57 Correct 173 ms 22240 KB Output is correct
58 Correct 6 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 173 ms 22124 KB Output is correct
62 Correct 3 ms 1004 KB Output is correct
63 Correct 301 ms 27872 KB Output is correct
64 Correct 177 ms 22240 KB Output is correct
65 Correct 3 ms 1004 KB Output is correct
66 Correct 250 ms 22752 KB Output is correct
67 Correct 3 ms 1024 KB Output is correct
68 Correct 3 ms 1004 KB Output is correct
69 Correct 106 ms 22428 KB Output is correct
70 Correct 5 ms 1132 KB Output is correct
71 Correct 231 ms 22132 KB Output is correct
72 Correct 266 ms 24032 KB Output is correct
73 Correct 1 ms 364 KB Output is correct
74 Correct 299 ms 28000 KB Output is correct
75 Correct 269 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 110 ms 22492 KB Output is correct
79 Correct 121 ms 27612 KB Output is correct
80 Correct 4 ms 1132 KB Output is correct
81 Correct 8 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 186 ms 24544 KB Output is correct
86 Correct 3 ms 1004 KB Output is correct
87 Correct 165 ms 21856 KB Output is correct
88 Correct 1 ms 364 KB Output is correct
89 Correct 201 ms 21856 KB Output is correct
90 Correct 196 ms 24032 KB Output is correct
91 Correct 199 ms 26848 KB Output is correct
92 Correct 102 ms 22240 KB Output is correct
93 Correct 3 ms 1132 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 142 ms 26592 KB Output is correct
96 Correct 280 ms 30048 KB Output is correct
97 Correct 4 ms 1132 KB Output is correct
98 Correct 1 ms 364 KB Output is correct
99 Correct 118 ms 24412 KB Output is correct
100 Correct 4 ms 1004 KB Output is correct
101 Correct 196 ms 22496 KB Output is correct
102 Correct 238 ms 28896 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 1004 KB Output is correct
106 Correct 3 ms 1004 KB Output is correct
107 Correct 3 ms 1132 KB Output is correct
108 Correct 839 ms 117176 KB Output is correct
109 Correct 2418 ms 120916 KB Output is correct
110 Correct 2368 ms 129968 KB Output is correct
111 Correct 2192 ms 150508 KB Output is correct
112 Correct 2137 ms 142676 KB Output is correct
113 Correct 1012 ms 130652 KB Output is correct
114 Correct 1057 ms 106776 KB Output is correct
115 Correct 1194 ms 106716 KB Output is correct
116 Correct 1349 ms 112060 KB Output is correct
117 Correct 1295 ms 132044 KB Output is correct
118 Correct 1287 ms 105460 KB Output is correct
119 Correct 1349 ms 132124 KB Output is correct
120 Correct 1901 ms 143060 KB Output is correct
121 Correct 1274 ms 132136 KB Output is correct
122 Correct 1294 ms 121436 KB Output is correct
123 Correct 828 ms 133448 KB Output is correct
124 Correct 1546 ms 129108 KB Output is correct