Submission #674352

# Submission time Handle Problem Language Result Execution time Memory
674352 2022-12-23T18:25:36 Z tibinyte Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 41432 KB
#include <bits/stdc++.h>
#include "catdog.h"
using namespace std;
const int inf = 1e5;

struct aint
{
  vector<vector<vector<int>>> a;
  void init(int n)
  {
    a = vector<vector<vector<int>>>(4 * n, vector<vector<int>>(2, vector<int>(2)));
  }
  vector<vector<int>> combine(vector<vector<int>> a, vector<vector<int>> b)
  {
    vector<vector<int>> ans(2, vector<int>(2, inf));
    if (a == ans)
    {
      return b;
    }
    if (b == ans)
    {
      return a;
    }
    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)
          {
            ans[i][j] = min(ans[i][j], a[i][k] + b[l][j] + (k != l));
          }
        }
      }
    }
    return ans;
  }
  void update(int node, int left, int right, int pos, pair<int, int> val)
  {
    if (left == right)
    {
      //cout << pos << ' ' << val.first << ' ' << val.second << '\n';
      a[node][0][0] = val.first;
      a[node][1][1] = val.second;
      a[node][0][1] = inf;
      a[node][1][0] = inf;
      return;
    }
    int mid = (left + right) / 2;
    if (pos <= mid)
    {
      update(2 * node, left, mid, pos, val);
    }
    else
    {
      update(2 * node + 1, mid + 1, right, pos, val);
    }
    a[node] = combine(a[2 * node], a[2 * node + 1]);
  }
  vector<vector<int>> query(int node, int left, int right, int st, int dr)
  {
    if (right < st || left > dr)
    {
      vector<vector<int>> ans(2, vector<int>(2, inf));
      return ans;
    }
    if (st <= left && dr >= right)
    {
      return a[node];
    }
    int mid = (left + right) / 2;
    return combine(query(2 * node, left, mid, st, dr), query(2 * node + 1, mid + 1, right, st, dr));
  }
};

int n;

vector<int> a;

vector<vector<int>> g;

vector<int> heavy, pos, head, par, down;

vector<vector<int>> sum, prv;

aint tree;

int p = 0;
void init_hld()
{
  pos = head = par = down = vector<int>(n + 1);
  heavy = vector<int>(n + 1, -1);
  function<int(int, int)> dfs = [&](int node, int parent)
  {
    int sz = 1;
    int maxi = 0;
    for (auto i : g[node])
    {
      if (i != parent)
      {
        int cine = dfs(i, node);
        sz += cine;
        if (cine > maxi)
        {
          maxi = cine;
          heavy[node] = i;
        }
      }
    }
    return sz;
  };
  dfs(1, 0);
  function<void(int, int, int)> get = [&](int node, int boss, int parent)
  {
    par[node] = parent;
    head[node] = boss;
    pos[node] = ++p;
    down[head[node]] = node;
    if (heavy[node] != -1)
    {
      get(heavy[node], boss, node);
    }
    for (auto i : g[node])
    {
      if (i != parent && i != heavy[node])
      {
        get(i, i, node);
      }
    }
  };
  get(1, 1, 0);
}

void initialize(int N, vector<int> A, vector<int> B)
{
  n = N;
  g = vector<vector<int>>(n + 1);
  a = vector<int>(n + 1, 2);
  prv = sum = vector<vector<int>>(n + 1, vector<int>(2));
  for (int i = 0; i <= n - 2; ++i)
  {
    g[A[i]].push_back(B[i]);
    g[B[i]].push_back(A[i]);
  }
  init_hld();
  tree.init(n);
  for (int i = 1; i <= n; ++i)
  {
    tree.update(1, 1, n, i, {0, 0});
  }
  for (int i = 1; i <= n; ++i)
  {
    //cout << pos[i] << ' ';
  }
  //cout << '\n';
}

vector<vector<int>> update(int node)
{
  if (a[node] == 0)
  {
    tree.update(1, 1, n, pos[node], {sum[node][0], inf});
  }
  if (a[node] == 1)
  {
    tree.update(1, 1, n, pos[node], {inf, sum[node][1]});
  }
  if (a[node] == 2)
  {
    tree.update(1, 1, n, pos[node], {sum[node][0], sum[node][1]});
  }
  int qui = head[node];
  vector<vector<int>> dp_qui = tree.query(1, 1, n, pos[qui], pos[down[qui]]);
  if (qui == 1)
  {
    /*if (node == 4 && a[node] == 0)
    {
      cout << "MAGIE: " << pos[qui] << ' ' << down[qui] << '\n';
      exit(0);
    }*/
    return dp_qui;
  }
  for (int i = 0; i < 2; ++i)
  {
    sum[par[qui]][i] -= prv[qui][i];
    int best = inf;
    for (int j = 0; j < 2; ++j)
    {
      best = min(best, dp_qui[i][j]);
      best = min(best, dp_qui[i ^ 1][j] + 1);
    }
    prv[qui][i] = best;
    sum[par[qui]][i] += prv[qui][i];
  }
  return update(par[qui]);
}

int cat(int v)
{
  a[v] = 0;
  vector<vector<int>> ans = update(v);
  int best = inf;
  for (int i = 0; i < 2; ++i)
  {
    for (int j = 0; j < 2; ++j)
    {
      best = min(best, ans[i][j]);
    }
  }
  return best;
}

int dog(int v)
{
  a[v] = 1;
  vector<vector<int>> ans = update(v);
  int best = inf;
  for (int i = 0; i < 2; ++i)
  {
    for (int j = 0; j < 2; ++j)
    {
      best = min(best, ans[i][j]);
    }
  }
  return best;
}

int neighbor(int v)
{
  a[v] = 2;
  vector<vector<int>> ans = update(v);
  int best = inf;
  for (int i = 0; i < 2; ++i)
  {
    for (int j = 0; j < 2; ++j)
    {
      best = min(best, ans[i][j]);
    }
  }
  return best;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 17 ms 816 KB Output is correct
18 Correct 14 ms 1064 KB Output is correct
19 Correct 8 ms 852 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 5 ms 520 KB Output is correct
22 Correct 6 ms 468 KB Output is correct
23 Correct 15 ms 988 KB Output is correct
24 Correct 15 ms 936 KB Output is correct
25 Correct 12 ms 596 KB Output is correct
26 Correct 5 ms 560 KB Output is correct
27 Correct 3 ms 468 KB Output is correct
28 Correct 4 ms 980 KB Output is correct
29 Correct 8 ms 980 KB Output is correct
30 Correct 5 ms 468 KB Output is correct
31 Correct 4 ms 852 KB Output is correct
32 Correct 7 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 17 ms 816 KB Output is correct
18 Correct 14 ms 1064 KB Output is correct
19 Correct 8 ms 852 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 5 ms 520 KB Output is correct
22 Correct 6 ms 468 KB Output is correct
23 Correct 15 ms 988 KB Output is correct
24 Correct 15 ms 936 KB Output is correct
25 Correct 12 ms 596 KB Output is correct
26 Correct 5 ms 560 KB Output is correct
27 Correct 3 ms 468 KB Output is correct
28 Correct 4 ms 980 KB Output is correct
29 Correct 8 ms 980 KB Output is correct
30 Correct 5 ms 468 KB Output is correct
31 Correct 4 ms 852 KB Output is correct
32 Correct 7 ms 468 KB Output is correct
33 Execution timed out 3066 ms 41432 KB Time limit exceeded
34 Halted 0 ms 0 KB -