#include "garden.h"
#include "gardenlib.h"
#define pb emplace_back
#include<bits/stdc++.h>
using namespace std;
vector<int>g[300005];
vector<int>g2[600005];
int jp=0;
vector<int>g_rev[600005];
bool cycle=false;
int c;
map<int,bool>vs;
bool vs1[600005],vs2[600005];
void dfs(int u,int cnt,int pp)
{
if(vs[u])
{
if(u==pp)
{
cycle = true;
c = cnt;
}
return ;
}
vs[u]=true;
for(auto v:g2[u])
{
dfs(v,cnt+1,pp);
}
}
int cnt_ans1[600005];
int cnt_ans2[600005];
void dfs1(int u,int cnt)
{
if(vs1[u]) return ;
vs1[u] = true;
cnt_ans1[u] = cnt;
for(auto v:g_rev[u]) dfs1(v,cnt+1);
}
void dfs2(int u,int cnt)
{
if(vs2[u]) return ;
vs2[u] = true;
cnt_ans2[u] = cnt;
for(auto v:g_rev[u]) dfs2(v,cnt+1);
}
void count_routes(int n, int m, int p, int R[][2], int Q, int G[])
{
for(int i=0;i<m;i++)
{
if(g[R[i][0]].size()<=2) g[R[i][0]].pb(R[i][1]);
if(g[R[i][1]].size()<=2) g[R[i][1]].pb(R[i][0]);
}
for(int i=0;i<n;i++)
{
int v = g[i][0];
if(i==g[v][0]) g2[i].pb(v+n);
else g2[i].pb(v);
int vv;
if(g[i].size()!=1) vv = g[i][1];
else vv = g[i][0];
if(i==g[vv][0]) g2[i+n].pb(vv+n);
else g2[i+n].pb(vv);
}
for(int i=0;i<2*n;i++)
{
g_rev[g2[i][0]].pb(i);
}
dfs(p,0,p);
bool cycle1 = cycle;
int c1 = c;
cycle=false;
vs.clear();
dfs(n+p,0,n+p);
vs.clear();
bool cycle2 = cycle;
int c2 = c;
dfs1(p,0);
dfs2(p+n,0);
for(int k=0;k<Q;k++)
{
int d = G[k];
int ans=0;
for(int i=0;i<n;i++)
{
bool ok = 0;
int len = d - cnt_ans1[i];
if(len >= 0 and vs1[i]) {
if(cycle1 and len%c1 == 0) ok = 1;
else if(!cycle1 and len == 0) ok = 1;
}
len = d - cnt_ans2[i];
if(len >= 0 and vs2[i]) {
if(cycle2 and len%c2 == 0) ok = 1;
else if(!cycle2 and len == 0) ok = 1;
}
ans += ok;
continue ;
// bool ch=true;
// if(cycle1 and (d-cnt_ans1[i])%c1==0)
// {
// ans++;
// ch=false;
// }
// else if(!cycle1 and d==cnt_ans1[i])
// {
// ans++;
// ch=false;
// }
// if(!ch) continue;
// if(cycle2 and (d-cnt_ans2[i])%c2==0)
// {
// ans++;
// ch=false;
// }
// else if(!cycle2 and d==cnt_ans2[i])
// {
// ans++;
// ch=false;
// }
// cout << ans << ' ' << i << endl;
}
// cout << endl;
answer(ans);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
39504 KB |
Output is correct |
2 |
Correct |
10 ms |
39504 KB |
Output is correct |
3 |
Correct |
8 ms |
39504 KB |
Output is correct |
4 |
Correct |
7 ms |
39416 KB |
Output is correct |
5 |
Correct |
7 ms |
39352 KB |
Output is correct |
6 |
Correct |
8 ms |
39504 KB |
Output is correct |
7 |
Correct |
8 ms |
39248 KB |
Output is correct |
8 |
Correct |
8 ms |
39376 KB |
Output is correct |
9 |
Correct |
9 ms |
39516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
39504 KB |
Output is correct |
2 |
Correct |
10 ms |
39504 KB |
Output is correct |
3 |
Correct |
8 ms |
39504 KB |
Output is correct |
4 |
Correct |
7 ms |
39416 KB |
Output is correct |
5 |
Correct |
7 ms |
39352 KB |
Output is correct |
6 |
Correct |
8 ms |
39504 KB |
Output is correct |
7 |
Correct |
8 ms |
39248 KB |
Output is correct |
8 |
Correct |
8 ms |
39376 KB |
Output is correct |
9 |
Correct |
9 ms |
39516 KB |
Output is correct |
10 |
Correct |
8 ms |
39248 KB |
Output is correct |
11 |
Correct |
19 ms |
44624 KB |
Output is correct |
12 |
Correct |
27 ms |
46160 KB |
Output is correct |
13 |
Correct |
123 ms |
78152 KB |
Output is correct |
14 |
Correct |
82 ms |
60744 KB |
Output is correct |
15 |
Correct |
84 ms |
60920 KB |
Output is correct |
16 |
Correct |
69 ms |
55892 KB |
Output is correct |
17 |
Correct |
70 ms |
53576 KB |
Output is correct |
18 |
Correct |
26 ms |
48476 KB |
Output is correct |
19 |
Correct |
78 ms |
60840 KB |
Output is correct |
20 |
Correct |
82 ms |
61000 KB |
Output is correct |
21 |
Correct |
64 ms |
55624 KB |
Output is correct |
22 |
Correct |
60 ms |
53584 KB |
Output is correct |
23 |
Correct |
109 ms |
62544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
39504 KB |
Output is correct |
2 |
Correct |
10 ms |
39504 KB |
Output is correct |
3 |
Correct |
8 ms |
39504 KB |
Output is correct |
4 |
Correct |
7 ms |
39416 KB |
Output is correct |
5 |
Correct |
7 ms |
39352 KB |
Output is correct |
6 |
Correct |
8 ms |
39504 KB |
Output is correct |
7 |
Correct |
8 ms |
39248 KB |
Output is correct |
8 |
Correct |
8 ms |
39376 KB |
Output is correct |
9 |
Correct |
9 ms |
39516 KB |
Output is correct |
10 |
Correct |
8 ms |
39248 KB |
Output is correct |
11 |
Correct |
19 ms |
44624 KB |
Output is correct |
12 |
Correct |
27 ms |
46160 KB |
Output is correct |
13 |
Correct |
123 ms |
78152 KB |
Output is correct |
14 |
Correct |
82 ms |
60744 KB |
Output is correct |
15 |
Correct |
84 ms |
60920 KB |
Output is correct |
16 |
Correct |
69 ms |
55892 KB |
Output is correct |
17 |
Correct |
70 ms |
53576 KB |
Output is correct |
18 |
Correct |
26 ms |
48476 KB |
Output is correct |
19 |
Correct |
78 ms |
60840 KB |
Output is correct |
20 |
Correct |
82 ms |
61000 KB |
Output is correct |
21 |
Correct |
64 ms |
55624 KB |
Output is correct |
22 |
Correct |
60 ms |
53584 KB |
Output is correct |
23 |
Correct |
109 ms |
62544 KB |
Output is correct |
24 |
Correct |
7 ms |
39248 KB |
Output is correct |
25 |
Correct |
120 ms |
44624 KB |
Output is correct |
26 |
Correct |
201 ms |
48464 KB |
Output is correct |
27 |
Correct |
796 ms |
78368 KB |
Output is correct |
28 |
Correct |
1120 ms |
60744 KB |
Output is correct |
29 |
Correct |
1055 ms |
61168 KB |
Output is correct |
30 |
Correct |
627 ms |
55888 KB |
Output is correct |
31 |
Correct |
453 ms |
53752 KB |
Output is correct |
32 |
Correct |
208 ms |
46448 KB |
Output is correct |
33 |
Correct |
1104 ms |
60736 KB |
Output is correct |
34 |
Correct |
1064 ms |
61256 KB |
Output is correct |
35 |
Correct |
556 ms |
55624 KB |
Output is correct |
36 |
Correct |
862 ms |
53576 KB |
Output is correct |
37 |
Correct |
861 ms |
62756 KB |
Output is correct |
38 |
Correct |
1707 ms |
85832 KB |
Output is correct |