#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn = 150005;
int f[maxn],g[maxn][2],cnt;
int nxt[2*maxn],num[2*maxn],dist[2*maxn],total[2*maxn];
bool vis[2*maxn];
vector<int> edge[2*maxn];
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
for(int i=0;i<M;i++){
int u=R[i][0],v=R[i][1];
int a=(f[u]!=0),b=(f[v]!=0);
if(!g[u][a]) g[u][a]=++cnt;
if(!g[v][b]) g[v][b]=++cnt;
if(!f[u]) f[u]=g[v][b];
else if(f[u]!=-1){
nxt[g[u][a]]=f[u];
nxt[g[u][0]]=g[v][b];
f[u]=-1;
}
if(!f[v]) f[v]=g[u][a];
else if(f[v]!=-1){
nxt[g[v][b]]=f[v];
nxt[g[v][0]]=g[u][a];
f[v]=-1;
}
}
for(int v=0;v<N;v++){
if(f[v]>0) nxt[g[v][0]]=f[v],num[g[v][0]]++;
else if(f[v]==-1) num[g[v][1]]++;
//for(int j=0;j<2;j++) if(g[v][j]) cout << v << ' ' << j << ' ' << g[v][j] << ' ' << nxt[g[v][j]] << '\n';
}
auto cal = [&](int x){
if(!x) return vector<int>(Q,0);
for(int i=0;i<=cnt;i++){
vis[i]=false;
edge[i].clear();
total[i]=0;
}
int u=x;
vector<int> cycle;
while(!vis[u]){
cycle.push_back(u);
vis[u]=true,u=nxt[u];
}
for(int i=1;i<=cnt;i++) if(!vis[i]) edge[nxt[i]].push_back(i);
int sz=(int)cycle.size();
function<void(int)> dfs = [&](int v){
total[dist[v]]+=num[v];
for(int k:edge[v]){
dist[k]=dist[v]+1;
dfs(k);
}
};
dist[x]=0;dfs(x);
if(u==x){
for(int i=1;i<(int)cycle.size();i++){
dist[cycle[i]]=sz-i;
dfs(cycle[i]);
}
for(int i=sz;i<=cnt;i++) total[i]+=total[i-sz];
}
vector<int> res(Q);
for(int i=0;i<Q;i++){
if(G[i]<=cnt) res[i]=total[G[i]];
else if(u==x){
int d=G[i]%sz;
d=((cnt-d)/sz)*sz+d;
res[i]=total[d];
}
}
return res;
};
vector<int> a=cal(g[P][0]),b=cal(g[P][1]);
for(int i=0;i<Q;i++) answer(a[i]+b[i]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7764 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
2 ms |
8924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7764 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
2 ms |
8924 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
7 ms |
9560 KB |
Output is correct |
12 |
Correct |
16 ms |
11356 KB |
Output is correct |
13 |
Correct |
20 ms |
14688 KB |
Output is correct |
14 |
Correct |
41 ms |
19536 KB |
Output is correct |
15 |
Correct |
47 ms |
20052 KB |
Output is correct |
16 |
Correct |
39 ms |
17748 KB |
Output is correct |
17 |
Correct |
35 ms |
16728 KB |
Output is correct |
18 |
Correct |
12 ms |
12380 KB |
Output is correct |
19 |
Correct |
38 ms |
20064 KB |
Output is correct |
20 |
Correct |
44 ms |
20264 KB |
Output is correct |
21 |
Correct |
37 ms |
17748 KB |
Output is correct |
22 |
Correct |
37 ms |
16732 KB |
Output is correct |
23 |
Correct |
37 ms |
20904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7764 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
2 ms |
8924 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
7 ms |
9560 KB |
Output is correct |
12 |
Correct |
16 ms |
11356 KB |
Output is correct |
13 |
Correct |
20 ms |
14688 KB |
Output is correct |
14 |
Correct |
41 ms |
19536 KB |
Output is correct |
15 |
Correct |
47 ms |
20052 KB |
Output is correct |
16 |
Correct |
39 ms |
17748 KB |
Output is correct |
17 |
Correct |
35 ms |
16728 KB |
Output is correct |
18 |
Correct |
12 ms |
12380 KB |
Output is correct |
19 |
Correct |
38 ms |
20064 KB |
Output is correct |
20 |
Correct |
44 ms |
20264 KB |
Output is correct |
21 |
Correct |
37 ms |
17748 KB |
Output is correct |
22 |
Correct |
37 ms |
16732 KB |
Output is correct |
23 |
Correct |
37 ms |
20904 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
7 ms |
9564 KB |
Output is correct |
26 |
Correct |
12 ms |
12380 KB |
Output is correct |
27 |
Correct |
18 ms |
15076 KB |
Output is correct |
28 |
Correct |
43 ms |
20052 KB |
Output is correct |
29 |
Correct |
45 ms |
20312 KB |
Output is correct |
30 |
Correct |
35 ms |
17756 KB |
Output is correct |
31 |
Correct |
35 ms |
16724 KB |
Output is correct |
32 |
Correct |
14 ms |
12380 KB |
Output is correct |
33 |
Correct |
40 ms |
20052 KB |
Output is correct |
34 |
Correct |
44 ms |
20260 KB |
Output is correct |
35 |
Correct |
38 ms |
17744 KB |
Output is correct |
36 |
Correct |
37 ms |
17020 KB |
Output is correct |
37 |
Correct |
40 ms |
20820 KB |
Output is correct |
38 |
Correct |
60 ms |
27260 KB |
Output is correct |