#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1.5e5 + 20;
vector<int> adj[maxN];
int nxt[maxN * 2];
int dist[maxN * 2][2];
int cnt[maxN * 2][2];
int cycle[2];
int dest;
void dfs(int u, int k) {
if (u == dest) {
dist[u][k] = 0;
return;
}
dist[u][k] = -2;
int v = nxt[u];
if (dist[v][k] == -2) {
dist[u][k] = -1;
return;
}
else if (dist[v][k] == -3) {
dfs(v, k);
}
if (dist[v][k] == -1) {
dist[u][k] = -1;
return;
}
else {
dist[u][k] = dist[v][k] + 1;
return;
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
for (int i = 0; i < M; i++) {
int u = R[i][0];
int v = R[i][1];
adj[u].push_back(i << 1);
adj[v].push_back(i << 1 | 1);
}
for (int i = 0; i < N; i++) {
if ((int)adj[i].size() == 1) {
nxt[adj[i][0] ^ 1] = adj[i][0];
}
else {
nxt[adj[i][0] ^ 1] = adj[i][1];
for (int j = 1; j < (int)adj[i].size(); j++) {
nxt[adj[i][j] ^ 1] = adj[i][0];
}
}
}
for (int k = 0; k < min(2, (int)adj[P].size()); k++) {
dest = adj[P][k];
for (int i = 0; i < M * 2; i++) {
dist[i][k] = -3;
}
for (int i = 0; i < M * 2; i++) {
if (dist[i][k] == -3) {
dfs(i, k);
}
}
cycle[k] = (dist[nxt[dest]][k] == -1 ? -1 : dist[nxt[dest]][k] + 1);
for (int i = 0; i < N; i++) {
int start = adj[i][0];
if (dist[start][k] != -1) {
cnt[dist[start][k]][k]++;
}
}
if (cycle[k] != -1) {
for (int i = cycle[k]; i < maxN * 2; i++) {
cnt[i][k] += cnt[i - cycle[k]][k];
}
}
}
for (int i = 0; i < Q; i++) {
int res = 0;
for (int k = 0; k < min(2, (int)adj[P].size()); k++) {
if (cycle[k] == -1) {
if (G[i] < maxN * 2) {
res += cnt[G[i]][k];
}
}
else {
int tmp = G[i];
if (G[i] >= maxN * 2) {
G[i] -= ((G[i] - maxN * 2) / cycle[k] + 1) * cycle[k];
}
res += cnt[G[i]][k];
G[i] = tmp;
}
}
answer(res);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6228 KB |
Output is correct |
2 |
Correct |
3 ms |
6228 KB |
Output is correct |
3 |
Correct |
3 ms |
6280 KB |
Output is correct |
4 |
Correct |
2 ms |
6100 KB |
Output is correct |
5 |
Correct |
3 ms |
6100 KB |
Output is correct |
6 |
Correct |
3 ms |
6408 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
3 ms |
6228 KB |
Output is correct |
9 |
Correct |
4 ms |
6612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6228 KB |
Output is correct |
2 |
Correct |
3 ms |
6228 KB |
Output is correct |
3 |
Correct |
3 ms |
6280 KB |
Output is correct |
4 |
Correct |
2 ms |
6100 KB |
Output is correct |
5 |
Correct |
3 ms |
6100 KB |
Output is correct |
6 |
Correct |
3 ms |
6408 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
3 ms |
6228 KB |
Output is correct |
9 |
Correct |
4 ms |
6612 KB |
Output is correct |
10 |
Correct |
3 ms |
6100 KB |
Output is correct |
11 |
Correct |
10 ms |
7812 KB |
Output is correct |
12 |
Correct |
17 ms |
9428 KB |
Output is correct |
13 |
Correct |
26 ms |
18772 KB |
Output is correct |
14 |
Correct |
46 ms |
15060 KB |
Output is correct |
15 |
Correct |
61 ms |
15216 KB |
Output is correct |
16 |
Correct |
43 ms |
13920 KB |
Output is correct |
17 |
Correct |
44 ms |
13520 KB |
Output is correct |
18 |
Correct |
17 ms |
6740 KB |
Output is correct |
19 |
Correct |
44 ms |
15060 KB |
Output is correct |
20 |
Correct |
48 ms |
15268 KB |
Output is correct |
21 |
Correct |
43 ms |
13784 KB |
Output is correct |
22 |
Correct |
46 ms |
13388 KB |
Output is correct |
23 |
Correct |
46 ms |
17612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6228 KB |
Output is correct |
2 |
Correct |
3 ms |
6228 KB |
Output is correct |
3 |
Correct |
3 ms |
6280 KB |
Output is correct |
4 |
Correct |
2 ms |
6100 KB |
Output is correct |
5 |
Correct |
3 ms |
6100 KB |
Output is correct |
6 |
Correct |
3 ms |
6408 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
3 ms |
6228 KB |
Output is correct |
9 |
Correct |
4 ms |
6612 KB |
Output is correct |
10 |
Correct |
3 ms |
6100 KB |
Output is correct |
11 |
Correct |
10 ms |
7812 KB |
Output is correct |
12 |
Correct |
17 ms |
9428 KB |
Output is correct |
13 |
Correct |
26 ms |
18772 KB |
Output is correct |
14 |
Correct |
46 ms |
15060 KB |
Output is correct |
15 |
Correct |
61 ms |
15216 KB |
Output is correct |
16 |
Correct |
43 ms |
13920 KB |
Output is correct |
17 |
Correct |
44 ms |
13520 KB |
Output is correct |
18 |
Correct |
17 ms |
6740 KB |
Output is correct |
19 |
Correct |
44 ms |
15060 KB |
Output is correct |
20 |
Correct |
48 ms |
15268 KB |
Output is correct |
21 |
Correct |
43 ms |
13784 KB |
Output is correct |
22 |
Correct |
46 ms |
13388 KB |
Output is correct |
23 |
Correct |
46 ms |
17612 KB |
Output is correct |
24 |
Correct |
3 ms |
6228 KB |
Output is correct |
25 |
Correct |
12 ms |
8080 KB |
Output is correct |
26 |
Correct |
18 ms |
10008 KB |
Output is correct |
27 |
Correct |
28 ms |
19840 KB |
Output is correct |
28 |
Correct |
45 ms |
16668 KB |
Output is correct |
29 |
Correct |
47 ms |
17076 KB |
Output is correct |
30 |
Correct |
41 ms |
15444 KB |
Output is correct |
31 |
Correct |
41 ms |
15096 KB |
Output is correct |
32 |
Correct |
17 ms |
7448 KB |
Output is correct |
33 |
Correct |
48 ms |
16652 KB |
Output is correct |
34 |
Correct |
48 ms |
17112 KB |
Output is correct |
35 |
Correct |
44 ms |
15308 KB |
Output is correct |
36 |
Correct |
46 ms |
15072 KB |
Output is correct |
37 |
Correct |
48 ms |
17756 KB |
Output is correct |
38 |
Correct |
58 ms |
24396 KB |
Output is correct |