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>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
#ifdef tabr
void answer(int n) {
cout << n << '\n';
}
#else
#include "garden.h"
#include "gardenlib.h"
#endif
void count_routes(int n, int m, int p, int r[][2], int q, int k[]) {
int g[150000][2] = {};
for (int i = 0; i < n; i++) {
g[i][0] = g[i][1] = -1;
}
for (int i = 0; i < m; i++) {
int x = r[i][0];
int y = r[i][1];
if (g[x][0] == -1) {
g[x][0] = y;
} else if (g[x][1] == -1) {
g[x][1] = y;
}
if (g[y][0] == -1) {
g[y][0] = x;
} else if (g[y][1] == -1) {
g[y][1] = x;
}
}
int nxt[30][300000] = {};
for (int i = 0; i < n; i++) {
nxt[0][i] = g[i][0];
nxt[0][i + n] = (g[i][1] == -1 ? g[i][0] : g[i][1]);
if (g[nxt[0][i]][0] == i) {
nxt[0][i] += n;
}
if (g[nxt[0][i + n]][0] == i) {
nxt[0][i + n] += n;
}
}
for (int i = 1; i < 30; i++) {
for (int j = 0; j < 2 * n; j++) {
nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
}
}
for (int i = 0; i < q; i++) {
int ans = 0;
for (int j = 0; j < n; j++) {
int v = j;
for (int l = 29; l >= 0; l--) {
if (k[i] & (1 << l)) {
v = nxt[l][v];
}
}
if (v % n == p) {
ans++;
}
debug(v, j);
}
answer(ans);
}
}
#ifdef tabr
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, p, q;
cin >> n >> m >> p >> q;
int r[100][2];
int k[100];
for (int i = 0; i < m; i++) {
cin >> r[i][0] >> r[i][1];
}
for (int i = 0; i < q; i++) {
cin >> k[i];
}
count_routes(n, m, p, r, q, k);
return 0;
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |