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<bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
void count_routes(int n, int m, int p, int r[][2], int q, int g[]) {
vector<int> togo(2*n+1,-1);
for (int i = 0; i < m; i++) {
auto [a,b] = r[i];
if (!~togo[2*a]) togo[2*a] = 2*b+(!~togo[2*b]);
else if (!~togo[2*a+1]) togo[2*a+1] = 2*b+(!~togo[2*b]);
if (!~togo[2*b]) togo[2*b] = 2*a+(togo[2*a]/2==b);
else if (!~togo[2*b+1]) togo[2*b+1] = 2*a+(togo[2*a]/2==b);
}
n *= 2;
vector<vector<int>> adj(n+1);
for (int i = 0; i < n; i++) {
if (!~togo[i]) togo[i] = togo[i-1];
adj[togo[i]].push_back(i);
}
vector<int> cmp(n+1), dist(n+1), to(n+1,-1), ind(n+1); vector<vector<int>> cyc(n+1); int cc = 0;
for (int i = 0; i < n; i++) if (!cmp[i]) {
int cur = i; ++cc;
while (!cmp[cur]) {
cmp[cur] = cc, cur = togo[cur];
}
int o = cur; queue<int> qq;
do {
ind[cur] = (int)cyc[cc].size(); cyc[cc].push_back(cur); to[cur] = cur; qq.push(cur);
} while ((cur = togo[cur]) != o);
while (!qq.empty()) {
int cur = qq.front(); qq.pop();
for (int j : adj[cur]) if (!~to[j]) {
to[j] = to[cur]; cmp[j] = cc;
dist[j] = dist[cur] + 1;
qq.push(j);
}
}
}
array<vector<int>,2> temp;
temp[0].resize(n); temp[1].resize(n);
for (int i = 2*p; i <= 2*p+1; i++) if (dist[i]) {
queue<int> qq; qq.push(i); int par = i % 2;
fill(temp[par].begin(),temp[par].end(),1e9);
temp[par][i] = 0;
while (!qq.empty()) {
int cur = qq.front(); qq.pop();
for (int j : adj[cur]) if (temp[par][cur] + 1 < temp[par][j]) {
temp[par][j] = temp[par][cur] + 1;
qq.push(j);
}
}
}
for (int i = 0; i < q; i++) {
int ret = 0, k = g[i];
for (int i = 0; i < n; i += 2) ret += temp[0][i] == k || temp[1][i] == k || (dist[i] <= k && cyc[cmp[i]][(ind[to[i]] + k - dist[i]) % ((int)cyc[cmp[i]].size())]/2 == p);
answer(ret);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |