Submission #496554

# Submission time Handle Problem Language Result Execution time Memory
496554 2021-12-21T14:04:57 Z 600Mihnea Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 12668 KB
#include "catdog.h"
#include <bits/stdc++.h>

using namespace std;

mt19937 rng((long long) (new char));

const int N = (int) 1e5 + 7;
const int INF = (int) 1e9 + 7;
int n;
int root;
int par[N];
int dp1[N];
int dp2[N];
int cost1[N];
int cost2[N];
vector<int> g[N];

void build(int a, int p = 0) {

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

void dfs(int a) {
  dp1[a] = 0;
  dp2[a] = 0;
  for (auto &b : g[a]) {
    dfs(b);
    dp1[a] += min(dp1[b], 1 + dp2[b]);
    dp2[a] += min(dp2[b], 1 + dp1[b]);
  }
  dp1[a] += cost1[a];
  dp2[a] += cost2[a];
}

void initialize(int nn, std::vector<int> edgesA, std::vector<int> edgesB) {
  n = nn;
  root = rng() % n + 1;
  root = 1;
  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(root);
}

void changerec(int a, int nw1, int nw2) {
  if (!a || (nw1 == dp1[a] && nw2 == dp2[a])) {
    return;
  }

  int p = par[a];

  changerec(p, dp1[p] - min(dp1[a], 1 + dp2[a]) + min(nw1, 1 + nw2), dp2[p] - min(dp2[a], 1 + dp1[a]) + min(nw2, 1 + nw1));

  dp1[a] = nw1;
  dp2[a] = nw2;
}

void change(int v, int c1, int c2) {

  changerec(v, dp1[v] - cost1[v] + c1, dp2[v] - cost2[v] + c2);
  cost1[v] = c1;
  cost2[v] = c2;
}

int cat(int v) {
  change(v, INF, 0);
  return min(dp1[1], dp2[1]);
}

int dog(int v) {
  change(v, 0, INF);
  return min(dp1[1], dp2[1]);
}

int neighbor(int v) {
  change(v, 0, 0);
  return min(dp1[1], dp2[1]);
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 1 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 2 ms 2636 KB Output is correct
6 Correct 2 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 2584 KB Output is correct
11 Correct 1 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 1 ms 2596 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 1 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 1 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 2 ms 2636 KB Output is correct
6 Correct 2 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 2584 KB Output is correct
11 Correct 1 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 1 ms 2596 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 1 ms 2636 KB Output is correct
17 Correct 3 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 3 ms 2636 KB Output is correct
22 Correct 3 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2764 KB Output is correct
29 Correct 2 ms 2764 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 1 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 2 ms 2636 KB Output is correct
6 Correct 2 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 2584 KB Output is correct
11 Correct 1 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 1 ms 2596 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 1 ms 2636 KB Output is correct
17 Correct 3 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 3 ms 2636 KB Output is correct
22 Correct 3 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2764 KB Output is correct
29 Correct 2 ms 2764 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 75 ms 6836 KB Output is correct
34 Correct 54 ms 6756 KB Output is correct
35 Correct 91 ms 6012 KB Output is correct
36 Correct 125 ms 9572 KB Output is correct
37 Correct 15 ms 4616 KB Output is correct
38 Correct 148 ms 10264 KB Output is correct
39 Correct 133 ms 10292 KB Output is correct
40 Correct 170 ms 10276 KB Output is correct
41 Correct 135 ms 10280 KB Output is correct
42 Correct 166 ms 10240 KB Output is correct
43 Correct 154 ms 10276 KB Output is correct
44 Correct 153 ms 10288 KB Output is correct
45 Correct 191 ms 10300 KB Output is correct
46 Correct 142 ms 10292 KB Output is correct
47 Correct 132 ms 10216 KB Output is correct
48 Correct 48 ms 8212 KB Output is correct
49 Correct 55 ms 9476 KB Output is correct
50 Correct 16 ms 4388 KB Output is correct
51 Correct 20 ms 5460 KB Output is correct
52 Correct 11 ms 4232 KB Output is correct
53 Correct 81 ms 9064 KB Output is correct
54 Correct 46 ms 5724 KB Output is correct
55 Correct 121 ms 8232 KB Output is correct
56 Correct 74 ms 6300 KB Output is correct
57 Correct 123 ms 8996 KB Output is correct
58 Correct 15 ms 5468 KB Output is correct
59 Correct 21 ms 5432 KB Output is correct
60 Correct 48 ms 8708 KB Output is correct
61 Correct 50 ms 9088 KB Output is correct
62 Correct 34 ms 7600 KB Output is correct
63 Correct 2552 ms 10216 KB Output is correct
64 Execution timed out 3062 ms 12668 KB Time limit exceeded
65 Halted 0 ms 0 KB -