#include "gardenlib.h"
#include "garden.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 2e5+5;
int d[N<<1];
vector<pii> pre[N];
vector<int> g[N<<1];
void count_routes(int n, int m, int p, int r[][2], int q, int _g[]) {
for(int i = 0; i < m; i++) {
int a = r[i][0], b = r[i][1];
if(pre[a].size() < 2) pre[a].emplace_back(b, i);
if(pre[b].size() < 2) pre[b].emplace_back(a, i);
}
for(int i = 0; i < n; i++) if(pre[i].size() == 1) pre[i].emplace_back(pre[i][0]);
for(int i = 0; i < n; i++) for(int j = 0; j < (int)pre[i].size(); j++) {
int v = pre[i][j].x, id = pre[i][j].y;
if(pre[v][0].y == id) v += n;
g[v].emplace_back(i + (j ? n : 0));
}
int cyc_sz = 0;
auto get_dist = [&](int a) {
vector<int> chk(n<<1), S;
fill_n(d, N<<1, 1e9+1);
function<void(int)> dfs = [&](int u) {
chk[u] = 1, S.emplace_back(u);
for(int v : g[u]) {
if(!chk[v]) d[v] = d[u] + 1, dfs(v);
else if(chk[v] == 1) for(int i = S.size() - 1; ~i; i--) {
++cyc_sz;
if(S[i] == v) break;
}
}
chk[u] = 2, S.pop_back();
};
d[a] = cyc_sz = 0;
dfs(a);
};
auto get_ans = [&](int len) {
int ret = 0;
for(int i = 0; i < n; i++) {
if(d[i] > len) continue;
if(cyc_sz) ret += ((len - d[i]) % cyc_sz == 0);
else ret += (d[i] == len);
}
return ret;
};
vector<int> ans(q, 0);
get_dist(p);
for(int i = 0; i < q; i++) ans[i] += get_ans(_g[i]);
get_dist(p + n);
for(int i = 0; i < q; i++) answer(ans[i] + get_ans(_g[i]));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
16248 KB |
Output is correct |
2 |
Correct |
18 ms |
16248 KB |
Output is correct |
3 |
Correct |
19 ms |
16164 KB |
Output is correct |
4 |
Correct |
17 ms |
15964 KB |
Output is correct |
5 |
Correct |
17 ms |
15992 KB |
Output is correct |
6 |
Correct |
18 ms |
16376 KB |
Output is correct |
7 |
Correct |
16 ms |
16036 KB |
Output is correct |
8 |
Correct |
18 ms |
16012 KB |
Output is correct |
9 |
Correct |
21 ms |
16276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
16248 KB |
Output is correct |
2 |
Correct |
18 ms |
16248 KB |
Output is correct |
3 |
Correct |
19 ms |
16164 KB |
Output is correct |
4 |
Correct |
17 ms |
15964 KB |
Output is correct |
5 |
Correct |
17 ms |
15992 KB |
Output is correct |
6 |
Correct |
18 ms |
16376 KB |
Output is correct |
7 |
Correct |
16 ms |
16036 KB |
Output is correct |
8 |
Correct |
18 ms |
16012 KB |
Output is correct |
9 |
Correct |
21 ms |
16276 KB |
Output is correct |
10 |
Correct |
17 ms |
15992 KB |
Output is correct |
11 |
Correct |
30 ms |
18392 KB |
Output is correct |
12 |
Correct |
55 ms |
19872 KB |
Output is correct |
13 |
Correct |
94 ms |
44004 KB |
Output is correct |
14 |
Correct |
167 ms |
28404 KB |
Output is correct |
15 |
Correct |
213 ms |
28848 KB |
Output is correct |
16 |
Correct |
167 ms |
25872 KB |
Output is correct |
17 |
Correct |
134 ms |
24572 KB |
Output is correct |
18 |
Correct |
50 ms |
19652 KB |
Output is correct |
19 |
Correct |
169 ms |
28372 KB |
Output is correct |
20 |
Correct |
217 ms |
28580 KB |
Output is correct |
21 |
Correct |
158 ms |
25088 KB |
Output is correct |
22 |
Correct |
138 ms |
24408 KB |
Output is correct |
23 |
Correct |
174 ms |
29392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
16248 KB |
Output is correct |
2 |
Correct |
18 ms |
16248 KB |
Output is correct |
3 |
Correct |
19 ms |
16164 KB |
Output is correct |
4 |
Correct |
17 ms |
15964 KB |
Output is correct |
5 |
Correct |
17 ms |
15992 KB |
Output is correct |
6 |
Correct |
18 ms |
16376 KB |
Output is correct |
7 |
Correct |
16 ms |
16036 KB |
Output is correct |
8 |
Correct |
18 ms |
16012 KB |
Output is correct |
9 |
Correct |
21 ms |
16276 KB |
Output is correct |
10 |
Correct |
17 ms |
15992 KB |
Output is correct |
11 |
Correct |
30 ms |
18392 KB |
Output is correct |
12 |
Correct |
55 ms |
19872 KB |
Output is correct |
13 |
Correct |
94 ms |
44004 KB |
Output is correct |
14 |
Correct |
167 ms |
28404 KB |
Output is correct |
15 |
Correct |
213 ms |
28848 KB |
Output is correct |
16 |
Correct |
167 ms |
25872 KB |
Output is correct |
17 |
Correct |
134 ms |
24572 KB |
Output is correct |
18 |
Correct |
50 ms |
19652 KB |
Output is correct |
19 |
Correct |
169 ms |
28372 KB |
Output is correct |
20 |
Correct |
217 ms |
28580 KB |
Output is correct |
21 |
Correct |
158 ms |
25088 KB |
Output is correct |
22 |
Correct |
138 ms |
24408 KB |
Output is correct |
23 |
Correct |
174 ms |
29392 KB |
Output is correct |
24 |
Correct |
18 ms |
16004 KB |
Output is correct |
25 |
Correct |
114 ms |
18420 KB |
Output is correct |
26 |
Correct |
167 ms |
19880 KB |
Output is correct |
27 |
Correct |
1603 ms |
44000 KB |
Output is correct |
28 |
Correct |
1320 ms |
28512 KB |
Output is correct |
29 |
Correct |
1921 ms |
28640 KB |
Output is correct |
30 |
Correct |
1050 ms |
25820 KB |
Output is correct |
31 |
Correct |
992 ms |
24144 KB |
Output is correct |
32 |
Correct |
159 ms |
19988 KB |
Output is correct |
33 |
Correct |
1320 ms |
28120 KB |
Output is correct |
34 |
Correct |
1707 ms |
28320 KB |
Output is correct |
35 |
Correct |
1125 ms |
24184 KB |
Output is correct |
36 |
Correct |
1605 ms |
23032 KB |
Output is correct |
37 |
Correct |
957 ms |
28164 KB |
Output is correct |
38 |
Correct |
4030 ms |
48100 KB |
Output is correct |