#include <bits/stdc++.h>
using namespace std;
#include "garden.h"
#include "gardenlib.h"
const int LIM = 1.5e5, INF = 2e9;
int n, cnt[LIM], c, d[2][LIM*2], p, r[2] = {INF, INF};
vector<int> g[LIM*2];
void dfs(int u, int dist){
if(u == p+c*n && dist) return void(r[c] = dist);
if(u < n) ++d[c][dist];
for(int v : g[u]) dfs(v, dist + 1);
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
n = N, p = P;
for(int i=0; i<M; ++i){
for(int j=0; j<2; ++j){
int u = R[i][j], v = R[i][!j];
if(cnt[u] == 0) g[v + (!cnt[v])*n].push_back(u);
if(cnt[u] == 1) g[v + (!cnt[v])*n].push_back(u+n);
}
for(int j=0; j<2; ++j) ++cnt[R[i][j]];
}
for(int u=0; u<n; ++u) if(cnt[u] < 2){
for(int v : g[u+n]) g[u].push_back(v);
g[u+n].clear();
}
for(c=0; c<2; ++c) dfs(p+c*n, 0);
for(int j=0; j<2; ++j){
for(int i=r[j]; i<n+n; ++i){
d[j][i] += d[j][i-r[j]];
}
}
n = 2*n - 1;
int l[2];
for(int j=0; j<2; ++j) if(r[j] < INF){
l[j] = r[j] * (n / r[j]);
while(l[j] <= n) l[j] += r[j];
}
for(int i=0; i<Q; ++i){
int res = 0;
for(int j=0; j<2; ++j){
if(G[i] >= n+n && r[j] < 2*n){
int x = l[j] + (G[i] % r[j]);
while(x > n) x -= r[j];
res += d[j][x];
}
if(G[i] < n+n){
res += d[j][G[i]];
}
}
answer(res);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7372 KB |
Output is correct |
2 |
Correct |
7 ms |
7372 KB |
Output is correct |
3 |
Correct |
7 ms |
7372 KB |
Output is correct |
4 |
Incorrect |
6 ms |
7372 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7372 KB |
Output is correct |
2 |
Correct |
7 ms |
7372 KB |
Output is correct |
3 |
Correct |
7 ms |
7372 KB |
Output is correct |
4 |
Incorrect |
6 ms |
7372 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7372 KB |
Output is correct |
2 |
Correct |
7 ms |
7372 KB |
Output is correct |
3 |
Correct |
7 ms |
7372 KB |
Output is correct |
4 |
Incorrect |
6 ms |
7372 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |