#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define eb emplace_back
const int mxn = 3e5+5;
vi adj[mxn];
int n,q,F[mxn],deg[mxn],vis[mxn],dist[mxn],D[mxn],Cnt[mxn],ans[2002];
struct query{
int k,ind;
query(int kk, int i){
k=kk,ind=i;
}
bool operator < (const query &rhs)const{
return k<rhs.k;
}
};
vector<query>qq;
void add(int a, int b, int ind){
if(vis[a]==ind){
if(vis[b]!=ind||deg[b]==1)F[a]=b;
else F[a]=b+n;
}else if(F[a+n]==-1){
if(vis[b]!=ind||deg[b]==1)F[a+n]=b;
else F[a+n]=b+n;
}
}
void dfs(int u, int d){
dist[u]=d;
for(int v:adj[u]){
if(dist[v]==-1)dfs(v,d+1);
}
}
void run(int st){
for(int i=0; i<=2*n; i++){
dist[i]=-1;
vis[i]=0;
Cnt[i]=0;
}
dfs(st,0);
int cnt = 0;
for(int i=0; i<n; i++){
if(dist[i]!=-1){
D[cnt++]=dist[i];
}
}
sort(D,D+cnt);
//calculate cycle size
int c = 0;
int now = st;
while(!vis[now]){
c++;
vis[now]=1;
now = F[now];
}
if(now!=st)c=1000000009;
int ptr = 0;
for(int i=0; i<q; i++){
while(ptr<cnt&&D[ptr]<=qq[i].k){
Cnt[D[ptr]%c]++;
ptr++;
}
int md = qq[i].k%c;
if(md>2*n)continue;
ans[qq[i].ind]+=Cnt[md];
}
return;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
n=N,q=Q;
int m=M;
for(int i=0; i<q; i++){
qq.eb(G[i],i);
}
sort(qq.begin(),qq.end());
for(int i=0; i<m; i++){
int u = R[i][0], v = R[i][1];
deg[u]++,deg[v]++;
if(!vis[u])vis[u]=i+1;
if(!vis[v])vis[v]=i+1;
}
memset(F,-1,sizeof(F));
for(int i=0; i<m; i++){
int u = R[i][0], v = R[i][1];
add(u,v,i+1);
add(v,u,i+1);
}
for(int i=0; i<n; i++){
adj[F[i]].eb(i);
if(deg[i]>1)adj[F[i+n]].eb(i+n);
}
run(P);
if(deg[P]>1)run(P+n);
for(int i=0; i<q; i++)answer(ans[i]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8704 KB |
Output is correct |
2 |
Correct |
6 ms |
8704 KB |
Output is correct |
3 |
Correct |
6 ms |
8704 KB |
Output is correct |
4 |
Correct |
7 ms |
8576 KB |
Output is correct |
5 |
Correct |
6 ms |
8576 KB |
Output is correct |
6 |
Correct |
7 ms |
8832 KB |
Output is correct |
7 |
Correct |
6 ms |
8576 KB |
Output is correct |
8 |
Correct |
6 ms |
8832 KB |
Output is correct |
9 |
Correct |
8 ms |
8832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8704 KB |
Output is correct |
2 |
Correct |
6 ms |
8704 KB |
Output is correct |
3 |
Correct |
6 ms |
8704 KB |
Output is correct |
4 |
Correct |
7 ms |
8576 KB |
Output is correct |
5 |
Correct |
6 ms |
8576 KB |
Output is correct |
6 |
Correct |
7 ms |
8832 KB |
Output is correct |
7 |
Correct |
6 ms |
8576 KB |
Output is correct |
8 |
Correct |
6 ms |
8832 KB |
Output is correct |
9 |
Correct |
8 ms |
8832 KB |
Output is correct |
10 |
Correct |
6 ms |
8576 KB |
Output is correct |
11 |
Correct |
15 ms |
10624 KB |
Output is correct |
12 |
Correct |
28 ms |
11904 KB |
Output is correct |
13 |
Correct |
59 ms |
23544 KB |
Output is correct |
14 |
Correct |
86 ms |
19576 KB |
Output is correct |
15 |
Correct |
120 ms |
20344 KB |
Output is correct |
16 |
Correct |
93 ms |
17400 KB |
Output is correct |
17 |
Correct |
84 ms |
16504 KB |
Output is correct |
18 |
Correct |
28 ms |
11904 KB |
Output is correct |
19 |
Correct |
89 ms |
19596 KB |
Output is correct |
20 |
Correct |
120 ms |
20344 KB |
Output is correct |
21 |
Correct |
97 ms |
17400 KB |
Output is correct |
22 |
Correct |
87 ms |
16376 KB |
Output is correct |
23 |
Correct |
86 ms |
20472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8704 KB |
Output is correct |
2 |
Correct |
6 ms |
8704 KB |
Output is correct |
3 |
Correct |
6 ms |
8704 KB |
Output is correct |
4 |
Correct |
7 ms |
8576 KB |
Output is correct |
5 |
Correct |
6 ms |
8576 KB |
Output is correct |
6 |
Correct |
7 ms |
8832 KB |
Output is correct |
7 |
Correct |
6 ms |
8576 KB |
Output is correct |
8 |
Correct |
6 ms |
8832 KB |
Output is correct |
9 |
Correct |
8 ms |
8832 KB |
Output is correct |
10 |
Correct |
6 ms |
8576 KB |
Output is correct |
11 |
Correct |
15 ms |
10624 KB |
Output is correct |
12 |
Correct |
28 ms |
11904 KB |
Output is correct |
13 |
Correct |
59 ms |
23544 KB |
Output is correct |
14 |
Correct |
86 ms |
19576 KB |
Output is correct |
15 |
Correct |
120 ms |
20344 KB |
Output is correct |
16 |
Correct |
93 ms |
17400 KB |
Output is correct |
17 |
Correct |
84 ms |
16504 KB |
Output is correct |
18 |
Correct |
28 ms |
11904 KB |
Output is correct |
19 |
Correct |
89 ms |
19596 KB |
Output is correct |
20 |
Correct |
120 ms |
20344 KB |
Output is correct |
21 |
Correct |
97 ms |
17400 KB |
Output is correct |
22 |
Correct |
87 ms |
16376 KB |
Output is correct |
23 |
Correct |
86 ms |
20472 KB |
Output is correct |
24 |
Correct |
6 ms |
8704 KB |
Output is correct |
25 |
Correct |
15 ms |
10624 KB |
Output is correct |
26 |
Correct |
29 ms |
12024 KB |
Output is correct |
27 |
Correct |
61 ms |
23680 KB |
Output is correct |
28 |
Correct |
89 ms |
19576 KB |
Output is correct |
29 |
Correct |
124 ms |
20472 KB |
Output is correct |
30 |
Correct |
92 ms |
17600 KB |
Output is correct |
31 |
Correct |
91 ms |
16632 KB |
Output is correct |
32 |
Correct |
30 ms |
12032 KB |
Output is correct |
33 |
Correct |
86 ms |
19576 KB |
Output is correct |
34 |
Correct |
149 ms |
20344 KB |
Output is correct |
35 |
Correct |
94 ms |
17400 KB |
Output is correct |
36 |
Correct |
93 ms |
16504 KB |
Output is correct |
37 |
Correct |
86 ms |
20600 KB |
Output is correct |
38 |
Correct |
94 ms |
30072 KB |
Output is correct |