#include "garden.h"
#include "gardenlib.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <tuple>
using namespace std;
const int N = 3e5 + 1, INF = 1e9+1;
int n, p, q, res[N], d[N];
vector<int> g[N], cycle;
vector<pair<int, int>> init_g[N];
int cycle_size;
bool mark[N];
void dfs(int u) {
mark[u] = true;
for(int v: g[u]) {
if(!mark[v]) {
d[v] = d[u] + 1;
dfs(v);
} else if(v == p) cycle_size = d[u] + 1;
}
}
void calc() {
fill(mark, mark+2*n, 0);
fill(d, d+2*n, 0);
cycle_size = 0;
dfs(p);
}
int solve(int len) {
int ret = 0;
if(cycle_size) {
for(int i = 0; i < n; i++) if(mark[i])
ret += d[i] <= len && (len - d[i]) % cycle_size == 0;
} else {
for(int i = 0; i < n; i++) if(mark[i])
ret += d[i] == len;
}
return ret;
}
void count_routes(int _n, int m, int _p, int _e[][2], int _q, int len[]) {
n = _n, p = _p, q = _q;
for(int i = 0; i < m; i++) {
if(init_g[_e[i][0]].size() < 2)
init_g[_e[i][0]].emplace_back(_e[i][1], i);
if(init_g[_e[i][1]].size() < 2)
init_g[_e[i][1]].emplace_back(_e[i][0], i);
}
for(int i = 0; i < n; i++) if(init_g[i].size() == 1) init_g[i].push_back(init_g[i][0]);
for(int u = 0; u < n; u++) {
for(int i = 0, v, idx; i < min((int)init_g[u].size(), 2); i++) {
tie(v, idx) = init_g[u][i];
g[v + (idx == init_g[v][0].second) * n].push_back(u + i * n);
}
}
calc();
for(int i = 0; i < q; i++) res[i] += solve(len[i]);
p += n;
calc();
for(int i = 0; i < q; i++) res[i] += solve(len[i]);
for(int i = 0; i < q; i++) answer(res[i]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14556 KB |
Output is correct |
2 |
Correct |
16 ms |
14684 KB |
Output is correct |
3 |
Correct |
16 ms |
14612 KB |
Output is correct |
4 |
Correct |
15 ms |
14492 KB |
Output is correct |
5 |
Correct |
15 ms |
14424 KB |
Output is correct |
6 |
Correct |
16 ms |
14556 KB |
Output is correct |
7 |
Correct |
19 ms |
14460 KB |
Output is correct |
8 |
Correct |
15 ms |
14556 KB |
Output is correct |
9 |
Correct |
17 ms |
14616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14556 KB |
Output is correct |
2 |
Correct |
16 ms |
14684 KB |
Output is correct |
3 |
Correct |
16 ms |
14612 KB |
Output is correct |
4 |
Correct |
15 ms |
14492 KB |
Output is correct |
5 |
Correct |
15 ms |
14424 KB |
Output is correct |
6 |
Correct |
16 ms |
14556 KB |
Output is correct |
7 |
Correct |
19 ms |
14460 KB |
Output is correct |
8 |
Correct |
15 ms |
14556 KB |
Output is correct |
9 |
Correct |
17 ms |
14616 KB |
Output is correct |
10 |
Correct |
15 ms |
14456 KB |
Output is correct |
11 |
Correct |
29 ms |
16504 KB |
Output is correct |
12 |
Correct |
52 ms |
17728 KB |
Output is correct |
13 |
Correct |
70 ms |
29320 KB |
Output is correct |
14 |
Correct |
197 ms |
25628 KB |
Output is correct |
15 |
Correct |
224 ms |
25896 KB |
Output is correct |
16 |
Correct |
170 ms |
22768 KB |
Output is correct |
17 |
Correct |
153 ms |
21576 KB |
Output is correct |
18 |
Correct |
54 ms |
17912 KB |
Output is correct |
19 |
Correct |
198 ms |
25596 KB |
Output is correct |
20 |
Correct |
236 ms |
25900 KB |
Output is correct |
21 |
Correct |
176 ms |
22720 KB |
Output is correct |
22 |
Correct |
152 ms |
21616 KB |
Output is correct |
23 |
Correct |
206 ms |
26816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14556 KB |
Output is correct |
2 |
Correct |
16 ms |
14684 KB |
Output is correct |
3 |
Correct |
16 ms |
14612 KB |
Output is correct |
4 |
Correct |
15 ms |
14492 KB |
Output is correct |
5 |
Correct |
15 ms |
14424 KB |
Output is correct |
6 |
Correct |
16 ms |
14556 KB |
Output is correct |
7 |
Correct |
19 ms |
14460 KB |
Output is correct |
8 |
Correct |
15 ms |
14556 KB |
Output is correct |
9 |
Correct |
17 ms |
14616 KB |
Output is correct |
10 |
Correct |
15 ms |
14456 KB |
Output is correct |
11 |
Correct |
29 ms |
16504 KB |
Output is correct |
12 |
Correct |
52 ms |
17728 KB |
Output is correct |
13 |
Correct |
70 ms |
29320 KB |
Output is correct |
14 |
Correct |
197 ms |
25628 KB |
Output is correct |
15 |
Correct |
224 ms |
25896 KB |
Output is correct |
16 |
Correct |
170 ms |
22768 KB |
Output is correct |
17 |
Correct |
153 ms |
21576 KB |
Output is correct |
18 |
Correct |
54 ms |
17912 KB |
Output is correct |
19 |
Correct |
198 ms |
25596 KB |
Output is correct |
20 |
Correct |
236 ms |
25900 KB |
Output is correct |
21 |
Correct |
176 ms |
22720 KB |
Output is correct |
22 |
Correct |
152 ms |
21616 KB |
Output is correct |
23 |
Correct |
206 ms |
26816 KB |
Output is correct |
24 |
Correct |
16 ms |
14428 KB |
Output is correct |
25 |
Correct |
146 ms |
16584 KB |
Output is correct |
26 |
Correct |
206 ms |
17836 KB |
Output is correct |
27 |
Correct |
1579 ms |
29324 KB |
Output is correct |
28 |
Correct |
1419 ms |
25968 KB |
Output is correct |
29 |
Correct |
1968 ms |
26192 KB |
Output is correct |
30 |
Correct |
1155 ms |
23040 KB |
Output is correct |
31 |
Correct |
1068 ms |
21852 KB |
Output is correct |
32 |
Correct |
227 ms |
17956 KB |
Output is correct |
33 |
Correct |
1418 ms |
25976 KB |
Output is correct |
34 |
Correct |
1821 ms |
26176 KB |
Output is correct |
35 |
Correct |
1152 ms |
23224 KB |
Output is correct |
36 |
Correct |
1633 ms |
21988 KB |
Output is correct |
37 |
Correct |
1122 ms |
27192 KB |
Output is correct |
38 |
Correct |
3871 ms |
35920 KB |
Output is correct |