Submission #497592

# Submission time Handle Problem Language Result Execution time Memory
497592 2021-12-23T10:24:49 Z 600Mihnea Cats or Dogs (JOI18_catdog) C++17
100 / 100
1014 ms 33820 KB
#include "catdog.h"
#include <bits/stdc++.h>

using namespace std;

const int N = (int) 1e5 + 7;
const int INF = (int) 1e8;
int n, sub[N], child[N], color[N], par[N], y, dp1[N], dp2[N], cost1[N], cost2[N], theindex[N], curind, node[N], l1[N], l2[N];
vector<int> members[N];
bool isTheChild[N];
vector<int> g[N];

struct D {
  bool nmc;
  int cost[2][2];
};

D operator + (D a, D b) {
  if (a.nmc) {
    return b;
  }
  if (b.nmc) {
    return a;
  }
  D c;
  c.nmc = 0;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      c.cost[i][j] = INF;
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++) {
        for (int l = 0; l < 2; l++) {
          c.cost[i][l] = min(c.cost[i][l], a.cost[i][j] + b.cost[k][l] + (j != k));
        }
      }
    }
  }
  return c;
}

D t[4 * N];
D wnmc;

void upd(int v, int tl, int tr, int i) {
  if (tr < i || i < tl) {
    return;
  }
  if (tl == tr) {
    vector<int> K = {cost1[node[i]] + dp1[node[i]], cost2[node[i]] + dp2[node[i]]};
    t[v].cost[0][1] = t[v].cost[1][0] = INF;
    t[v].cost[0][0] = K[0];
    t[v].cost[1][1] = K[1];
    return;
  }
  int tm = (tl + tr) / 2;
  upd(2 * v, tl, tm, i);
  upd(2 * v + 1, tm + 1, tr, i);
  t[v] = t[2 * v] + t[2 * v + 1];
}

D get(int v, int tl, int tr, int l, int r) {
  if (tr < l || r < tl) {
    return wnmc;
  }
  if (l <= tl && tr <= r) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  D x = get(2 * v, tl, tm, l, r);
  D y = get(2 * v + 1, tm + 1, tr, l, r);
  D z = x + y;
  return x + y;
}

void buildtr(int v = 1, int tl = 0, int tr = n) {
  if (tl == tr) {
    t[v].cost[0][1] = t[v].cost[1][0] = INF;
    t[v].cost[0][0] = t[v].cost[1][1] = 0;
  } else {
    int tm = (tl + tr) / 2;
    buildtr(2 * v, tl, tm);
    buildtr(2 * v + 1, tm + 1, tr);
    t[v] = t[2 * v] + t[2 * v + 1];
  }
}


void build(int a, int p = 0) {
  vector<int> kids;
  sub[a] = 1;
  par[a] = p;
  for (auto &b : g[a]) {
    if (b != p) {
      kids.push_back(b);
      build(b, a);
      sub[a] += sub[b];
      if (sub[b] > sub[child[a]]) {
        child[a] = b;
      }
    }
  }
  isTheChild[child[a]] = 1;
  g[a] = kids;
}

void compute(int a) {
  if (isTheChild[a]) {
    color[a] = color[par[a]];
  } else {
    color[a] = ++y;
  }
  theindex[a] = ++curind;
  node[theindex[a]] = a;
  members[color[a]].push_back(a);
  if (child[a]) {
    compute(child[a]);
    for (auto &b : g[a]) {
      if (b != child[a]) {
        compute(b);
      }
    }
  }
}

void updVertex(int v) {
  int p = par[members[color[v]][0]];

  dp1[p] -= l1[color[v]];
  dp2[p] -= l2[color[v]];

  upd(1, 0, n, theindex[v]);

  D sol = get(1, 0, n, theindex[members[color[v]][0]], theindex[members[color[v]].back()]);

  l1[color[v]] = min(sol.cost[0][0], sol.cost[0][1]);
  l2[color[v]] = min(sol.cost[1][0], sol.cost[1][1]);

  l1[color[v]] = min(l1[color[v]], l2[color[v]] + 1);
  l2[color[v]] = min(l2[color[v]], l1[color[v]] + 1);


  dp1[p] += l1[color[v]];
  dp2[p] += l2[color[v]];
}


void update(int node) {
  while (node) {
    updVertex(node);
    node = par[members[color[node]][0]];
  }
}


void initialize(int nn, std::vector<int> edgesA, std::vector<int> edgesB) {
  wnmc.nmc = 1;
  n = nn;
  buildtr();
  for (int i = 0; i < n - 1; i++) {
    int a = edgesA[i];
    int b = edgesB[i];
    g[a].push_back(b);
    g[b].push_back(a);
  }
  build(1);
  compute(1);
}

void changeCost(int v, int c1, int c2) {
  dp1[v] -= cost1[v];
  dp2[v] -= cost2[v];

  cost1[v] = c1;
  cost2[v] = c2;

  dp1[v] += cost1[v];
  dp2[v] += cost2[v];


  updVertex(v);

  update(v);

}

int eval() {
  return min(dp1[0], dp2[0]);
}

int cat(int v) {
  changeCost(v, 0, INF);
  return eval();
}

int dog(int v) {
  changeCost(v, INF, 0);
  return eval();
}


