#include<bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
const int MX = 300005;
const int INF = 1e9+1;
int edge[MX][2];
pii nxt[MX][2];
vector<pii> rev[MX][2];
int dist[2][MX][2];
vector<int> gg0,gg1;
vector<pii> queries;
int ans[MX];
int cnt0[MX],cnt1[MX];
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
P++;
for(int i = 0; i < M; i ++){
int u = R[i][0]+1;
int v = R[i][1]+1;
if(!edge[u][0]) edge[u][0] = v;
else if(!edge[u][1]) edge[u][1] = v;
swap(u,v);
if(!edge[u][0]) edge[u][0] = v;
else if(!edge[u][1]) edge[u][1] = v;
}
for(int i = 1; i <= N; i ++){
if(!edge[i][1]) edge[i][1] = edge[i][0];
if(edge[edge[i][0]][0] == i){
nxt[i][1] = {edge[i][0],0};
}
else nxt[i][1] = {edge[i][0],1};
if(edge[edge[i][1]][0] == i){
nxt[i][0] = {edge[i][1],0};
}
else nxt[i][0] = {edge[i][1],1};
}
for(int i = 1; i <= N; i ++){
rev[nxt[i][0].F][nxt[i][0].S].pb({i,0});
rev[nxt[i][1].F][nxt[i][1].S].pb({i,1});
dist[0][i][0] = INF;
dist[0][i][1] = INF;
dist[1][i][0] = INF;
dist[1][i][1] = INF;
}
queue<pii> q;
q.push({P,0});
dist[0][P][0] = 0;
while(!q.empty()){
pii u = q.front();
q.pop();
for(auto v:rev[u.F][u.S]){
if(dist[0][v.F][v.S] > 1+dist[0][u.F][u.S]){
dist[0][v.F][v.S] = 1+dist[0][u.F][u.S];
q.push(v);
}
}
}
dist[1][P][1] = 0;
q.push({P,1});
while(!q.empty()){
pii u = q.front();
q.pop();
for(auto v:rev[u.F][u.S]){
if(dist[1][v.F][v.S] > 1+dist[1][u.F][u.S]){
dist[1][v.F][v.S] = 1+dist[1][u.F][u.S];
q.push(v);
}
}
}
int cyc0 = 0,cyc1 = 0;
pii cur = {P,0};
while(cyc0 <= 2*N){
cur = nxt[cur.F][cur.S];
cyc0++;
if(cur.F == P and cur.S == 0) break;
}
if(cyc0 > 2*N) cyc0 = INF;
cur = {P,1};
while(cyc1 <= 2*N){
cur = nxt[cur.F][cur.S];
cyc1++;
if(cur.F == P and cur.S == 1) break;
}
if(cyc1 > 2*N) cyc1 = INF;
for(int i = 1; i <= N; i ++){
gg0.pb(dist[0][i][1]);
gg1.pb(dist[1][i][1]);
}
sort(gg0.begin(),gg0.end());
sort(gg1.begin(),gg1.end());
for(int i = 0; i < Q; i ++){
queries.pb({G[i],i});
}
sort(queries.begin(),queries.end());
int cur0 = 0,cur1 = 0;
for(int i = 0; i < Q; i ++){
while(cur0 < N and gg0[cur0] <= queries[i].F){
cnt0[gg0[cur0]%cyc0]++;
cur0++;
}
while(cur1 < N and gg1[cur1] <= queries[i].F){
cnt1[gg1[cur1]%cyc1]++;
cur1++;
}
if(queries[i].F%cyc0 <= MX) ans[queries[i].S] += cnt0[queries[i].F%cyc0];
if(queries[i].F%cyc1 <= MX) ans[queries[i].S] += cnt1[queries[i].F%cyc1];
}
for(int i = 0; i < Q; i ++) answer(ans[i]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14540 KB |
Output is correct |
2 |
Correct |
12 ms |
14540 KB |
Output is correct |
3 |
Correct |
11 ms |
14548 KB |
Output is correct |
4 |
Correct |
12 ms |
14448 KB |
Output is correct |
5 |
Correct |
10 ms |
14408 KB |
Output is correct |
6 |
Correct |
12 ms |
14540 KB |
Output is correct |
7 |
Correct |
13 ms |
14412 KB |
Output is correct |
8 |
Correct |
13 ms |
14540 KB |
Output is correct |
9 |
Correct |
12 ms |
14668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14540 KB |
Output is correct |
2 |
Correct |
12 ms |
14540 KB |
Output is correct |
3 |
Correct |
11 ms |
14548 KB |
Output is correct |
4 |
Correct |
12 ms |
14448 KB |
Output is correct |
5 |
Correct |
10 ms |
14408 KB |
Output is correct |
6 |
Correct |
12 ms |
14540 KB |
Output is correct |
7 |
Correct |
13 ms |
14412 KB |
Output is correct |
8 |
Correct |
13 ms |
14540 KB |
Output is correct |
9 |
Correct |
12 ms |
14668 KB |
Output is correct |
10 |
Correct |
11 ms |
14472 KB |
Output is correct |
11 |
Correct |
22 ms |
16992 KB |
Output is correct |
12 |
Correct |
41 ms |
18872 KB |
Output is correct |
13 |
Correct |
60 ms |
25952 KB |
Output is correct |
14 |
Correct |
103 ms |
28452 KB |
Output is correct |
15 |
Correct |
132 ms |
28708 KB |
Output is correct |
16 |
Correct |
127 ms |
24416 KB |
Output is correct |
17 |
Correct |
90 ms |
23348 KB |
Output is correct |
18 |
Correct |
34 ms |
18896 KB |
Output is correct |
19 |
Correct |
99 ms |
29480 KB |
Output is correct |
20 |
Correct |
142 ms |
29788 KB |
Output is correct |
21 |
Correct |
102 ms |
25368 KB |
Output is correct |
22 |
Correct |
91 ms |
24148 KB |
Output is correct |
23 |
Correct |
102 ms |
30652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14540 KB |
Output is correct |
2 |
Correct |
12 ms |
14540 KB |
Output is correct |
3 |
Correct |
11 ms |
14548 KB |
Output is correct |
4 |
Correct |
12 ms |
14448 KB |
Output is correct |
5 |
Correct |
10 ms |
14408 KB |
Output is correct |
6 |
Correct |
12 ms |
14540 KB |
Output is correct |
7 |
Correct |
13 ms |
14412 KB |
Output is correct |
8 |
Correct |
13 ms |
14540 KB |
Output is correct |
9 |
Correct |
12 ms |
14668 KB |
Output is correct |
10 |
Correct |
11 ms |
14472 KB |
Output is correct |
11 |
Correct |
22 ms |
16992 KB |
Output is correct |
12 |
Correct |
41 ms |
18872 KB |
Output is correct |
13 |
Correct |
60 ms |
25952 KB |
Output is correct |
14 |
Correct |
103 ms |
28452 KB |
Output is correct |
15 |
Correct |
132 ms |
28708 KB |
Output is correct |
16 |
Correct |
127 ms |
24416 KB |
Output is correct |
17 |
Correct |
90 ms |
23348 KB |
Output is correct |
18 |
Correct |
34 ms |
18896 KB |
Output is correct |
19 |
Correct |
99 ms |
29480 KB |
Output is correct |
20 |
Correct |
142 ms |
29788 KB |
Output is correct |
21 |
Correct |
102 ms |
25368 KB |
Output is correct |
22 |
Correct |
91 ms |
24148 KB |
Output is correct |
23 |
Correct |
102 ms |
30652 KB |
Output is correct |
24 |
Correct |
10 ms |
14420 KB |
Output is correct |
25 |
Correct |
20 ms |
17112 KB |
Output is correct |
26 |
Correct |
32 ms |
18908 KB |
Output is correct |
27 |
Correct |
69 ms |
26260 KB |
Output is correct |
28 |
Correct |
93 ms |
29496 KB |
Output is correct |
29 |
Correct |
133 ms |
29844 KB |
Output is correct |
30 |
Correct |
120 ms |
25380 KB |
Output is correct |
31 |
Correct |
104 ms |
24304 KB |
Output is correct |
32 |
Correct |
41 ms |
18976 KB |
Output is correct |
33 |
Correct |
96 ms |
29432 KB |
Output is correct |
34 |
Correct |
127 ms |
29764 KB |
Output is correct |
35 |
Correct |
111 ms |
25396 KB |
Output is correct |
36 |
Correct |
91 ms |
24280 KB |
Output is correct |
37 |
Correct |
98 ms |
30640 KB |
Output is correct |
38 |
Correct |
116 ms |
35200 KB |
Output is correct |