#include "garden.h"
#include "gardenlib.h"
#include <vector>
#define MAXN 150000
#define MAXK 300000
#define MAXQ 2000
int ans[MAXQ];
std::vector < int > g[MAXK + 1];
int muchie[MAXN][2], t[MAXK + 1];
int id[MAXN][2], h[MAXK + 1];
bool viz[MAXK + 1];
inline void avem(int x, int y) {
if (muchie[x][0] == -1)
muchie[x][0] = y;
else if (muchie[x][1] == -1)
muchie[x][1] = y;
}
inline void getPapa(int x, int r) {
if (muchie[muchie[x][r]][0] != x) {
t[id[x][r]] = id[muchie[x][r]][0];
g[t[id[x][r]]].push_back(id[x][r]);
} else {
t[id[x][r]] = id[muchie[x][r]][1];
g[t[id[x][r]]].push_back(id[x][r]);
}
}
void dfs(int x) {
viz[x] = 1;
for (auto &y : g[x]) {
if (viz[y] == 0) {
h[y] = h[x] + 1;
dfs(y);
}
}
}
inline void solve(int s, int n, int q, int k[], int N) {
for (int i = 1; i <= n; i++)
viz[i] = 0;
int x = s, cnt = 0;
while (viz[x] == 0) {
viz[x] = 1;
h[x] = 0;
cnt++;
x = t[x];
}
if (x == s) {
int len = cnt;
while (cnt > 0) {
cnt--;
x = t[x];
h[x] = cnt;
dfs(x);
}
for (int i = 0; i < q; i++)
for (int j = 0; j < N; j++)
if (viz[id[j][0]] && k[i] >= h[id[j][0]])
ans[i] += (k[i] - h[id[j][0]]) % len == 0;
} else {
dfs(s);
for (int i = 0; i < q; i++)
for (int j = 0; j < N; j++)
if (viz[id[j][0]] && h[id[j][0]])
ans[i] += k[i] == h[id[j][0]];
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
for (int i = 0; i < N; i++)
muchie[i][0] = muchie[i][1] = -1;
for (int i = 0; i < M; i++) {
int x = R[i][0], y = R[i][1];
avem(x, y);
avem(y, x);
}
int k = 0;
for (int i = 0; i < N; i++) {
id[i][0] = ++k;
if (muchie[i][1] == -1)
muchie[i][1] = muchie[i][0], id[i][1] = k;
else
id[i][1] = ++k;
}
for (int i = 1; i <= k; i++)
g[i].clear();
for (int i = 0; i < N; i++) {
getPapa(i, 0);
getPapa(i, 1);
}
for (int i = 0; i < Q; i++)
ans[i] = 0;
solve(id[P][0], k, Q, G, N);
if (id[P][1] != id[P][0])
solve(id[P][1], k, Q, G, N);
for (int i = 0; i < Q; i++)
answer(ans[i]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7544 KB |
Output is correct |
2 |
Correct |
9 ms |
7552 KB |
Output is correct |
3 |
Correct |
8 ms |
7544 KB |
Output is correct |
4 |
Correct |
8 ms |
7448 KB |
Output is correct |
5 |
Correct |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7544 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
11 ms |
7644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7544 KB |
Output is correct |
2 |
Correct |
9 ms |
7552 KB |
Output is correct |
3 |
Correct |
8 ms |
7544 KB |
Output is correct |
4 |
Correct |
8 ms |
7448 KB |
Output is correct |
5 |
Correct |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7544 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
11 ms |
7644 KB |
Output is correct |
10 |
Correct |
8 ms |
7412 KB |
Output is correct |
11 |
Correct |
18 ms |
9344 KB |
Output is correct |
12 |
Correct |
33 ms |
10832 KB |
Output is correct |
13 |
Correct |
52 ms |
16800 KB |
Output is correct |
14 |
Correct |
117 ms |
17720 KB |
Output is correct |
15 |
Correct |
144 ms |
18064 KB |
Output is correct |
16 |
Correct |
103 ms |
15804 KB |
Output is correct |
17 |
Correct |
95 ms |
14840 KB |
Output is correct |
18 |
Correct |
32 ms |
10792 KB |
Output is correct |
19 |
Correct |
108 ms |
17752 KB |
Output is correct |
20 |
Correct |
147 ms |
18136 KB |
Output is correct |
21 |
Correct |
111 ms |
14916 KB |
Output is correct |
22 |
Correct |
102 ms |
14028 KB |
Output is correct |
23 |
Correct |
116 ms |
17884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7544 KB |
Output is correct |
2 |
Correct |
9 ms |
7552 KB |
Output is correct |
3 |
Correct |
8 ms |
7544 KB |
Output is correct |
4 |
Correct |
8 ms |
7448 KB |
Output is correct |
5 |
Correct |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7544 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
11 ms |
7644 KB |
Output is correct |
10 |
Correct |
8 ms |
7412 KB |
Output is correct |
11 |
Correct |
18 ms |
9344 KB |
Output is correct |
12 |
Correct |
33 ms |
10832 KB |
Output is correct |
13 |
Correct |
52 ms |
16800 KB |
Output is correct |
14 |
Correct |
117 ms |
17720 KB |
Output is correct |
15 |
Correct |
144 ms |
18064 KB |
Output is correct |
16 |
Correct |
103 ms |
15804 KB |
Output is correct |
17 |
Correct |
95 ms |
14840 KB |
Output is correct |
18 |
Correct |
32 ms |
10792 KB |
Output is correct |
19 |
Correct |
108 ms |
17752 KB |
Output is correct |
20 |
Correct |
147 ms |
18136 KB |
Output is correct |
21 |
Correct |
111 ms |
14916 KB |
Output is correct |
22 |
Correct |
102 ms |
14028 KB |
Output is correct |
23 |
Correct |
116 ms |
17884 KB |
Output is correct |
24 |
Correct |
9 ms |
7416 KB |
Output is correct |
25 |
Correct |
140 ms |
9304 KB |
Output is correct |
26 |
Correct |
204 ms |
10652 KB |
Output is correct |
27 |
Correct |
1702 ms |
16512 KB |
Output is correct |
28 |
Correct |
1519 ms |
17200 KB |
Output is correct |
29 |
Correct |
2129 ms |
17308 KB |
Output is correct |
30 |
Correct |
1179 ms |
14788 KB |
Output is correct |
31 |
Correct |
1115 ms |
14388 KB |
Output is correct |
32 |
Correct |
236 ms |
10892 KB |
Output is correct |
33 |
Correct |
1488 ms |
17716 KB |
Output is correct |
34 |
Correct |
1916 ms |
17804 KB |
Output is correct |
35 |
Correct |
1163 ms |
15172 KB |
Output is correct |
36 |
Correct |
1834 ms |
14036 KB |
Output is correct |
37 |
Correct |
1145 ms |
18072 KB |
Output is correct |
38 |
Correct |
4413 ms |
22984 KB |
Output is correct |