#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define f first
#define s second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
void count_routes(int n, int m, int p, int r[][2], int q, int g[]) {
ios::sync_with_stdio(false); cin.tie(0);
vector<pii> mx(n,{-1,-1});
auto chmax = [&](int x, int v) {
if (mx[x].f==-1) {mx[x].f=v;}
else if (mx[x].s==-1) {mx[x].s=v;}
};
F0R(i,m) {
chmax(r[i][0],r[i][1]); chmax(r[i][1],r[i][0]);
}
vector<int> dist(2*n,INT_MAX); dist[p] = 0; // to P
vector<int> dist2(2*n,INT_MAX); dist2[n+p] = 0; // to P+N
vector<bool> vis(2*n,false), vis2(2*n,false);
vis[p] = true; vis2[n+p] = true;
int cyc[2] = {0,0};
auto find = [&](int cur) {
int ret;
if (cur >= n) {ret = (mx[cur-n].s==-1 ? mx[cur-n].f : mx[cur-n].s);}
else {ret = mx[cur].f;}
return (ret+n*(mx[ret].f==(cur%n)));
};
auto dfs = [&](auto& dfs, int cur, vector<int>& d, vector<bool>& v)->void {
int nxt = find(cur);
v[cur] = true;
if (!v[nxt]) {dfs(dfs,nxt,d,v); d[cur] = d[nxt]+1;}
else if (d[nxt]!=INT_MAX) {d[cur] = d[nxt]+1;}
else {d[cur] = INT_MIN;}
};
//cout<<find(5);
//dfs(dfs,1,dist,vis);
//if (mx[p].f!=-1) {dfs(dfs,p+n,p+n,1,dist,vis);}
F0R(i,2*n) {if (!vis[i]&&mx[i%n].f!=-1) {dfs(dfs,i,dist,vis);}}
//if (mx[p].f!=-1) {dfs(dfs,n,n,1,dist2,vis2);}
//dfs(dfs,5,dist2,vis2);
F0R(i,2*n) {if (!vis2[i]&&mx[i%n].f!=-1) {dfs(dfs,i,dist2,vis2);}}
auto cycle = [&](auto& cycle, int cur, int src, int depth)->void {
int nxt = find(cur); vis[cur] = true;
if (!vis[nxt]) {cycle(cycle,nxt,src,depth+1);}
else {if (nxt==src) {cyc[src/n] = depth+1;}}
};
if (mx[p].f!=-1) {
vis.assign(2*n,false); cycle(cycle,p,p,0);
vis.assign(2*n,false); cycle(cycle,p+n,p+n,0);
}
F0R(j,q) {
int ans = 0;
int k = g[j];
F0R(i,n) {
ans += (dist[i]==k) + (dist2[i]==k);
if (dist[i]>=0 && dist[i]!=INT_MAX && cyc[0] && (k-dist[i]>0)) {ans += !((k-dist[i])%cyc[0]);}
if (dist2[i]>=0 && dist2[i]!=INT_MAX && cyc[1] && (k-dist2[i]>0)) {ans += !((k-dist2[i])%cyc[1]);}
} answer(ans);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
416 KB |
Output is correct |
11 |
Correct |
4 ms |
3420 KB |
Output is correct |
12 |
Correct |
9 ms |
4064 KB |
Output is correct |
13 |
Correct |
18 ms |
10072 KB |
Output is correct |
14 |
Correct |
30 ms |
7560 KB |
Output is correct |
15 |
Correct |
27 ms |
7484 KB |
Output is correct |
16 |
Correct |
24 ms |
6492 KB |
Output is correct |
17 |
Correct |
21 ms |
6068 KB |
Output is correct |
18 |
Correct |
10 ms |
3976 KB |
Output is correct |
19 |
Correct |
28 ms |
7380 KB |
Output is correct |
20 |
Correct |
29 ms |
7504 KB |
Output is correct |
21 |
Correct |
32 ms |
6220 KB |
Output is correct |
22 |
Correct |
25 ms |
5980 KB |
Output is correct |
23 |
Correct |
29 ms |
8136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
416 KB |
Output is correct |
11 |
Correct |
4 ms |
3420 KB |
Output is correct |
12 |
Correct |
9 ms |
4064 KB |
Output is correct |
13 |
Correct |
18 ms |
10072 KB |
Output is correct |
14 |
Correct |
30 ms |
7560 KB |
Output is correct |
15 |
Correct |
27 ms |
7484 KB |
Output is correct |
16 |
Correct |
24 ms |
6492 KB |
Output is correct |
17 |
Correct |
21 ms |
6068 KB |
Output is correct |
18 |
Correct |
10 ms |
3976 KB |
Output is correct |
19 |
Correct |
28 ms |
7380 KB |
Output is correct |
20 |
Correct |
29 ms |
7504 KB |
Output is correct |
21 |
Correct |
32 ms |
6220 KB |
Output is correct |
22 |
Correct |
25 ms |
5980 KB |
Output is correct |
23 |
Correct |
29 ms |
8136 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
59 ms |
3616 KB |
Output is correct |
26 |
Correct |
93 ms |
4440 KB |
Output is correct |
27 |
Correct |
684 ms |
10076 KB |
Output is correct |
28 |
Correct |
729 ms |
7512 KB |
Output is correct |
29 |
Correct |
660 ms |
7508 KB |
Output is correct |
30 |
Correct |
383 ms |
6488 KB |
Output is correct |
31 |
Correct |
373 ms |
5976 KB |
Output is correct |
32 |
Correct |
101 ms |
4184 KB |
Output is correct |
33 |
Correct |
765 ms |
7600 KB |
Output is correct |
34 |
Correct |
603 ms |
7720 KB |
Output is correct |
35 |
Correct |
385 ms |
6236 KB |
Output is correct |
36 |
Correct |
773 ms |
5948 KB |
Output is correct |
37 |
Correct |
526 ms |
8184 KB |
Output is correct |
38 |
Correct |
1287 ms |
12036 KB |
Output is correct |