#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int deg[N], Mn[N], F[N], D[N], n, Res[N];
bool v[N];
vector < int > E[N];
void DFS(int a, int d)
{
int i;
D[a] = d;
for (i = 0; i < (int)E[a].size(); i++){
if (D[E[a][i]] == -1)DFS(E[a][i], d + 1);
}
}
int QQ, DD[N];
struct Query{
int a, num;
bool operator<(const Query &p)const{
return a < p.a;
}
}w[N];
int S[N];
void Do(int a){
int i, x, c = 0, cnt = 0, pv;
for (i = 0; i < 2 * n; i++){
D[i] = -1;
v[i] = false;
S[i] = 0;
}
DFS(a, 0);
for (i = 0; i < n; i++){
if (D[i] != -1){
DD[cnt++] = D[i];
}
}
sort(DD, DD + cnt);
x = a;
while (!v[x]){
c++;
v[x] = true;
x = F[x];
}
if (x != a){
c = 1000000009;
}
pv = 0;
for (i = 0; i < QQ; i++){
while (pv < cnt && DD[pv] <= w[i].a){
S[DD[pv] % c]++;
pv++;
}
if (w[i].a%c > 2 * n)continue;
Res[w[i].num] += S[w[i].a%c];
}
}
void Add(int a, int b, int i){
if (Mn[a] == i){
if (Mn[b] != i || deg[b] == 1)F[a] = b;
else F[a] = b + n;
}
else if (F[a + n] == -1){
if (Mn[b] != i || deg[b] == 1)F[a + n] = b;
else F[a + n] = b + n;
}
}
void count_routes(int N2, int M, int P, int R[][2], int Q, int G[])
{
int i, a, b;
n = N2;
QQ = Q;
for (i = 0; i < Q; i++){
w[i].a = G[i];
w[i].num = i;
}
sort(w, w + Q);
for (i = 0; i < 2 * N2; i++)F[i] = -1;
for (i = 0; i < M; i++){
a = R[i][0], b = R[i][1];
deg[a]++, deg[b]++;
if (!Mn[a])Mn[a] = i + 1;
if (!Mn[b])Mn[b] = i + 1;
}
for (i = 0; i < M; i++){
a = R[i][0], b = R[i][1];
Add(a, b, i+1);
Add(b, a, i+1);
}
for (i = 0; i < N2; i++){
E[F[i]].push_back(i);
if (deg[i]>1)E[F[i + N2]].push_back(i + N2);
}
Do(P);
if (deg[P] != 1)Do(P + N2);
for (i = 0; i < Q; i++)answer(Res[i]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12276 KB |
Output is correct |
2 |
Correct |
9 ms |
12272 KB |
Output is correct |
3 |
Correct |
9 ms |
12192 KB |
Output is correct |
4 |
Correct |
8 ms |
12060 KB |
Output is correct |
5 |
Correct |
8 ms |
12084 KB |
Output is correct |
6 |
Correct |
8 ms |
12236 KB |
Output is correct |
7 |
Correct |
8 ms |
12056 KB |
Output is correct |
8 |
Correct |
8 ms |
12108 KB |
Output is correct |
9 |
Correct |
10 ms |
12200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12276 KB |
Output is correct |
2 |
Correct |
9 ms |
12272 KB |
Output is correct |
3 |
Correct |
9 ms |
12192 KB |
Output is correct |
4 |
Correct |
8 ms |
12060 KB |
Output is correct |
5 |
Correct |
8 ms |
12084 KB |
Output is correct |
6 |
Correct |
8 ms |
12236 KB |
Output is correct |
7 |
Correct |
8 ms |
12056 KB |
Output is correct |
8 |
Correct |
8 ms |
12108 KB |
Output is correct |
9 |
Correct |
10 ms |
12200 KB |
Output is correct |
10 |
Correct |
8 ms |
12056 KB |
Output is correct |
11 |
Correct |
17 ms |
14252 KB |
Output is correct |
12 |
Correct |
29 ms |
15692 KB |
Output is correct |
13 |
Correct |
62 ms |
30144 KB |
Output is correct |
14 |
Correct |
79 ms |
23876 KB |
Output is correct |
15 |
Correct |
121 ms |
24556 KB |
Output is correct |
16 |
Correct |
110 ms |
21476 KB |
Output is correct |
17 |
Correct |
75 ms |
20388 KB |
Output is correct |
18 |
Correct |
31 ms |
15668 KB |
Output is correct |
19 |
Correct |
78 ms |
23772 KB |
Output is correct |
20 |
Correct |
112 ms |
24472 KB |
Output is correct |
21 |
Correct |
88 ms |
21416 KB |
Output is correct |
22 |
Correct |
82 ms |
20312 KB |
Output is correct |
23 |
Correct |
89 ms |
24964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12276 KB |
Output is correct |
2 |
Correct |
9 ms |
12272 KB |
Output is correct |
3 |
Correct |
9 ms |
12192 KB |
Output is correct |
4 |
Correct |
8 ms |
12060 KB |
Output is correct |
5 |
Correct |
8 ms |
12084 KB |
Output is correct |
6 |
Correct |
8 ms |
12236 KB |
Output is correct |
7 |
Correct |
8 ms |
12056 KB |
Output is correct |
8 |
Correct |
8 ms |
12108 KB |
Output is correct |
9 |
Correct |
10 ms |
12200 KB |
Output is correct |
10 |
Correct |
8 ms |
12056 KB |
Output is correct |
11 |
Correct |
17 ms |
14252 KB |
Output is correct |
12 |
Correct |
29 ms |
15692 KB |
Output is correct |
13 |
Correct |
62 ms |
30144 KB |
Output is correct |
14 |
Correct |
79 ms |
23876 KB |
Output is correct |
15 |
Correct |
121 ms |
24556 KB |
Output is correct |
16 |
Correct |
110 ms |
21476 KB |
Output is correct |
17 |
Correct |
75 ms |
20388 KB |
Output is correct |
18 |
Correct |
31 ms |
15668 KB |
Output is correct |
19 |
Correct |
78 ms |
23772 KB |
Output is correct |
20 |
Correct |
112 ms |
24472 KB |
Output is correct |
21 |
Correct |
88 ms |
21416 KB |
Output is correct |
22 |
Correct |
82 ms |
20312 KB |
Output is correct |
23 |
Correct |
89 ms |
24964 KB |
Output is correct |
24 |
Correct |
9 ms |
12108 KB |
Output is correct |
25 |
Correct |
18 ms |
14380 KB |
Output is correct |
26 |
Correct |
29 ms |
15752 KB |
Output is correct |
27 |
Correct |
63 ms |
30236 KB |
Output is correct |
28 |
Correct |
80 ms |
23828 KB |
Output is correct |
29 |
Correct |
116 ms |
24660 KB |
Output is correct |
30 |
Correct |
87 ms |
21620 KB |
Output is correct |
31 |
Correct |
75 ms |
20424 KB |
Output is correct |
32 |
Correct |
29 ms |
15708 KB |
Output is correct |
33 |
Correct |
82 ms |
23844 KB |
Output is correct |
34 |
Correct |
142 ms |
24644 KB |
Output is correct |
35 |
Correct |
86 ms |
21488 KB |
Output is correct |
36 |
Correct |
82 ms |
20452 KB |
Output is correct |
37 |
Correct |
83 ms |
24900 KB |
Output is correct |
38 |
Correct |
92 ms |
36896 KB |
Output is correct |