#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
vector<int> adj[150005];
vector<int> sadj[300005];
int v[300005], s[300005], cnt[300005];
int vn, sn;
stack<int> st;
int dfs(int now) {
int ret = v[now] = vn++;
st.push(now);
for (auto &there : sadj[now]) {
if (v[there] == -1)
ret = min(ret, dfs(there));
else if (s[there] == -1)
ret = min(ret, v[there]);
}
if (ret == v[now]) {
while (true) {
int temp = st.top();
st.pop();
s[temp] = sn;
cnt[sn]++;
if (temp == now)break;
}
sn++;
}
return ret;
}
vector<int> dist[2][300005];
int trip[300005];
void bfs(int st, int mode) {
int k = cnt[s[st]];
memset(trip, -1, sizeof(trip));
queue<int> q;
q.push(st);
trip[st] = 0;
while (!q.empty()) {
int now = q.front();
q.pop();
if (now % 2 == 0)dist[mode][k == 1 ? trip[now] : trip[now] % k].push_back(trip[now]);
for (auto &there : sadj[now]) {
if (trip[there] == -1) {
trip[there] = trip[now] + 1;
q.push(there);
}
}
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
n = N; m = M; p = P;
for (int i = 0; i < m; i++) {
adj[R[i][0]].push_back(R[i][1]);
adj[R[i][1]].push_back(R[i][0]);
}
for (int now = 0; now < n; now++) {
int there = adj[now][0];
int u = now * 2;
int v = there * 2 + (adj[there].size() > 1 && adj[there][0] == now);
sadj[v].push_back(u);
if (adj[now].size() > 1) {
there = adj[now][1];
int u = now * 2 + 1;
int v = there * 2 + (adj[there].size() > 1 && adj[there][0] == now);
sadj[v].push_back(u);
}
}
memset(v, -1, sizeof(v));
memset(s, -1, sizeof(s));
for (int i = 0; i < n * 2; i++)
if (v[i] == -1)dfs(i);
for (int i = 0; i < 2; i++)
bfs(p * 2 + i, i);
for (int i = 0; i < Q; i++) {
int ans = 0;
for (int j = 0; j < 2; j++) {
int st = p * 2 + j;
int k = cnt[s[st]];
if (k == 1) {
if(G[i]<300005)
ans += dist[j][G[i]].size();
}
else {
ans += upper_bound(dist[j][G[i] % k].begin(), dist[j][G[i] % k].end(), G[i]) - dist[j][G[i] % k].begin();
}
}
answer(ans);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
28664 KB |
Output is correct |
2 |
Correct |
29 ms |
28636 KB |
Output is correct |
3 |
Correct |
29 ms |
28644 KB |
Output is correct |
4 |
Correct |
29 ms |
28628 KB |
Output is correct |
5 |
Correct |
29 ms |
28508 KB |
Output is correct |
6 |
Correct |
30 ms |
28684 KB |
Output is correct |
7 |
Correct |
29 ms |
28496 KB |
Output is correct |
8 |
Correct |
34 ms |
28664 KB |
Output is correct |
9 |
Correct |
31 ms |
28792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
28664 KB |
Output is correct |
2 |
Correct |
29 ms |
28636 KB |
Output is correct |
3 |
Correct |
29 ms |
28644 KB |
Output is correct |
4 |
Correct |
29 ms |
28628 KB |
Output is correct |
5 |
Correct |
29 ms |
28508 KB |
Output is correct |
6 |
Correct |
30 ms |
28684 KB |
Output is correct |
7 |
Correct |
29 ms |
28496 KB |
Output is correct |
8 |
Correct |
34 ms |
28664 KB |
Output is correct |
9 |
Correct |
31 ms |
28792 KB |
Output is correct |
10 |
Correct |
29 ms |
28540 KB |
Output is correct |
11 |
Correct |
45 ms |
30884 KB |
Output is correct |
12 |
Correct |
77 ms |
32476 KB |
Output is correct |
13 |
Correct |
100 ms |
51360 KB |
Output is correct |
14 |
Correct |
244 ms |
40728 KB |
Output is correct |
15 |
Correct |
305 ms |
41428 KB |
Output is correct |
16 |
Correct |
301 ms |
38496 KB |
Output is correct |
17 |
Correct |
214 ms |
36976 KB |
Output is correct |
18 |
Correct |
75 ms |
32264 KB |
Output is correct |
19 |
Correct |
238 ms |
39628 KB |
Output is correct |
20 |
Correct |
278 ms |
40444 KB |
Output is correct |
21 |
Correct |
230 ms |
37512 KB |
Output is correct |
22 |
Correct |
204 ms |
36208 KB |
Output is correct |
23 |
Correct |
256 ms |
40776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
28664 KB |
Output is correct |
2 |
Correct |
29 ms |
28636 KB |
Output is correct |
3 |
Correct |
29 ms |
28644 KB |
Output is correct |
4 |
Correct |
29 ms |
28628 KB |
Output is correct |
5 |
Correct |
29 ms |
28508 KB |
Output is correct |
6 |
Correct |
30 ms |
28684 KB |
Output is correct |
7 |
Correct |
29 ms |
28496 KB |
Output is correct |
8 |
Correct |
34 ms |
28664 KB |
Output is correct |
9 |
Correct |
31 ms |
28792 KB |
Output is correct |
10 |
Correct |
29 ms |
28540 KB |
Output is correct |
11 |
Correct |
45 ms |
30884 KB |
Output is correct |
12 |
Correct |
77 ms |
32476 KB |
Output is correct |
13 |
Correct |
100 ms |
51360 KB |
Output is correct |
14 |
Correct |
244 ms |
40728 KB |
Output is correct |
15 |
Correct |
305 ms |
41428 KB |
Output is correct |
16 |
Correct |
301 ms |
38496 KB |
Output is correct |
17 |
Correct |
214 ms |
36976 KB |
Output is correct |
18 |
Correct |
75 ms |
32264 KB |
Output is correct |
19 |
Correct |
238 ms |
39628 KB |
Output is correct |
20 |
Correct |
278 ms |
40444 KB |
Output is correct |
21 |
Correct |
230 ms |
37512 KB |
Output is correct |
22 |
Correct |
204 ms |
36208 KB |
Output is correct |
23 |
Correct |
256 ms |
40776 KB |
Output is correct |
24 |
Correct |
29 ms |
28536 KB |
Output is correct |
25 |
Correct |
52 ms |
30720 KB |
Output is correct |
26 |
Correct |
79 ms |
32052 KB |
Output is correct |
27 |
Correct |
104 ms |
50604 KB |
Output is correct |
28 |
Correct |
258 ms |
39668 KB |
Output is correct |
29 |
Correct |
283 ms |
40424 KB |
Output is correct |
30 |
Correct |
243 ms |
37596 KB |
Output is correct |
31 |
Correct |
209 ms |
36220 KB |
Output is correct |
32 |
Correct |
71 ms |
31868 KB |
Output is correct |
33 |
Correct |
241 ms |
39616 KB |
Output is correct |
34 |
Correct |
294 ms |
40412 KB |
Output is correct |
35 |
Correct |
218 ms |
37340 KB |
Output is correct |
36 |
Correct |
227 ms |
36372 KB |
Output is correct |
37 |
Correct |
252 ms |
40704 KB |
Output is correct |
38 |
Correct |
261 ms |
55616 KB |
Output is correct |