#include "garden.h"
#include "gardenlib.h"
#include <algorithm>
#include <vector>
#define N 150001
#define MP 23
using namespace std;
int n, m, start, cycle1_len, cycle2_len;
int Sx, Sy, d[N][2], d1[N][2], d2[N][2];
pair<int, int> r[N];
void Push_r(int x, int y) {
if (!r[x].first) r[x].first = y;
else if (!r[x].second) r[x].second = y;
}
int dfs(int x, int y, int len) {
d[x][y] = len;
int nx = y ? r[x].second : r[x].first;
int ny = r[nx].second && r[nx].first == x ? 1 : 0;
if (d[nx][ny]) {
if (d[nx][ny] == 1) return len;
else return -1;
}
return dfs(nx, ny, len + 1);
}
int dfs_find1(int x, int y) {
if (x == start && y == 0) return 0;
if (d1[x][y] != 0) return d1[x][y];
int nx = y ? r[x].second : r[x].first;
int ny = r[nx].second && r[nx].first == x ? 1 : 0;
d1[x][y] = -1, d1[x][y] = dfs_find1(nx, ny);
if (d1[x][y] >= 0) d1[x][y]++;
return d1[x][y];
}
int dfs_find2(int x, int y) {
if (x == start && y == 1) return 0;
if (d2[x][y] != 0) return d2[x][y];
int nx = y ? r[x].second : r[x].first;
int ny = r[nx].second && r[nx].first == x ? 1 : 0;
d2[x][y] = -1, d2[x][y] = dfs_find2(nx, ny);
if (d2[x][y] >= 0) d2[x][y]++;
return d2[x][y];
}
void count_routes(int _n, int _m, int _s, int _r[][2], int Q_len, int Q[]) {
n = _n, start = _s + 1;
for (int i = 0; i < _m; i++) {
int x = _r[i][0] + 1, y = _r[i][1] + 1;
Push_r(x, y), Push_r(y, x);
}
cycle1_len = dfs(start, 0, 1);
for (int i = 1; i <= n; i++) d[i][0] = d[i][1] = 0;
cycle2_len = dfs(start, 1, 1);
for (int i = 1; i <= n; i++) d[i][0] = d[i][1] = 0;
for (int i = 1; i <= n; i++) {
if (d1[i][0] == 0) dfs_find1(i, 0);
if (d2[i][0] == 0) dfs_find2(i, 0);
if (i == start) d1[i][0] = 0;
}
for (int i = 0; i < Q_len; i++) {
int res = 0;
if (Q[i] == 0) { answer(1); continue; }
for (int j = 1; j <= n; j++) {
if ((d1[j][0] > 0 || j == start) && cycle1_len > 0 && Q[i] >= d1[j][0] && (Q[i] - d1[j][0]) % cycle1_len == 0) res++;
else if (d2[j][0] > 0 && cycle2_len > 0 && Q[i] >= d2[j][0] && (Q[i] - d2[j][0]) % cycle2_len == 0) res++;
} answer(res);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
424 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
2 ms |
352 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
388 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
424 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
2 ms |
352 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
388 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
9 ms |
1556 KB |
Output is correct |
12 |
Correct |
17 ms |
2268 KB |
Output is correct |
13 |
Correct |
32 ms |
6640 KB |
Output is correct |
14 |
Correct |
47 ms |
5996 KB |
Output is correct |
15 |
Correct |
50 ms |
6320 KB |
Output is correct |
16 |
Correct |
43 ms |
4812 KB |
Output is correct |
17 |
Correct |
42 ms |
4264 KB |
Output is correct |
18 |
Correct |
17 ms |
2312 KB |
Output is correct |
19 |
Correct |
47 ms |
6444 KB |
Output is correct |
20 |
Correct |
49 ms |
6480 KB |
Output is correct |
21 |
Correct |
45 ms |
5032 KB |
Output is correct |
22 |
Correct |
46 ms |
4472 KB |
Output is correct |
23 |
Correct |
50 ms |
6896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
424 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
2 ms |
352 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
388 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
9 ms |
1556 KB |
Output is correct |
12 |
Correct |
17 ms |
2268 KB |
Output is correct |
13 |
Correct |
32 ms |
6640 KB |
Output is correct |
14 |
Correct |
47 ms |
5996 KB |
Output is correct |
15 |
Correct |
50 ms |
6320 KB |
Output is correct |
16 |
Correct |
43 ms |
4812 KB |
Output is correct |
17 |
Correct |
42 ms |
4264 KB |
Output is correct |
18 |
Correct |
17 ms |
2312 KB |
Output is correct |
19 |
Correct |
47 ms |
6444 KB |
Output is correct |
20 |
Correct |
49 ms |
6480 KB |
Output is correct |
21 |
Correct |
45 ms |
5032 KB |
Output is correct |
22 |
Correct |
46 ms |
4472 KB |
Output is correct |
23 |
Correct |
50 ms |
6896 KB |
Output is correct |
24 |
Correct |
3 ms |
296 KB |
Output is correct |
25 |
Correct |
86 ms |
1592 KB |
Output is correct |
26 |
Correct |
116 ms |
2632 KB |
Output is correct |
27 |
Correct |
1762 ms |
7388 KB |
Output is correct |
28 |
Correct |
1144 ms |
7288 KB |
Output is correct |
29 |
Correct |
1796 ms |
7588 KB |
Output is correct |
30 |
Correct |
946 ms |
5928 KB |
Output is correct |
31 |
Correct |
1089 ms |
5424 KB |
Output is correct |
32 |
Incorrect |
126 ms |
2556 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |