#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
int mx[150001],mx2[150001],nxt[300001],ch[300001],id,cycle[300001],d[300001],res,idx[300001],pos[300001];
vector <int> ke[300001],ve[300001],tmp;
void dfs(int u){
for (int v:ke[u]){
d[v]=d[u]+1;
dfs(v);
}
}
void dfs2(int u, int val){
if (u%2==0&&d[u]==val){
ch[u]=1;
return;
}
for (int v:ke[u])
dfs2(v,val);
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
memset(mx,-1,sizeof(mx));
memset(mx2,-1,sizeof(mx2));
reverse(R,R+M);
for (int i=0;i<M;i++){
int u=R[i][0],v=R[i][1];
if (mx[u]<i){
mx2[u]=mx[u];
mx[u]=i;
}
else if (mx2[u]<i)
mx2[u]=i;
if (mx[v]<i){
mx2[v]=mx[v];
mx[v]=i;
}
else if (mx2[v]<i)
mx2[v]=i;
}
for (int i=0;i<N;i++){
int v=R[mx[i]][0]^R[mx[i]][1]^i;
nxt[i*2]=v*2+1-(mx[i]!=mx[v]||mx2[v]==-1);
if (mx2[i]==-1)
continue;
v=R[mx2[i]][0]^R[mx2[i]][1]^i;
nxt[i*2+1]=v*2+1-(mx2[i]!=mx[v]||mx2[v]==-1);
}
for (int i=0;i<N*2;i++){
if (!ch[i]){
tmp.clear();
tmp.push_back(i);
int u=i;
while (true){
u=nxt[u];
if (ch[u]>1){
for (int j:tmp){
idx[j]=idx[u];
pos[j]=pos[u];
ch[j]=2;
}
break;
}
if (ch[u]==1){
int p=0;
for (int j=0;j<tmp.size();j++)
if (tmp[j]==u)
p=j;
for (int j=p;j<tmp.size();j++){
ve[id].push_back(tmp[j]);
cycle[tmp[j]]=1;
pos[tmp[j]]=j-p;
}
for (int j:tmp){
idx[j]=id;
ch[j]=2;
}
id++;
break;
}
tmp.push_back(u);
ch[u]=1;
}
}
}
for (int i=0;i<N*2;i++)
if (!cycle[i])
ke[nxt[i]].push_back(i);
for (int i=0;i<id;i++)
for (int j:ve[i])
dfs(j);
memset(ch,0,sizeof(ch));
for (int i=0;i<Q;i++){
res=0;
dfs2(P*2,d[P*2]+G[i]);
dfs2(P*2+1,d[P*2+1]+G[i]);
for (int j=0;j<N*2;j+=2){
if (!ch[j]&&d[j]<G[i]){
int v=ve[idx[j]][(pos[j]+G[i]-d[j])%ve[idx[j]].size()];
ch[j]=(v==P*2||v==P*2+1);
}
res+=ch[j];
ch[j]=0;
}
answer(res);
}
}
Compilation message
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:65:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int j=0;j<tmp.size();j++)
| ~^~~~~~~~~~~
garden.cpp:68:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int j=p;j<tmp.size();j++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23132 KB |
Output is correct |
3 |
Correct |
4 ms |
23132 KB |
Output is correct |
4 |
Correct |
4 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
4 ms |
23132 KB |
Output is correct |
7 |
Correct |
4 ms |
23164 KB |
Output is correct |
8 |
Correct |
4 ms |
23132 KB |
Output is correct |
9 |
Correct |
5 ms |
23132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23132 KB |
Output is correct |
3 |
Correct |
4 ms |
23132 KB |
Output is correct |
4 |
Correct |
4 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
4 ms |
23132 KB |
Output is correct |
7 |
Correct |
4 ms |
23164 KB |
Output is correct |
8 |
Correct |
4 ms |
23132 KB |
Output is correct |
9 |
Correct |
5 ms |
23132 KB |
Output is correct |
10 |
Correct |
4 ms |
23132 KB |
Output is correct |
11 |
Correct |
9 ms |
23432 KB |
Output is correct |
12 |
Correct |
18 ms |
24412 KB |
Output is correct |
13 |
Correct |
21 ms |
27344 KB |
Output is correct |
14 |
Correct |
58 ms |
29672 KB |
Output is correct |
15 |
Correct |
66 ms |
29900 KB |
Output is correct |
16 |
Correct |
48 ms |
28528 KB |
Output is correct |
17 |
Correct |
42 ms |
27988 KB |
Output is correct |
18 |
Correct |
22 ms |
24668 KB |
Output is correct |
19 |
Correct |
53 ms |
29692 KB |
Output is correct |
20 |
Correct |
55 ms |
29956 KB |
Output is correct |
21 |
Correct |
45 ms |
28580 KB |
Output is correct |
22 |
Correct |
43 ms |
27988 KB |
Output is correct |
23 |
Correct |
53 ms |
29900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23132 KB |
Output is correct |
3 |
Correct |
4 ms |
23132 KB |
Output is correct |
4 |
Correct |
4 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
4 ms |
23132 KB |
Output is correct |
7 |
Correct |
4 ms |
23164 KB |
Output is correct |
8 |
Correct |
4 ms |
23132 KB |
Output is correct |
9 |
Correct |
5 ms |
23132 KB |
Output is correct |
10 |
Correct |
4 ms |
23132 KB |
Output is correct |
11 |
Correct |
9 ms |
23432 KB |
Output is correct |
12 |
Correct |
18 ms |
24412 KB |
Output is correct |
13 |
Correct |
21 ms |
27344 KB |
Output is correct |
14 |
Correct |
58 ms |
29672 KB |
Output is correct |
15 |
Correct |
66 ms |
29900 KB |
Output is correct |
16 |
Correct |
48 ms |
28528 KB |
Output is correct |
17 |
Correct |
42 ms |
27988 KB |
Output is correct |
18 |
Correct |
22 ms |
24668 KB |
Output is correct |
19 |
Correct |
53 ms |
29692 KB |
Output is correct |
20 |
Correct |
55 ms |
29956 KB |
Output is correct |
21 |
Correct |
45 ms |
28580 KB |
Output is correct |
22 |
Correct |
43 ms |
27988 KB |
Output is correct |
23 |
Correct |
53 ms |
29900 KB |
Output is correct |
24 |
Correct |
6 ms |
23132 KB |
Output is correct |
25 |
Correct |
249 ms |
23644 KB |
Output is correct |
26 |
Correct |
437 ms |
24460 KB |
Output is correct |
27 |
Correct |
1038 ms |
27392 KB |
Output is correct |
28 |
Correct |
2219 ms |
30108 KB |
Output is correct |
29 |
Correct |
2302 ms |
30184 KB |
Output is correct |
30 |
Correct |
1332 ms |
28568 KB |
Output is correct |
31 |
Correct |
1562 ms |
28108 KB |
Output is correct |
32 |
Correct |
538 ms |
24552 KB |
Output is correct |
33 |
Correct |
2214 ms |
29868 KB |
Output is correct |
34 |
Correct |
2374 ms |
29960 KB |
Output is correct |
35 |
Correct |
1552 ms |
28596 KB |
Output is correct |
36 |
Correct |
4035 ms |
28132 KB |
Output is correct |
37 |
Correct |
2109 ms |
30156 KB |
Output is correct |
38 |
Correct |
1786 ms |
27716 KB |
Output is correct |