#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int NN=15e5;
// came in: 0 - best, 1 - not best
vector<pair<int,int>> in[NN][2];
int anss[NN+10];
int out[NN+10][2];
vector<int> cnt[2*NN+10];
bool vis[NN+10][2];
int dd[NN+10][2];
bool end_type(int x,int y)
{
return (x!=out[y][1]);
}
pair<int,int> go(pair<int,int> x)
{
int y=out[x.fi][x.se];
return {y,end_type(x.fi,y)};
}
void add_edge(int x,int y)
{
if(out[x][1]==-1)
out[x][1]=y;
else if(out[x][0]==-1)
out[x][0]=y;
return;
}
void reverse_edge(int x,int t,int y)
{
in[y][end_type(x,y)].push_back({x,t});
return;
}
int cycle(int P,int t,int N)
{
for(int i=0;i<N;i++)
vis[i][0]=vis[i][1]=false;
int ans=0;
pair<int,int> x={P,t};
for(;!vis[x.fi][x.se];ans++,x=go(x))
{
vis[x.fi][x.se]=true;
}
if(x==make_pair(P,t))
return ans;
return 10*N;
}
void paths(int P,int t,int N,int l)
{
for(int i=0;i<l;i++)
cnt[i].clear();
for(int i=0;i<N;i++)
vis[i][0]=vis[i][1]=false;
vis[P][t]=true;
dd[P][t]=0;
queue<pair<int,int>> qq;
qq.push({P,t});
while(!qq.empty())
{
auto v=qq.front();
qq.pop();
if(v.se==1)
cnt[dd[v.fi][v.se]%l].push_back(dd[v.fi][v.se]);
for(auto y:in[v.fi][v.se])
{
if(!vis[y.fi][y.se])
{
vis[y.fi][y.se]=true;
dd[y.fi][y.se]=dd[v.fi][v.se]+1;
qq.push(y);
}
}
}
return;
}
int cnt_paths(int id,int mx)
{
if(cnt[id].empty() || cnt[id][0]>mx)
return 0;
int bg=0,en=cnt[id].size()-1;
while(bg<en)
{
int mid=(bg+en+1)/2;
if(cnt[id][mid]<=mx)
bg=mid;
else
en=mid-1;
}
return bg+1;
}
void count_routes(int N,int M,int P,int R[][2],int Q,int G[])
{
for(int i=0;i<N;i++)
out[i][0]=out[i][1]=-1;
for(int i=0;i<M;i++)
{
add_edge(R[i][0],R[i][1]);
add_edge(R[i][1],R[i][0]);
}
for(int i=0;i<N;i++)
{
if(out[i][0]==-1)
out[i][0]=out[i][1];
reverse_edge(i,0,out[i][0]);
reverse_edge(i,1,out[i][1]);
}
for(int t:{0,1})
{
int l=cycle(P,t,N);
paths(P,t,N,l);
for(int i=0;i<Q;i++)
{
if(l==10*N && G[i]>l)
continue;
anss[i]+=cnt_paths(G[i]%l,G[i]);
}
}
for(int i=0;i<Q;i++)
answer(anss[i]);
return;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
141300 KB |
Output is correct |
2 |
Correct |
89 ms |
141300 KB |
Output is correct |
3 |
Correct |
83 ms |
141272 KB |
Output is correct |
4 |
Correct |
84 ms |
141144 KB |
Output is correct |
5 |
Correct |
84 ms |
141172 KB |
Output is correct |
6 |
Correct |
98 ms |
141232 KB |
Output is correct |
7 |
Correct |
86 ms |
141232 KB |
Output is correct |
8 |
Correct |
85 ms |
141200 KB |
Output is correct |
9 |
Correct |
86 ms |
141344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
141300 KB |
Output is correct |
2 |
Correct |
89 ms |
141300 KB |
Output is correct |
3 |
Correct |
83 ms |
141272 KB |
Output is correct |
4 |
Correct |
84 ms |
141144 KB |
Output is correct |
5 |
Correct |
84 ms |
141172 KB |
Output is correct |
6 |
Correct |
98 ms |
141232 KB |
Output is correct |
7 |
Correct |
86 ms |
141232 KB |
Output is correct |
8 |
Correct |
85 ms |
141200 KB |
Output is correct |
9 |
Correct |
86 ms |
141344 KB |
Output is correct |
10 |
Correct |
89 ms |
141224 KB |
Output is correct |
11 |
Correct |
104 ms |
142748 KB |
Output is correct |
12 |
Correct |
103 ms |
143772 KB |
Output is correct |
13 |
Correct |
131 ms |
151364 KB |
Output is correct |
14 |
Correct |
162 ms |
150072 KB |
Output is correct |
15 |
Correct |
190 ms |
151024 KB |
Output is correct |
16 |
Correct |
154 ms |
148240 KB |
Output is correct |
17 |
Correct |
150 ms |
147256 KB |
Output is correct |
18 |
Correct |
107 ms |
144396 KB |
Output is correct |
19 |
Correct |
195 ms |
150952 KB |
Output is correct |
20 |
Correct |
187 ms |
151732 KB |
Output is correct |
21 |
Correct |
163 ms |
149132 KB |
Output is correct |
22 |
Correct |
155 ms |
147888 KB |
Output is correct |
23 |
Correct |
164 ms |
151588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
141300 KB |
Output is correct |
2 |
Correct |
89 ms |
141300 KB |
Output is correct |
3 |
Correct |
83 ms |
141272 KB |
Output is correct |
4 |
Correct |
84 ms |
141144 KB |
Output is correct |
5 |
Correct |
84 ms |
141172 KB |
Output is correct |
6 |
Correct |
98 ms |
141232 KB |
Output is correct |
7 |
Correct |
86 ms |
141232 KB |
Output is correct |
8 |
Correct |
85 ms |
141200 KB |
Output is correct |
9 |
Correct |
86 ms |
141344 KB |
Output is correct |
10 |
Correct |
89 ms |
141224 KB |
Output is correct |
11 |
Correct |
104 ms |
142748 KB |
Output is correct |
12 |
Correct |
103 ms |
143772 KB |
Output is correct |
13 |
Correct |
131 ms |
151364 KB |
Output is correct |
14 |
Correct |
162 ms |
150072 KB |
Output is correct |
15 |
Correct |
190 ms |
151024 KB |
Output is correct |
16 |
Correct |
154 ms |
148240 KB |
Output is correct |
17 |
Correct |
150 ms |
147256 KB |
Output is correct |
18 |
Correct |
107 ms |
144396 KB |
Output is correct |
19 |
Correct |
195 ms |
150952 KB |
Output is correct |
20 |
Correct |
187 ms |
151732 KB |
Output is correct |
21 |
Correct |
163 ms |
149132 KB |
Output is correct |
22 |
Correct |
155 ms |
147888 KB |
Output is correct |
23 |
Correct |
164 ms |
151588 KB |
Output is correct |
24 |
Correct |
93 ms |
141152 KB |
Output is correct |
25 |
Correct |
94 ms |
143044 KB |
Output is correct |
26 |
Correct |
101 ms |
144324 KB |
Output is correct |
27 |
Correct |
133 ms |
152136 KB |
Output is correct |
28 |
Correct |
171 ms |
151000 KB |
Output is correct |
29 |
Correct |
191 ms |
151764 KB |
Output is correct |
30 |
Correct |
209 ms |
148996 KB |
Output is correct |
31 |
Correct |
155 ms |
148280 KB |
Output is correct |
32 |
Correct |
110 ms |
144416 KB |
Output is correct |
33 |
Correct |
166 ms |
150940 KB |
Output is correct |
34 |
Correct |
213 ms |
151748 KB |
Output is correct |
35 |
Correct |
194 ms |
149064 KB |
Output is correct |
36 |
Correct |
167 ms |
148168 KB |
Output is correct |
37 |
Correct |
172 ms |
151684 KB |
Output is correct |
38 |
Correct |
173 ms |
158856 KB |
Output is correct |