#include "garden.h"
#include "gardenlib.h"
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#define L 150005
using namespace std;
int ty;
int n, m, st, cnt1, cnt, cnt2, fi[L], se[L], b[L][2][2], la[L][2], l[L][2];
bool v[L][2], v2[L][2], v3[L][2], ans[L];
vector<int> a[L], c[L][2][2];
void f(int p, int q) {
int t;
// if (v[p][q]) return;
// v[p][q] = 1;
if (q == 0) {
t = fi[p];
} else {
t = se[p];
}
b[p][q][0] = t;
b[p][q][1] = (fi[t] == p);
// f(t, (fi[t] == p));
}
void g(int p, int q) {
int i, cnt, t1, t2, j1, j2;
if (v2[p][q]) {
j1 = p;
j2 = q;
cnt = 0;
while (1) {
cnt++;
t1 = c[j1][j2][0][la[j1][j2]];
t2 = c[j1][j2][1][la[j1][j2]];
j1 = t1;
j2 = t2;
if (j1 == p && j2 == q) break;
}
if (ty == 0) cnt1 = cnt;
else cnt2 = cnt;
return;
}
v2[p][q] = 1;
for (i =0; i < c[p][q][0].size(); i++) {
la[p][q] = i;
g(c[p][q][0][i], c[p][q][1][i]);
}
}
void g2(int p, int q, int w) {
int i;
if (v3[p][q]) return;
v3[p][q] = 1;
if (q == 0) l[p][ty] = w;
for (i = 0; i < c[p][q][0].size(); i++) {
g2(c[p][q][0][i], c[p][q][1][i], w + 1);
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
int i, j, t, t1, t2;
// freopen ("input.txt", "r", stdin);
n = N;
m = M;
st = P;
for (i = 0; i < m; i++) {
t1 = R[i][0];
t2 = R[i][1];
a[t1].push_back(t2);
a[t2].push_back(t1);
}
for (i = 0; i < n; i++) {
fi[i] = a[i][0];
if (a[i].size() == 1) se[i] = a[i][0];
else se[i] = a[i][1];
}
// for (i = 0; i < n; i++) {
// printf("%d %d\n", fi[i], se[i]);
// }
for (i = 0; i < n; i++) {
f(i, 0);
f(i, 1);
}
for (i = 0; i < n; i++) {
for (j = 0; j < 2; j++) {
c[b[i][j][0]][b[i][j][1]][0].push_back(i);
c[b[i][j][0]][b[i][j][1]][1].push_back(j);
// printf("%d %d %d %d\n", i, j, b[i][j][0], b[i][j][1]);
}
}
memset(l, -1, sizeof(l));
ty = 0;
cnt = 0;
g(st, 0);
g2(st, 0, 0);
memset(v2, 0, sizeof(v2));
memset(v3, 0, sizeof(v3));
ty = 1;
cnt = 0;
g(st, 1);
g2(st, 1, 0);
int anscnt;
for (j = 0; j < Q; j++) {
t = G[j];
anscnt = 0;
for (i = 0; i < n; i++) {
if (t == l[i][0] || l[i][0] != -1 && t > l[i][0] && cnt1 > 0 && (t - l[i][0]) % cnt1 == 0) anscnt++;
else if (t == l[i][1] || l[i][1] != -1 && t > l[i][1] && cnt2 > 0 && (t - l[i][1]) % cnt2 == 0) anscnt++;
}
answer(anscnt);
}
}
Compilation message
garden.cpp: In function 'void g(int, int)':
garden.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (i =0; i < c[p][q][0].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
garden.cpp: In function 'void g2(int, int, int)':
garden.cpp:58:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (i = 0; i < c[p][q][0].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:113:74: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
113 | if (t == l[i][0] || l[i][0] != -1 && t > l[i][0] && cnt1 > 0 && (t - l[i][0]) % cnt1 == 0) anscnt++;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
garden.cpp:114:79: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
114 | else if (t == l[i][1] || l[i][1] != -1 && t > l[i][1] && cnt2 > 0 && (t - l[i][1]) % cnt2 == 0) anscnt++;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19948 KB |
Output is correct |
2 |
Correct |
14 ms |
19948 KB |
Output is correct |
3 |
Correct |
13 ms |
19948 KB |
Output is correct |
4 |
Correct |
13 ms |
19692 KB |
Output is correct |
5 |
Correct |
13 ms |
19692 KB |
Output is correct |
6 |
Correct |
15 ms |
20204 KB |
Output is correct |
7 |
Correct |
13 ms |
19820 KB |
Output is correct |
8 |
Correct |
13 ms |
19948 KB |
Output is correct |
9 |
Correct |
15 ms |
20076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19948 KB |
Output is correct |
2 |
Correct |
14 ms |
19948 KB |
Output is correct |
3 |
Correct |
13 ms |
19948 KB |
Output is correct |
4 |
Correct |
13 ms |
19692 KB |
Output is correct |
5 |
Correct |
13 ms |
19692 KB |
Output is correct |
6 |
Correct |
15 ms |
20204 KB |
Output is correct |
7 |
Correct |
13 ms |
19820 KB |
Output is correct |
8 |
Correct |
13 ms |
19948 KB |
Output is correct |
9 |
Correct |
15 ms |
20076 KB |
Output is correct |
10 |
Correct |
13 ms |
19820 KB |
Output is correct |
11 |
Correct |
28 ms |
23404 KB |
Output is correct |
12 |
Correct |
46 ms |
25708 KB |
Output is correct |
13 |
Correct |
94 ms |
47468 KB |
Output is correct |
14 |
Correct |
160 ms |
38892 KB |
Output is correct |
15 |
Correct |
252 ms |
39356 KB |
Output is correct |
16 |
Correct |
174 ms |
34028 KB |
Output is correct |
17 |
Correct |
168 ms |
32108 KB |
Output is correct |
18 |
Correct |
49 ms |
25580 KB |
Output is correct |
19 |
Correct |
166 ms |
38856 KB |
Output is correct |
20 |
Correct |
251 ms |
39148 KB |
Output is correct |
21 |
Correct |
174 ms |
34028 KB |
Output is correct |
22 |
Correct |
160 ms |
31980 KB |
Output is correct |
23 |
Correct |
168 ms |
40556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19948 KB |
Output is correct |
2 |
Correct |
14 ms |
19948 KB |
Output is correct |
3 |
Correct |
13 ms |
19948 KB |
Output is correct |
4 |
Correct |
13 ms |
19692 KB |
Output is correct |
5 |
Correct |
13 ms |
19692 KB |
Output is correct |
6 |
Correct |
15 ms |
20204 KB |
Output is correct |
7 |
Correct |
13 ms |
19820 KB |
Output is correct |
8 |
Correct |
13 ms |
19948 KB |
Output is correct |
9 |
Correct |
15 ms |
20076 KB |
Output is correct |
10 |
Correct |
13 ms |
19820 KB |
Output is correct |
11 |
Correct |
28 ms |
23404 KB |
Output is correct |
12 |
Correct |
46 ms |
25708 KB |
Output is correct |
13 |
Correct |
94 ms |
47468 KB |
Output is correct |
14 |
Correct |
160 ms |
38892 KB |
Output is correct |
15 |
Correct |
252 ms |
39356 KB |
Output is correct |
16 |
Correct |
174 ms |
34028 KB |
Output is correct |
17 |
Correct |
168 ms |
32108 KB |
Output is correct |
18 |
Correct |
49 ms |
25580 KB |
Output is correct |
19 |
Correct |
166 ms |
38856 KB |
Output is correct |
20 |
Correct |
251 ms |
39148 KB |
Output is correct |
21 |
Correct |
174 ms |
34028 KB |
Output is correct |
22 |
Correct |
160 ms |
31980 KB |
Output is correct |
23 |
Correct |
168 ms |
40556 KB |
Output is correct |
24 |
Correct |
14 ms |
19820 KB |
Output is correct |
25 |
Correct |
130 ms |
23532 KB |
Output is correct |
26 |
Correct |
189 ms |
25580 KB |
Output is correct |
27 |
Correct |
2585 ms |
47468 KB |
Output is correct |
28 |
Correct |
1186 ms |
39020 KB |
Output is correct |
29 |
Correct |
2975 ms |
39328 KB |
Output is correct |
30 |
Correct |
1725 ms |
34156 KB |
Output is correct |
31 |
Correct |
1713 ms |
32064 KB |
Output is correct |
32 |
Correct |
187 ms |
25580 KB |
Output is correct |
33 |
Correct |
1236 ms |
40432 KB |
Output is correct |
34 |
Correct |
2964 ms |
40892 KB |
Output is correct |
35 |
Correct |
1809 ms |
35436 KB |
Output is correct |
36 |
Correct |
1742 ms |
33644 KB |
Output is correct |
37 |
Correct |
951 ms |
42348 KB |
Output is correct |
38 |
Correct |
2288 ms |
60528 KB |
Output is correct |