int neighbor(int v) {
  changeCost(v, 0, 0);
  return eval();

}

Compilation message

catdog.cpp: In function 'D get(int, int, int, int, int)':
catdog.cpp:74:5: warning: variable 'z' set but not used [-Wunused-but-set-variable]
   74 |   D z = x + y;
      |     ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 5068 KB Output is correct
5 Correct 3 ms 5012 KB Output is correct
6 Correct 3 ms 5068 KB Output is correct
7 Correct 3 ms 5068 KB Output is correct
8 Correct 3 ms 5068 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 3 ms 5000 KB Output is correct
14 Correct 3 ms 5012 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 3 ms 5012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 5068 KB Output is correct
5 Correct 3 ms 5012 KB Output is correct
6 Correct 3 ms 5068 KB Output is correct
7 Correct 3 ms 5068 KB Output is correct
8 Correct 3 ms 5068 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 3 ms 5000 KB Output is correct
14 Correct 3 ms 5012 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 3 ms 5012 KB Output is correct
17 Correct 5 ms 5172 KB Output is correct
18 Correct 6 ms 5128 KB Output is correct
19 Correct 5 ms 5144 KB Output is correct
20 Correct 3 ms 5060 KB Output is correct
21 Correct 4 ms 5016 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 6 ms 5196 KB Output is correct
24 Correct 6 ms 5148 KB Output is correct
25 Correct 5 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 3 ms 5068 KB Output is correct
28 Correct 3 ms 5196 KB Output is correct
29 Correct 5 ms 5196 KB Output is correct
30 Correct 3 ms 5068 KB Output is correct
31 Correct 3 ms 5148 KB Output is correct
32 Correct 4 ms 5020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 5068 KB Output is correct
5 Correct 3 ms 5012 KB Output is correct
6 Correct 3 ms 5068 KB Output is correct
7 Correct 3 ms 5068 KB Output is correct
8 Correct 3 ms 5068 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 3 ms 5000 KB Output is correct
14 Correct 3 ms 5012 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 3 ms 5012 KB Output is correct
17 Correct 5 ms 5172 KB Output is correct
18 Correct 6 ms 5128 KB Output is correct
19 Correct 5 ms 5144 KB Output is correct
20 Correct 3 ms 5060 KB Output is correct
21 Correct 4 ms 5016 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 6 ms 5196 KB Output is correct
24 Correct 6 ms 5148 KB Output is correct
25 Correct 5 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 3 ms 5068 KB Output is correct
28 Correct 3 ms 5196 KB Output is correct
29 Correct 5 ms 5196 KB Output is correct
30 Correct 3 ms 5068 KB Output is correct
31 Correct 3 ms 5148 KB Output is correct
32 Correct 4 ms 5020 KB Output is correct
33 Correct 538 ms 14288 KB Output is correct
34 Correct 166 ms 14768 KB Output is correct
35 Correct 474 ms 12264 KB Output is correct
36 Correct 818 ms 21256 KB Output is correct
37 Correct 20 ms 9752 KB Output is correct
38 Correct 863 ms 22208 KB Output is correct
39 Correct 898 ms 22224 KB Output is correct
40 Correct 923 ms 22284 KB Output is correct
41 Correct 1014 ms 22212 KB Output is correct
42 Correct 914 ms 22228 KB Output is correct
43 Correct 969 ms 22412 KB Output is correct
44 Correct 931 ms 22372 KB Output is correct
45 Correct 876 ms 22376 KB Output is correct
46 Correct 972 ms 22360 KB Output is correct
47 Correct 927 ms 22364 KB Output is correct
48 Correct 283 ms 20392 KB Output is correct
49 Correct 282 ms 22772 KB Output is correct
50 Correct 115 ms 9388 KB Output is correct
51 Correct 111 ms 12904 KB Output is correct
52 Correct 45 ms 9028 KB Output is correct
53 Correct 369 ms 21180 KB Output is correct
54 Correct 286 ms 12920 KB Output is correct
55 Correct 771 ms 19076 KB Output is correct
56 Correct 445 ms 13768 KB Output is correct
57 Correct 635 ms 20644 KB Output is correct
58 Correct 46 ms 13116 KB Output is correct
59 Correct 141 ms 11196 KB Output is correct
60 Correct 261 ms 21456 KB Output is correct
61 Correct 270 ms 21924 KB Output is correct
62 Correct 207 ms 19604 KB Output is correct
63 Correct 92 ms 16836 KB Output is correct
64 Correct 146 ms 19360 KB Output is correct
65 Correct 129 ms 27784 KB Output is correct
66 Correct 215 ms 10764 KB Output is correct
67 Correct 176 ms 22748 KB Output is correct
68 Correct 339 ms 27240 KB Output is correct
69 Correct 109 ms 7248 KB Output is correct
70 Correct 24 ms 5280 KB Output is correct
71 Correct 197 ms 16692 KB Output is correct
72 Correct 229 ms 26824 KB Output is correct
73 Correct 582 ms 33820 KB Output is correct
74 Correct 639 ms 27432 KB Output is correct
75 Correct 492 ms 33696 KB Output is correct
76 Correct 506 ms 31396 KB Output is correct
77 Correct 596 ms 28060 KB Output is correct