#include"garden.h"
#include"gardenlib.h"
#include<vector>
#include<algorithm>
#define INF 1000000000
using namespace std;
int Exit[300005]; //daca i<=N sunt in nodul i si ies prin cea mai mare muchie, i>N ies prin a doua cea mai mare
int Prim[150005],Sec[150005];
bool Viz[300005];
int Dist[300005];
int Sol[2005];
void getDist(int nod)
{
Viz[nod]=1;
if(!Viz[Exit[nod]])
getDist(Exit[nod]);
Dist[nod]=1+Dist[Exit[nod]];
}
pair<int,int> Qry[2005];
int Fr[300005];
void getSol(int N, int Q, int G[], int cycleSize)
{
for(int i=0; i<=300000; i++)
Fr[i]=0;
for(int i=0; i<Q; i++)
Qry[i+1]={G[i],i+1};
sort(Qry+1,Qry+Q+1);
sort(Dist+1,Dist+N+1);
//Dist[nod]<=Qry[ind] si Dist[nod]%cycleSize==Qry[ind]%cycleSize ca sa pot ajunge in P si nod<=N (o sa ies prin cel mai mare edge)
//daca cycleSize e INF, practic nu se face modulo, lungimile trb sa fie egale (P nu e in niciun ciclu)
int j=1;
for(int i=1; i<=Q; i++)
{
while(j<=N && Dist[j]<=Qry[i].first)
{
if(Dist[j]>=INF)
continue;
Fr[Dist[j]%cycleSize]++;
j++;
}
if(cycleSize<INF)
Sol[Qry[i].second]+=Fr[Qry[i].first%cycleSize];
else if(Qry[i].first<=300000)
Sol[Qry[i].second]+=Fr[Qry[i].first];
}
}
void addEdge(int ind, int x, int y, int N)
{
if(ind==Prim[x])
{
if(ind==Prim[y] && Sec[y])
Exit[x]=N+y;
else
Exit[x]=y;
}
else if(ind==Sec[x])
{
if(ind==Prim[y] && Sec[y])
Exit[N+x]=N+y;
else
Exit[N+x]=y;
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
for(int i=0; i<M; i++)
{
R[i][0]++;
R[i][1]++;
if(!Prim[R[i][0]])
Prim[R[i][0]]=i+1;
else if(!Sec[R[i][0]])
Sec[R[i][0]]=i+1;
if(!Prim[R[i][1]])
Prim[R[i][1]]=i+1;
else if(!Sec[R[i][1]])
Sec[R[i][1]]=i+1;
}
for(int i=0; i<=M; i++)
{
addEdge(i+1,R[i][0],R[i][1],N);
addEdge(i+1,R[i][1],R[i][0],N);
}
for(int i=1; i<=2*N; i++)
Dist[i]=INF;
P++;
Dist[0]=INF;
Viz[0]=1;
//termin in nodul P
Viz[P]=1;
Dist[P]=0;
for(int i=1; i<=2*N; i++)
{
if(!Viz[i])
getDist(i);
}
int cycleSize=1+Dist[Exit[P]]; //dupa ce va ajunge in P va face ciclul respectiv, daca P se afla pe vreun ciclu
getSol(N,Q,G,cycleSize);
//termin in 2*P
for(int i=1; i<=2*N; i++)
{
Dist[i]=INF;
Viz[i]=0;
}
Viz[N+P]=1;
Dist[N+P]=0;
for(int i=1; i<=2*N; i++)
{
if(!Viz[i])
getDist(i);
}
cycleSize=1+Dist[Exit[N+P]];
getSol(N,Q,G,cycleSize);
for(int i=1; i<=Q; i++)
answer(Sol[i]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1536 KB |
Output is correct |
2 |
Correct |
5 ms |
1536 KB |
Output is correct |
3 |
Correct |
6 ms |
1536 KB |
Output is correct |
4 |
Correct |
5 ms |
1536 KB |
Output is correct |
5 |
Correct |
5 ms |
1536 KB |
Output is correct |
6 |
Correct |
6 ms |
1536 KB |
Output is correct |
7 |
Correct |
6 ms |
1536 KB |
Output is correct |
8 |
Correct |
6 ms |
1536 KB |
Output is correct |
9 |
Correct |
7 ms |
1664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1536 KB |
Output is correct |
2 |
Correct |
5 ms |
1536 KB |
Output is correct |
3 |
Correct |
6 ms |
1536 KB |
Output is correct |
4 |
Correct |
5 ms |
1536 KB |
Output is correct |
5 |
Correct |
5 ms |
1536 KB |
Output is correct |
6 |
Correct |
6 ms |
1536 KB |
Output is correct |
7 |
Correct |
6 ms |
1536 KB |
Output is correct |
8 |
Correct |
6 ms |
1536 KB |
Output is correct |
9 |
Correct |
7 ms |
1664 KB |
Output is correct |
10 |
Correct |
5 ms |
1536 KB |
Output is correct |
11 |
Correct |
14 ms |
2304 KB |
Output is correct |
12 |
Correct |
27 ms |
2944 KB |
Output is correct |
13 |
Correct |
44 ms |
7032 KB |
Output is correct |
14 |
Correct |
60 ms |
6008 KB |
Output is correct |
15 |
Correct |
64 ms |
6136 KB |
Output is correct |
16 |
Correct |
62 ms |
4984 KB |
Output is correct |
17 |
Correct |
53 ms |
4604 KB |
Output is correct |
18 |
Correct |
27 ms |
2944 KB |
Output is correct |
19 |
Correct |
61 ms |
6008 KB |
Output is correct |
20 |
Correct |
67 ms |
6264 KB |
Output is correct |
21 |
Correct |
60 ms |
4984 KB |
Output is correct |
22 |
Correct |
56 ms |
4600 KB |
Output is correct |
23 |
Correct |
76 ms |
6520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1536 KB |
Output is correct |
2 |
Correct |
5 ms |
1536 KB |
Output is correct |
3 |
Correct |
6 ms |
1536 KB |
Output is correct |
4 |
Correct |
5 ms |
1536 KB |
Output is correct |
5 |
Correct |
5 ms |
1536 KB |
Output is correct |
6 |
Correct |
6 ms |
1536 KB |
Output is correct |
7 |
Correct |
6 ms |
1536 KB |
Output is correct |
8 |
Correct |
6 ms |
1536 KB |
Output is correct |
9 |
Correct |
7 ms |
1664 KB |
Output is correct |
10 |
Correct |
5 ms |
1536 KB |
Output is correct |
11 |
Correct |
14 ms |
2304 KB |
Output is correct |
12 |
Correct |
27 ms |
2944 KB |
Output is correct |
13 |
Correct |
44 ms |
7032 KB |
Output is correct |
14 |
Correct |
60 ms |
6008 KB |
Output is correct |
15 |
Correct |
64 ms |
6136 KB |
Output is correct |
16 |
Correct |
62 ms |
4984 KB |
Output is correct |
17 |
Correct |
53 ms |
4604 KB |
Output is correct |
18 |
Correct |
27 ms |
2944 KB |
Output is correct |
19 |
Correct |
61 ms |
6008 KB |
Output is correct |
20 |
Correct |
67 ms |
6264 KB |
Output is correct |
21 |
Correct |
60 ms |
4984 KB |
Output is correct |
22 |
Correct |
56 ms |
4600 KB |
Output is correct |
23 |
Correct |
76 ms |
6520 KB |
Output is correct |
24 |
Correct |
6 ms |
1536 KB |
Output is correct |
25 |
Correct |
17 ms |
2432 KB |
Output is correct |
26 |
Correct |
28 ms |
2944 KB |
Output is correct |
27 |
Correct |
43 ms |
7040 KB |
Output is correct |
28 |
Correct |
70 ms |
6136 KB |
Output is correct |
29 |
Correct |
66 ms |
6264 KB |
Output is correct |
30 |
Correct |
67 ms |
4984 KB |
Output is correct |
31 |
Correct |
56 ms |
4600 KB |
Output is correct |
32 |
Correct |
27 ms |
2936 KB |
Output is correct |
33 |
Correct |
63 ms |
6136 KB |
Output is correct |
34 |
Correct |
68 ms |
6264 KB |
Output is correct |
35 |
Correct |
60 ms |
4984 KB |
Output is correct |
36 |
Correct |
58 ms |
4600 KB |
Output is correct |
37 |
Correct |
75 ms |
6648 KB |
Output is correct |
38 |
Correct |
83 ms |
8824 KB |
Output is correct |