#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
//#define ll long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
/*
Algo: We have 2n nodes (i, 0/1 - is last node the largest)
Just for lmao
*/
typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef pair<ii, ii> iiii;
const ll N = 3e5 + 5;
const ll oo = 1e18 + 7, mod = 1e9 + 7;
mt19937 rng(1);
ll rnd(ll l, ll r){
ll temp = rng() % (r - l + 1);
return abs(temp) + l;
}
ll n, m, p, q;
vector<ll> Adj[N];
// cycles that node u can get
vector<ii> cyc[N][2];
//map<ii, ll> mp;
ii nxt[N][2];
ll mn_dist[N][2];
vector<ii> Adj2[N][2];
ii savee[N];
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
n = N, m = M, p = P;
for(ll i = 0; i < m; i++){
Adj[R[i][0]].pb(R[i][1]);
Adj[R[i][1]].pb(R[i][0]);
//mp[{min(R[i][0], R[i][1]), m}
}
for(ll i = 0; i < n; i++){
// last trail is the best in all trails from i -> need to choose second if there is one
if(Adj[i].size() >= 2) nxt[i][1] = {Adj[i][1], (Adj[Adj[i][1]][0] == i)};
else nxt[i][1] = {Adj[i][0], (Adj[Adj[i][0]][0] == i)};
nxt[i][0] = {Adj[i][0], (Adj[Adj[i][0]][0] == i)};
}
for(ll i = 0; i < n; i++){
for(ll j = 0; j < 2; j++){
Adj2[nxt[i][j].fi][nxt[i][j].se].pb({i, j});
}
}
// finding the cycle for (p, 0)
ii itr = {p, 0};
ll len = 0;
while((!len || itr != make_pair(p, 0LL)) && len <= 2 * n){
len++;
itr = nxt[itr.fi][itr.se];
}
if(len > 2 * n) len = oo;
for(ll i = 0; i < n; i++) mn_dist[i][0] = mn_dist[i][1] = oo;
mn_dist[p][0] = 0;
queue<ii> bfs;
bfs.push({p, 0});
while(!bfs.empty()){
ii u = bfs.front();
bfs.pop();
for(auto it : Adj2[u.fi][u.se]){
if(mn_dist[it.fi][it.se] != oo) continue;
mn_dist[it.fi][it.se] = mn_dist[u.fi][u.se] + 1;
bfs.push(it);
}
}
for(ll i = 0; i < n; i++){
for(ll j = 0; j < 2; j++){
if(mn_dist[i][j] == oo) continue;
cyc[i][j].pb({mn_dist[i][j], len});
}
}
itr = {p, 1};
len = 0;
while((!len || itr != make_pair(p, 1LL)) && len <= 2 * n){
len++;
itr = nxt[itr.fi][itr.se];
}
if(len > 2 * n) len = oo;
for(ll i = 0; i < n; i++) mn_dist[i][0] = mn_dist[i][1] = oo;
mn_dist[p][1] = 0;
//queue<ii> bfs;
bfs.push({p, 1});
//cout << "OKAY\n";
while(!bfs.empty()){
ii u = bfs.front();
bfs.pop();
for(auto it : Adj2[u.fi][u.se]){
if(mn_dist[it.fi][it.se] != oo) continue;
mn_dist[it.fi][it.se] = mn_dist[u.fi][u.se] + 1;
bfs.push(it);
}
}
for(ll i = 0; i < n; i++){
for(ll j = 0; j < 2; j++){
if(mn_dist[i][j] == oo) continue;
cyc[i][j].pb({mn_dist[i][j], len});
//cout << i << " " << j << " " << mn_dist[i][j] << " " << len << "\n";
}
}
// exit(0);
for(ll i = 0; i < n; i++) savee[i] = {Adj[i][0], (Adj[Adj[i][0]][0] == i)};
for(ll i = 0; i < Q; i++){
ll tol = 0;
G[i]--;
for(ll j = 0; j < n; j++){
ii temp = savee[j];
//cout << temp.fi << " " << temp.se << "\n";
//cout << j << " " << Adj[j][0] << "\n";
//continue;
bool ck = 0;
for(auto it : cyc[temp.fi][temp.se]){
ll diff = G[i] - it.fi;
if(diff >= 0 && !(diff % it.se)){
ck = 1;
break;
}
}
tol += ck;
}
//cout << i << " " << tol << "\n";
answer(tol);
//if(n >= 100000 && Q > 1) exit(0);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
35668 KB |
Output is correct |
2 |
Correct |
19 ms |
35796 KB |
Output is correct |
3 |
Correct |
20 ms |
35796 KB |
Output is correct |
4 |
Correct |
19 ms |
35572 KB |
Output is correct |
5 |
Correct |
21 ms |
35540 KB |
Output is correct |
6 |
Correct |
19 ms |
35796 KB |
Output is correct |
7 |
Correct |
19 ms |
35540 KB |
Output is correct |
8 |
Correct |
20 ms |
35796 KB |
Output is correct |
9 |
Correct |
21 ms |
36012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
35668 KB |
Output is correct |
2 |
Correct |
19 ms |
35796 KB |
Output is correct |
3 |
Correct |
20 ms |
35796 KB |
Output is correct |
4 |
Correct |
19 ms |
35572 KB |
Output is correct |
5 |
Correct |
21 ms |
35540 KB |
Output is correct |
6 |
Correct |
19 ms |
35796 KB |
Output is correct |
7 |
Correct |
19 ms |
35540 KB |
Output is correct |
8 |
Correct |
20 ms |
35796 KB |
Output is correct |
9 |
Correct |
21 ms |
36012 KB |
Output is correct |
10 |
Correct |
17 ms |
35576 KB |
Output is correct |
11 |
Correct |
35 ms |
39252 KB |
Output is correct |
12 |
Correct |
42 ms |
41944 KB |
Output is correct |
13 |
Correct |
84 ms |
57600 KB |
Output is correct |
14 |
Correct |
171 ms |
58728 KB |
Output is correct |
15 |
Correct |
188 ms |
66152 KB |
Output is correct |
16 |
Correct |
114 ms |
57052 KB |
Output is correct |
17 |
Correct |
133 ms |
54500 KB |
Output is correct |
18 |
Correct |
56 ms |
41916 KB |
Output is correct |
19 |
Correct |
166 ms |
58572 KB |
Output is correct |
20 |
Correct |
179 ms |
66160 KB |
Output is correct |
21 |
Correct |
158 ms |
57016 KB |
Output is correct |
22 |
Correct |
130 ms |
54276 KB |
Output is correct |
23 |
Correct |
149 ms |
60296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
35668 KB |
Output is correct |
2 |
Correct |
19 ms |
35796 KB |
Output is correct |
3 |
Correct |
20 ms |
35796 KB |
Output is correct |
4 |
Correct |
19 ms |
35572 KB |
Output is correct |
5 |
Correct |
21 ms |
35540 KB |
Output is correct |
6 |
Correct |
19 ms |
35796 KB |
Output is correct |
7 |
Correct |
19 ms |
35540 KB |
Output is correct |
8 |
Correct |
20 ms |
35796 KB |
Output is correct |
9 |
Correct |
21 ms |
36012 KB |
Output is correct |
10 |
Correct |
17 ms |
35576 KB |
Output is correct |
11 |
Correct |
35 ms |
39252 KB |
Output is correct |
12 |
Correct |
42 ms |
41944 KB |
Output is correct |
13 |
Correct |
84 ms |
57600 KB |
Output is correct |
14 |
Correct |
171 ms |
58728 KB |
Output is correct |
15 |
Correct |
188 ms |
66152 KB |
Output is correct |
16 |
Correct |
114 ms |
57052 KB |
Output is correct |
17 |
Correct |
133 ms |
54500 KB |
Output is correct |
18 |
Correct |
56 ms |
41916 KB |
Output is correct |
19 |
Correct |
166 ms |
58572 KB |
Output is correct |
20 |
Correct |
179 ms |
66160 KB |
Output is correct |
21 |
Correct |
158 ms |
57016 KB |
Output is correct |
22 |
Correct |
130 ms |
54276 KB |
Output is correct |
23 |
Correct |
149 ms |
60296 KB |
Output is correct |
24 |
Correct |
20 ms |
35540 KB |
Output is correct |
25 |
Correct |
185 ms |
39360 KB |
Output is correct |
26 |
Correct |
285 ms |
42104 KB |
Output is correct |
27 |
Correct |
2586 ms |
57616 KB |
Output is correct |
28 |
Correct |
2407 ms |
58800 KB |
Output is correct |
29 |
Execution timed out |
5095 ms |
67160 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |