Submission #1063142

#TimeUsernameProblemLanguageResultExecution timeMemory
1063142BoasTropical Garden (IOI11_garden)C++17
0 / 100
1 ms604 KiB
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vii> vvii;
#define loop(x, i) for (int i = 0; i < x; i++)
#define pb push_back

void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
  vvii adj(N);
  loop(M, i)
  {
    adj[R[i][0]].pb({i, R[i][1]});
    adj[R[i][1]].pb({i, R[i][0]});
  }
  vvi p(30, vi(N));
  vvb otherOption(30, vb(N));
  loop(N, i)
  {
    auto [ix, j] = adj[i][0];
    p[0][i] = j;
    if (adj[j].size() > 1 && adj[j][0] == ii{ix, i})
      otherOption[0][i] = 1;
  }
  typedef pair<int, bool> ib;
  map<ii, ib> dp;
  auto nthParent = [&](auto &&self, int i, int n) -> ib
  {
    int input = i;
    if (dp.count({i, n}))
      return dp[{i, n}];
    bool othOption = 0;
    loop(30, x)
    {
      if ((1 << x) & n)
      {
        if (othOption && adj[i].size() > 1)
        {
          othOption = 0;
          auto [newI, newOthOption] = self(self, adj[i][1].second, (1 << x) - 1);
          i = newI;
          othOption = newOthOption;
        }
        else
        {
          if (otherOption[x][i])
            othOption = 1;
          i = p[x][i];
        }
      }
    }
    return dp[{input, n}] = {i, othOption};
  };
  for (int x = 1; x < 30; x++)
  {
    loop(N, i)
    {
      int half = p[x - 1][i];
      bool opt2 = otherOption[x - 1][i];
      /*if (otherOption[x - 1][i] && adj[i].size() > 1)
      {
        auto [halfN, opt2N] = nthParent(nthParent, adj[i][1].second, (1 << (x - 1)) - 1);
        half = halfN;
        opt2 = opt2N;
      }*/
      int nieuw = p[x - 1][half];
      otherOption[x][i] = otherOption[x - 1][nieuw];
      if (opt2 && adj[half].size() > 1)
      {
        auto [nieuwN, opt2N] = nthParent(nthParent, adj[half][1].second, (1 << (x - 1)) - 1);
        nieuw = nieuwN;
        otherOption[x][i] = opt2N;
      }
      p[x][i] = nieuw;
    }
  }
  for (int i = 0; i < Q; i++)
  {
    int K = G[i];
    int cnt = 0;
    loop(N, start)
    {
      if (nthParent(nthParent, start, K).first == P)
        cnt++;
    }
    answer(cnt);
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...