Submission #496556

# Submission time Handle Problem Language Result Execution time Memory
496556 2021-12-21T14:06:26 Z 600Mihnea Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 11972 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;

  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[root], dp2[root]);
}

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

int neighbor(int v) {
  change(v, 0, 0);
  return min(dp1[root], dp2[root]);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 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 1 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 1 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 1 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 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 1 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 1 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 1 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 3 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 2 ms 2636 KB Output is correct
22 Correct 2 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 1 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 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 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 1 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 1 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 1 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 3 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 2 ms 2636 KB Output is correct
22 Correct 2 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 1 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 6804 KB Output is correct
34 Correct 44 ms 6744 KB Output is correct
35 Correct 105 ms 6124 KB Output is correct
36 Correct 115 ms 9600 KB Output is correct
37 Correct 12 ms 4684 KB Output is correct
38 Correct 147 ms 10220 KB Output is correct
39 Correct 169 ms 10292 KB Output is correct
40 Correct 164 ms 10240 KB Output is correct
41 Correct 133 ms 10292 KB Output is correct
42 Correct 177 ms 10344 KB Output is correct
43 Correct 148 ms 10288 KB Output is correct
44 Correct 144 ms 10280 KB Output is correct
45 Correct 140 ms 10320 KB Output is correct
46 Correct 154 ms 10292 KB Output is correct
47 Correct 145 ms 10288 KB Output is correct
48 Correct 42 ms 8200 KB Output is correct
49 Correct 52 ms 9476 KB Output is correct
50 Correct 17 ms 4388 KB Output is correct
51 Correct 20 ms 5452 KB Output is correct
52 Correct 11 ms 4184 KB Output is correct
53 Correct 78 ms 9056 KB Output is correct
54 Correct 45 ms 5756 KB Output is correct
55 Correct 112 ms 8180 KB Output is correct
56 Correct 64 ms 6252 KB Output is correct
57 Correct 106 ms 8968 KB Output is correct
58 Correct 15 ms 5468 KB Output is correct
59 Correct 24 ms 5400 KB Output is correct
60 Correct 47 ms 8736 KB Output is correct
61 Correct 49 ms 8988 KB Output is correct
62 Correct 35 ms 7620 KB Output is correct
63 Execution timed out 3051 ms 11972 KB Time limit exceeded
64 Halted 0 ms 0 KB -