Submission #496555

# Submission time Handle Problem Language Result Execution time Memory
496555 2021-12-21T14:05:52 Z 600Mihnea Cats or Dogs (JOI18_catdog) C++17
0 / 100
1 ms 2636 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 = abs(int(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[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 1 ms 2636 KB Output is correct
2 Incorrect 1 ms 2636 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Incorrect 1 ms 2636 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Incorrect 1 ms 2636 KB Output isn't correct
3 Halted 0 ms 0 KB -