#include "garden.h"
#include <bits/stdc++.h>
#include "gardenlib.h"
using namespace std;
#define sz(a) (int)(a.size())
#define pb push_back
#define se second
typedef vector<vector<int>> vii;
typedef pair<int,int> pii;
const int MAXN = 4e5+5;
int nxt[MAXN][31];
vector<vector<pii>> g1(MAXN);
vii g2(MAXN);
void count_routes(int n, int m, int p, int r[][2], int q, int g[])
{
for(int i=0;i<m;i++){
g1[r[i][0]].pb({i,r[i][1]});
g1[r[i][1]].pb({i,r[i][0]});
}
for(int i=0;i<n;i++)sort(g1[i].begin(),g1[i].end());
for(int i=0;i<n;i++){
int to = g1[i][0].se;
if(g1[to][0].se != i){
nxt[i][0] = to; //not beautiful
g2[to].pb(i);
}
else {
nxt[i][0] = to+n;
g2[to+n].pb(i);
}
to = (sz(g1[i]) > 1)?g1[i][1].se:g1[i][0].se;
if(g1[to][0].se != i){
nxt[i+n][0] = to; //not beautiful
g2[to].pb(i+n);
}
else {
nxt[i+n][0] = to+n;
g2[to+n].pb(i+n);
}
}
vector<int>cycle(2*n),vis(2*n);
int cur = 0;
while(!vis[cur]){
vis[cur] = 1;
cur = nxt[cur][0];
}
int len = 0;
while(!cycle[cur]){
cycle[cur] = 1;
len++;
cur = nxt[cur][0];
}
vii d(2,vector<int>(2*n,-1));
queue<int>qq;
qq.push(p);
d[0][p] = 0;
vis.assign(2*n,0);
vis[p] = 1;
while(!qq.empty()){
int v = qq.front();
qq.pop();
for(int x:g2[v]){
if(vis[x])continue;
vis[x] =1;
d[0][x] = d[0][v]+1;
qq.push(x);
}
}
d[1][p+n] = 0;
qq.push(p+n);
vis.assign(2*n,0);
vis[p+n] = 1;
while(!qq.empty()){
int v = qq.front();
qq.pop();
for(int x:g2[v]){
if(vis[x])continue;
vis[x] =1;
d[1][x] = d[1][v]+1;
qq.push(x);
}
}
for(int i=0;i<q;i++){
int ans = 0;
for(int j=0;j<n;j++){
bool ok=0;
if(d[0][j] != -1 && ((cycle[p] && g[i] >= d[0][j] && (g[i]-d[0][j])%len == 0) || d[0][j] == g[i]))ok = 1;
if(d[1][j] != -1 && ((cycle[p+n] && g[i] >= d[1][j] && (g[i]-d[0][j])%len == 0) || d[1][j] == g[i]))ok = 1;
ans+=ok;
}
answer(ans);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
19404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
19404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
19404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |