이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "garden.h"
#include <iostream>
#include <vector>
#include "gardenlib.h"
using namespace std;
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
vector<int> n1(N, -1), n2(N, -1);
for (int i = M - 1; i >= 0; i--) {
int u = R[i][0], v = R[i][1];
n2[u] = n1[u];
n1[u] = v;
n2[v] = n1[v];
n1[v] = u;
}
for (int i = 0; i < N; i++) {
if (n2[i] == -1) n2[i] = n1[i];
}
vector<vector<pair<int, bool>>> next1(31, vector<pair<int, bool>>(N)),
next2(31, vector<pair<int, bool>>(N));
for (int i = 0; i < N; i++) {
next1[0][i] = {n1[i], n1[n1[i]] != i};
next2[0][i] = {n2[i], n1[n2[i]] != i};
}
for (int l = 1; l <= 30; l++) {
for (int i = 0; i < N; i++) {
{
auto [at, c] = next1[l - 1][i];
if (c) {
next1[l][i] = next1[l - 1][at];
} else {
next1[l][i] = next2[l - 1][at];
}
// cout << "n " << i << " 1 " << (1 << l) << " -> "
// << next1[l][i].first << " " << next1[l][i].second << "\n";
}
{
auto [at, c] = next2[l - 1][i];
if (c) {
next2[l][i] = next1[l - 1][at];
} else {
next2[l][i] = next2[l - 1][at];
}
// cout << "n " << i << " 0 " << (1 << l) << " -> "
// << next2[l][i].first << " " << next2[l][i].second <<
// "\n";
}
}
}
for (int i = 0; i < Q; i++) {
int K = G[i], ans = 0;
for (int j = 0; j < N; j++) {
// cout << "try " << j << " " << K << " \n";
int cur = j, c = true;
for (int k = 0; k <= 30; k++) {
if (K & (1 << k)) {
// cout << "step " << cur << " " << c << " to ";
if (c) {
auto [cur1, c1] = next1[k][cur];
cur = cur1;
c = c1;
} else {
auto [cur1, c1] = next2[k][cur];
cur = cur1;
c = c1;
}
// cout << cur << " " << c << " in " << (1 << k) << "\n";
}
}
// cout << "start " << j << " steps " << K << " dest " << cur <<
// "\n";
if (cur == P) {
// cout << "ok " << j << " " << K << " \n";
ans++;
}
}
answer(ans);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |