답안 #674354

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
674354 2022-12-23T18:27:28 Z tibinyte Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 41164 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)
    {
      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});
  }
}
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)
  {
    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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 13 ms 892 KB Output is correct
18 Correct 14 ms 980 KB Output is correct
19 Correct 9 ms 928 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 5 ms 468 KB Output is correct
22 Correct 5 ms 564 KB Output is correct
23 Correct 16 ms 852 KB Output is correct
24 Correct 15 ms 924 KB Output is correct
25 Correct 14 ms 624 KB Output is correct
26 Correct 5 ms 596 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 1032 KB Output is correct
30 Correct 5 ms 480 KB Output is correct
31 Correct 4 ms 852 KB Output is correct
32 Correct 7 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 13 ms 892 KB Output is correct
18 Correct 14 ms 980 KB Output is correct
19 Correct 9 ms 928 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 5 ms 468 KB Output is correct
22 Correct 5 ms 564 KB Output is correct
23 Correct 16 ms 852 KB Output is correct
24 Correct 15 ms 924 KB Output is correct
25 Correct 14 ms 624 KB Output is correct
26 Correct 5 ms 596 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 1032 KB Output is correct
30 Correct 5 ms 480 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 3036 ms 41164 KB Time limit exceeded
34 Halted 0 ms 0 KB -