답안 #532284

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
532284 2022-03-02T16:45:07 Z Alex_tz307 Cats or Dogs (JOI18_catdog) C++17
100 / 100
511 ms 22008 KB
#include <bits/stdc++.h>
#include "catdog.h"
#define INF 0x3f3f3f3f
 
using namespace std;
 
const int kN = 1e5;
vector<int> g[1 + kN];
int labels, sz[1 + kN], p[1 + kN], heavySon[1 + kN], chainTop[1 + kN], label[1 + kN], down[1 + kN], last[1 + kN][2], sum[1 + kN][2];
short col[1 + kN];
 
void minSelf(int &x, int y) {
  if (y < x) {
    x = y;
  }
}
 
struct node {
  int dp[2][2];
 
  node() {
    for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 2; ++j) {
        dp[i][j] = INF;
      }
    }
  }
 
  void init(int pos, int c) {
    dp[0][1] = dp[1][0] = INF;
    for (int i = 0; i < 2; ++i) {
      if (i != c && c != 2) {
        dp[i][i] = INF;
      } else {
        dp[i][i] = sum[pos][i];
      }
    }
  }
 
  node operator + (const node &rhs) const {
    node ret;
    for (int a = 0; a < 2; ++a) {
      for (int b = 0; b < 2; ++b) {
        for (int c = 0; c < 2; ++c) {
          for (int d = 0; d < 2; ++d) {
            minSelf(ret.dp[a][d], dp[a][b] + rhs.dp[c][d] + (b != c));
          }
        }
      }
    }
    return ret;
  }
};
 
struct ST {
  int n;
  vector<node> t;
 
  void init(int N) {
    n = N;
    int dim = 1;
    while (dim < n) {
      dim *= 2;
    }
    t.resize(dim * 2);
  }
 
  void build(int x, int lx, int rx) {
    if (lx == rx) {
      t[x].init(lx, 2);
      return;
    }
    int mid = (lx + rx) / 2;
    build(x * 2, lx, mid);
    build(x * 2 + 1, mid + 1, rx);
    t[x] = t[x * 2] + t[x * 2 + 1];
  }
 
  void update(int x, int lx, int rx, int pos, int c) {
    if (lx == rx) {
      t[x].init(pos, c);
      return;
    }
    int mid = (lx + rx) / 2;
    if (pos <= mid) {
      update(x * 2, lx, mid, pos, c);
    } else {
      update(x * 2 + 1, mid + 1, rx, pos, c);
    }
    t[x] = t[x * 2] + t[x * 2 + 1];
  }
 
  void update(int pos, int c) {
    update(1, 1, n, pos, c);
  }
 
  node query(int x, int lx, int rx, int st, int dr) {
    if (st <= lx && rx <= dr) {
      return t[x];
    }
    int mid = (lx + rx) / 2;
    if (st <= mid && mid < dr) {
      return query(x * 2, lx, mid, st, dr) + query(x * 2 + 1, mid + 1, rx, st, dr);
    }
    if (st <= mid) {
      return query(x * 2, lx, mid, st, dr);
    }
    return query(x * 2 + 1, mid + 1, rx, st, dr);
  }
 
  node query(int st, int dr) {
    return query(1, 1, n, st, dr);
  }
} t;
 
void dfs1(int u) {
  sz[u] = 1;
  chainTop[u] = u;
  for (int v : g[u]) {
    if (v != p[u]) {
      p[v] = u;
      dfs1(v);
      if (sz[heavySon[u]] < sz[v]) {
        heavySon[u] = v;
      }
      sz[u] += sz[v];
    }
  }
}
 
void dfs2(int u) {
  label[u] = ++labels;
  down[chainTop[u]] = u;
  if (heavySon[u] == 0) {
    return;
  }
  chainTop[heavySon[u]] = chainTop[u];
  dfs2(heavySon[u]);
  for (int v : g[u]) {
    if (v != p[u] && v != heavySon[u]) {
      dfs2(v);
    }
  }
}
 
int getDp(node x, int i) {
  int best = INF;
  for (int j = 0; j < 2; ++j) {
    minSelf(best, x.dp[i][j]);
    minSelf(best, x.dp[i ^ 1][j] + 1);
  }
  return best;
}
 
node update(int v) {
  t.update(label[v], col[v]);
  int root = chainTop[v];
  node chain = t.query(label[root], label[down[root]]);
  if (root == 1) {
    return chain;
  }
  for (int i = 0; i < 2; ++i) {
    sum[label[p[root]]][i] -= last[root][i];
    last[root][i] = getDp(chain, i);
    sum[label[p[root]]][i] += last[root][i];
  }
  return update(p[root]);
}
 
