This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
typedef tuple<int, int, bool> iib;
map<iib, ib> dp;
auto nthParent = [&](auto &&self, int i, int n, bool othOption) -> ib
{
iib input = {i, n, othOption};
if (dp.count(input))
return dp[input];
loop(30, x)
{
if ((1 << x) & n)
{
if (othOption && adj[i].size() > 1)
{
auto [ix, j] = adj[i][1];
othOption = 0;
auto [newI, newOthOption] = self(self, j, (1 << x) - 1, adj[j][0].first == ix);
i = newI;
othOption = newOthOption;
}
else
{
i = p[x][i];
if (otherOption[x][i])
othOption = 1;
}
}
}
return dp[input] = {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][half];
if (opt2 && adj[half].size() > 1)
{
auto [ix, j] = adj[half][1];
auto [nieuwN, opt2N] = nthParent(nthParent, j, (1 << (x - 1)) - 1, adj[j][0].first == ix);
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, 0).first == P)
cnt++;
}
answer(cnt);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |