#include "garden.h"
#include "gardenlib.h"
using namespace std;
#include <vector>
#define INF 999999999
struct node{
int p=-1;//0
bool g1=false;//1
bool g2=false;//1
bool l1=false;//1
bool l2=false;//1
bool f=false;//valid start?
vector<int> v;//0
int z1=INF;
int z2=INF;
node(){}
};
int e,n;
vector<node> v;
void count_routes(int N, int m, int E, int w[][2], int Q, int G[]){
n=N;e=E;
//0 input trails
v.resize(n*2,node());
int a,b;
for (int i=0;i<m;i++){
a=w[i][0];
b=w[i][1];
if (v[b].p==-1){b+=n;}
if (v[a].p==-1){
v[a].p=b;
v[b].v.push_back(a);
a+=n;
}else if (v[a+n].p==-1){
v[a+n].p=b;
v[b].v.push_back(a+n);
}
b%=n;
if (v[b].p==-1){
v[b].p=a;
v[a].v.push_back(b);
}else if (v[b+n].p==-1){
v[b+n].p=a;
v[a].v.push_back(b+n);
}
}
for (int i=0;i<n;i++){
if (v[i+n].p==-1){
v[i+n].p=v[i].p;
}
}
//1 find loops
int ra,rb;
a=e;
ra=0;
while (!v[a].g1){
v[a].g1=true;
ra++;
a=v[a].p;
}
if (a!=e){ra=1;}
a=e+n;
rb=0;
while (!v[a].g2){
v[a].g2=true;
rb++;
a=v[a].p;
}
if (a!=e+n){rb=1;}
a=e;//print(e);
for (int i=0;i<ra;i++){
// print(a);
v[a].l1=true;
a=v[a].p;
}
a=e+n;
for (int i=0;i<rb;i++){
v[a].l2=true;
a=v[a].p;
}
//2 grow trees
vector<pair<int,int>> q;
pair<int,int> p;
a=e;
for (int i=0;i<ra;i++){
q.clear();
q.push_back({a,(ra-i)%ra});
while (!q.empty()){
p=q[q.size()-1];//print(p.first);
q.pop_back();
if (v[p.first].l1&&p.second!=((ra-i)%ra)){continue;}
v[p.first].z1=p.second;
for (int j:v[p.first].v){
q.push_back({j,p.second+1});
}
}
a=v[a].p;
}
a=e+n;
for (int i=0;i<rb;i++){
q.clear();
q.push_back({a,(rb-i)%rb});
while (!q.empty()){
p=q[q.size()-1];
q.pop_back();
if (v[p.first].l2&&p.second!=((rb-i)%rb)){continue;}
v[p.first].z2=p.second;
for (int j:v[p.first].v){
q.push_back({j,p.second+1});
}
}
a=v[a].p;
}
// for (int i=0;i<n*2;i++){print(v[i].z1);print(v[i].z2);}
//3 answer queries
int z;
for (int i=0;i<Q;i++){
a=G[i];
z=0;
for (int j=0;j<n;j++){
if (v[j].z1==a){
z++;
}else if (ra!=1 && a>=v[j].z1 && ((a-v[j].z1)%ra==0)){
z++;
}else if (v[j].z2==a){
z++;
}else if (rb!=1 && a>=v[j].z2 && ((a-v[j].z2)%rb==0)){
z++;
}
}
answer(z);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
3676 KB |
Output is correct |
12 |
Correct |
9 ms |
6296 KB |
Output is correct |
13 |
Correct |
28 ms |
15420 KB |
Output is correct |
14 |
Correct |
26 ms |
22100 KB |
Output is correct |
15 |
Correct |
38 ms |
22356 KB |
Output is correct |
16 |
Correct |
25 ms |
16768 KB |
Output is correct |
17 |
Correct |
24 ms |
14940 KB |
Output is correct |
18 |
Correct |
10 ms |
6236 KB |
Output is correct |
19 |
Correct |
27 ms |
22144 KB |
Output is correct |
20 |
Correct |
29 ms |
22356 KB |
Output is correct |
21 |
Correct |
28 ms |
16776 KB |
Output is correct |
22 |
Correct |
28 ms |
14940 KB |
Output is correct |
23 |
Correct |
28 ms |
23928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
3676 KB |
Output is correct |
12 |
Correct |
9 ms |
6296 KB |
Output is correct |
13 |
Correct |
28 ms |
15420 KB |
Output is correct |
14 |
Correct |
26 ms |
22100 KB |
Output is correct |
15 |
Correct |
38 ms |
22356 KB |
Output is correct |
16 |
Correct |
25 ms |
16768 KB |
Output is correct |
17 |
Correct |
24 ms |
14940 KB |
Output is correct |
18 |
Correct |
10 ms |
6236 KB |
Output is correct |
19 |
Correct |
27 ms |
22144 KB |
Output is correct |
20 |
Correct |
29 ms |
22356 KB |
Output is correct |
21 |
Correct |
28 ms |
16776 KB |
Output is correct |
22 |
Correct |
28 ms |
14940 KB |
Output is correct |
23 |
Correct |
28 ms |
23928 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
65 ms |
3940 KB |
Output is correct |
26 |
Correct |
84 ms |
6232 KB |
Output is correct |
27 |
Correct |
582 ms |
15452 KB |
Output is correct |
28 |
Correct |
593 ms |
22108 KB |
Output is correct |
29 |
Correct |
625 ms |
22592 KB |
Output is correct |
30 |
Correct |
338 ms |
16960 KB |
Output is correct |
31 |
Correct |
284 ms |
15092 KB |
Output is correct |
32 |
Correct |
73 ms |
6236 KB |
Output is correct |
33 |
Correct |
588 ms |
22472 KB |
Output is correct |
34 |
Correct |
601 ms |
22352 KB |
Output is correct |
35 |
Correct |
335 ms |
16732 KB |
Output is correct |
36 |
Correct |
289 ms |
14932 KB |
Output is correct |
37 |
Correct |
473 ms |
24168 KB |
Output is correct |
38 |
Correct |
1053 ms |
28832 KB |
Output is correct |