void initialize(int N, vector<int> A, vector<int> B) {
  for (int i = 0; i < N - 1; ++i) {
    g[A[i]].emplace_back(B[i]);
    g[B[i]].emplace_back(A[i]);
  }
  for (int v = 1; v <= N; ++v) {
    col[v] = 2;
  }
  dfs1(1);
  dfs2(1);
  t.init(N);
  t.build(1, 1, N);
}
 
int cat(int v) {
  col[v] = 0;
  node ret = update(v);
  return min(getDp(ret, 0), getDp(ret, 1));
}
 
int dog(int v) {
  col[v] = 1;
  node ret = update(v);
  return min(getDp(ret, 0), getDp(ret, 1));
}
 
int neighbor(int v) {
  col[v] = 2;
  node ret = update(v);
  return min(getDp(ret, 0), getDp(ret, 1));
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 1 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 1 ms 2636 KB Output is correct
12 Correct 1 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 1 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 1 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 1 ms 2636 KB Output is correct
12 Correct 1 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 1 ms 2636 KB Output is correct
17 Correct 3 ms 2764 KB Output is correct
18 Correct 3 ms 2764 KB Output is correct
19 Correct 2 ms 2764 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 3 ms 2764 KB Output is correct
24 Correct 3 ms 2764 KB Output is correct
25 Correct 3 ms 2636 KB Output is correct
26 Correct 3 ms 2712 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2764 KB Output is correct
29 Correct 3 ms 2764 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2764 KB Output is correct
32 Correct 3 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 1 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 1 ms 2636 KB Output is correct
12 Correct 1 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 1 ms 2636 KB Output is correct
17 Correct 3 ms 2764 KB Output is correct
18 Correct 3 ms 2764 KB Output is correct
19 Correct 2 ms 2764 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 3 ms 2764 KB Output is correct
24 Correct 3 ms 2764 KB Output is correct
25 Correct 3 ms 2636 KB Output is correct
26 Correct 3 ms 2712 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2764 KB Output is correct
29 Correct 3 ms 2764 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2764 KB Output is correct
32 Correct 3 ms 2636 KB Output is correct
33 Correct 290 ms 10036 KB Output is correct
34 Correct 101 ms 10284 KB Output is correct
35 Correct 270 ms 8780 KB Output is correct
36 Correct 436 ms 15576 KB Output is correct
37 Correct 15 ms 6348 KB Output is correct
38 Correct 495 ms 16564 KB Output is correct
39 Correct 511 ms 16564 KB Output is correct
40 Correct 466 ms 16504 KB Output is correct
41 Correct 494 ms 16608 KB Output is correct
42 Correct 456 ms 16532 KB Output is correct
43 Correct 471 ms 16564 KB Output is correct
44 Correct 466 ms 16480 KB Output is correct
45 Correct 463 ms 16592 KB Output is correct
46 Correct 471 ms 16460 KB Output is correct
47 Correct 484 ms 16564 KB Output is correct
48 Correct 120 ms 13072 KB Output is correct
49 Correct 140 ms 14468 KB Output is correct
50 Correct 49 ms 5536 KB Output is correct
51 Correct 61 ms 7900 KB Output is correct
52 Correct 22 ms 5304 KB Output is correct
53 Correct 190 ms 15336 KB Output is correct
54 Correct 132 ms 8684 KB Output is correct
55 Correct 376 ms 13840 KB Output is correct
56 Correct 213 ms 9328 KB Output is correct
57 Correct 269 ms 14872 KB Output is correct
58 Correct 22 ms 7808 KB Output is correct
59 Correct 51 ms 6544 KB Output is correct
60 Correct 107 ms 13644 KB Output is correct
61 Correct 112 ms 14012 KB Output is correct
62 Correct 68 ms 12392 KB Output is correct
63 Correct 38 ms 11084 KB Output is correct
64 Correct 41 ms 12724 KB Output is correct
65 Correct 64 ms 19180 KB Output is correct
66 Correct 58 ms 6824 KB Output is correct
67 Correct 54 ms 15456 KB Output is correct
68 Correct 110 ms 19000 KB Output is correct
69 Correct 27 ms 4172 KB Output is correct
70 Correct 7 ms 2892 KB Output is correct
71 Correct 47 ms 10572 KB Output is correct
72 Correct 72 ms 17580 KB Output is correct
73 Correct 175 ms 22008 KB Output is correct
74 Correct 196 ms 18476 KB Output is correct
75 Correct 130 ms 21992 KB Output is correct
76 Correct 144 ms 20720 KB Output is correct
77 Correct 201 ms 18828 KB Output is correct