#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
vector<int> deg(N);
vector<int> best(N, -1);
for (int i = 0; i < M; ++i) {
for (int z = 0; z < 2; ++z) {
++deg[R[i][z]];
if (best[R[i][z]] == -1) best[R[i][z]] = i;
}
}
vector<int> nxt(N * 2, -1);
for (int i = 0; i < M; ++i) {
for (int z = 0; z < 2; ++z) {
int to = R[i][!z];
if (best[R[i][!z]] == i && deg[R[i][!z]] > 1) to += N;
if (nxt[R[i][z]] == -1) nxt[R[i][z]] = to;
else if (nxt[R[i][z] + N] == -1) nxt[R[i][z] + N] = to;
}
}
vector<vector<int>> adj(2 * N);
for (int i = 0; i < N; ++i) {
adj[nxt[i]].emplace_back(i);
if (deg[i] > 1) adj[nxt[i + N]].emplace_back(i + N);
}
vector<pair<int, int>> qs;
for (int i = 0; i < Q; ++i) qs.emplace_back(G[i], i);
sort(qs.begin(), qs.end());
vector<int> ans(Q);
auto go = [&](int start) {
vector<int> dist(2 * N, -1);
vector<int> q;
dist[start] = 0;
q.emplace_back(start);
for (int i = 0; i < int(q.size()); ++i) {
int v = q[i];
for (int u : adj[v]) if (dist[u] == -1) {
dist[u] = dist[v] + 1;
q.emplace_back(u);
}
}
vector<int> dists;
for (int v : q) if (v < N) dists.emplace_back(dist[v]);
int len = 0;
vector<bool> visited(2 * N);
int v = start;
while (!visited[v]) {
visited[v] = true;
v = nxt[v];
++len;
}
if (v != start) len = -1;
vector<int> cnts(2 * N);
int ptr = 0;
for (int i = 0; i < Q; ++i) {
while (ptr < int(dists.size()) && dists[ptr] <= qs[i].first) {
int cur = dists[ptr++];
if (len != -1) cur %= len;
++cnts[cur];
}
int cur = qs[i].first;
if (len != -1) cur %= len;
if (cur < 2 * N) ans[qs[i].second] += cnts[cur];
}
};
go(P);
if (deg[P] > 1) go(P + N);
for (int i = 0; i < Q; ++i) answer(ans[i]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
7 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
7 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
14 ms |
3488 KB |
Output is correct |
12 |
Correct |
27 ms |
5760 KB |
Output is correct |
13 |
Correct |
53 ms |
15640 KB |
Output is correct |
14 |
Correct |
90 ms |
18420 KB |
Output is correct |
15 |
Correct |
114 ms |
19884 KB |
Output is correct |
16 |
Correct |
85 ms |
14956 KB |
Output is correct |
17 |
Correct |
90 ms |
13452 KB |
Output is correct |
18 |
Correct |
29 ms |
5676 KB |
Output is correct |
19 |
Correct |
86 ms |
18424 KB |
Output is correct |
20 |
Correct |
129 ms |
20204 KB |
Output is correct |
21 |
Correct |
94 ms |
14956 KB |
Output is correct |
22 |
Correct |
83 ms |
12664 KB |
Output is correct |
23 |
Correct |
91 ms |
20088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
7 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
14 ms |
3488 KB |
Output is correct |
12 |
Correct |
27 ms |
5760 KB |
Output is correct |
13 |
Correct |
53 ms |
15640 KB |
Output is correct |
14 |
Correct |
90 ms |
18420 KB |
Output is correct |
15 |
Correct |
114 ms |
19884 KB |
Output is correct |
16 |
Correct |
85 ms |
14956 KB |
Output is correct |
17 |
Correct |
90 ms |
13452 KB |
Output is correct |
18 |
Correct |
29 ms |
5676 KB |
Output is correct |
19 |
Correct |
86 ms |
18424 KB |
Output is correct |
20 |
Correct |
129 ms |
20204 KB |
Output is correct |
21 |
Correct |
94 ms |
14956 KB |
Output is correct |
22 |
Correct |
83 ms |
12664 KB |
Output is correct |
23 |
Correct |
91 ms |
20088 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
15 ms |
3584 KB |
Output is correct |
26 |
Correct |
28 ms |
5736 KB |
Output is correct |
27 |
Correct |
50 ms |
15780 KB |
Output is correct |
28 |
Correct |
88 ms |
18540 KB |
Output is correct |
29 |
Correct |
123 ms |
20332 KB |
Output is correct |
30 |
Correct |
86 ms |
15084 KB |
Output is correct |
31 |
Correct |
78 ms |
13512 KB |
Output is correct |
32 |
Correct |
27 ms |
5760 KB |
Output is correct |
33 |
Correct |
82 ms |
18560 KB |
Output is correct |
34 |
Correct |
118 ms |
19956 KB |
Output is correct |
35 |
Correct |
94 ms |
15084 KB |
Output is correct |
36 |
Correct |
100 ms |
13552 KB |
Output is correct |
37 |
Correct |
88 ms |
20088 KB |
Output is correct |
38 |
Correct |
88 ms |
26236 KB |
Output is correct |