#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
#ifdef tabr
void answer(int n) {
cout << n << '\n';
}
#else
#include "garden.h"
#include "gardenlib.h"
#endif
void count_routes(int n, int m, int p, int r[][2], int q, int k[]) {
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
g[r[i][0]].emplace_back(r[i][1]);
g[r[i][1]].emplace_back(r[i][0]);
}
vector<int> nxt(2 * n);
for (int i = 0; i < n; i++) {
nxt[i] = g[i][0];
nxt[i + n] = (g[i].size() == 1 ? g[i][0] : g[i][1]);
if (g[nxt[i]][0] == i) {
nxt[i] += n;
}
if (g[nxt[i + n]][0] == i) {
nxt[i + n] += n;
}
}
g = vector<vector<int>>(2 * n);
for (int i = 0; i < 2 * n; i++) {
g[nxt[i]].emplace_back(i);
}
vector<int> a(2 * n, -1), b(2 * n, -1);
a[p] = 0;
b[p + n] = 0;
queue<int> que;
que.emplace(p);
while (!que.empty()) {
int v = que.front();
que.pop();
for (int to : g[v]) {
if (a[to] == -1) {
a[to] = a[v] + 1;
que.emplace(to);
}
}
}
que.emplace(p + n);
while (!que.empty()) {
int v = que.front();
que.pop();
for (int to : g[v]) {
if (b[to] == -1) {
b[to] = b[v] + 1;
que.emplace(to);
}
}
}
int c = 0;
int v = p;
do {
v = nxt[v];
c++;
} while (v != p && c < 2 * n + 1);
int d = 0;
v = p + n;
do {
v = nxt[v];
d++;
} while (v != p + n && d < 2 * n + 1);
debug(c, d);
for (int i = 0; i < q; i++) {
int ans = 0;
for (int j = 0; j < n; j++) {
if (c > 2 * n) {
if (k[i] == a[j]) {
ans++;
}
} else {
if (a[j] != -1 && k[i] >= a[j] && (k[i] - a[j]) % c == 0) {
ans++;
}
}
if (d > 2 * n) {
if (k[i] == b[j]) {
ans++;
}
} else {
if (b[j] != -1 && k[i] >= b[j] && (k[i] - b[j]) % d == 0) {
ans++;
}
}
}
answer(ans);
}
}
#ifdef tabr
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, p, q;
cin >> n >> m >> p >> q;
int r[100][2];
int k[100];
for (int i = 0; i < m; i++) {
cin >> r[i][0] >> r[i][1];
}
for (int i = 0; i < q; i++) {
cin >> k[i];
}
count_routes(n, m, p, r, q, k);
return 0;
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
0 ms |
284 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
0 ms |
284 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
11 ms |
3036 KB |
Output is correct |
12 |
Correct |
26 ms |
5040 KB |
Output is correct |
13 |
Correct |
42 ms |
12288 KB |
Output is correct |
14 |
Correct |
89 ms |
16368 KB |
Output is correct |
15 |
Correct |
129 ms |
16672 KB |
Output is correct |
16 |
Correct |
93 ms |
11952 KB |
Output is correct |
17 |
Correct |
75 ms |
10424 KB |
Output is correct |
18 |
Correct |
23 ms |
5040 KB |
Output is correct |
19 |
Correct |
127 ms |
16392 KB |
Output is correct |
20 |
Correct |
121 ms |
16676 KB |
Output is correct |
21 |
Correct |
82 ms |
11888 KB |
Output is correct |
22 |
Correct |
80 ms |
10420 KB |
Output is correct |
23 |
Correct |
128 ms |
18144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
0 ms |
284 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
11 ms |
3036 KB |
Output is correct |
12 |
Correct |
26 ms |
5040 KB |
Output is correct |
13 |
Correct |
42 ms |
12288 KB |
Output is correct |
14 |
Correct |
89 ms |
16368 KB |
Output is correct |
15 |
Correct |
129 ms |
16672 KB |
Output is correct |
16 |
Correct |
93 ms |
11952 KB |
Output is correct |
17 |
Correct |
75 ms |
10424 KB |
Output is correct |
18 |
Correct |
23 ms |
5040 KB |
Output is correct |
19 |
Correct |
127 ms |
16392 KB |
Output is correct |
20 |
Correct |
121 ms |
16676 KB |
Output is correct |
21 |
Correct |
82 ms |
11888 KB |
Output is correct |
22 |
Correct |
80 ms |
10420 KB |
Output is correct |
23 |
Correct |
128 ms |
18144 KB |
Output is correct |
24 |
Correct |
1 ms |
284 KB |
Output is correct |
25 |
Correct |
105 ms |
3104 KB |
Output is correct |
26 |
Correct |
147 ms |
4992 KB |
Output is correct |
27 |
Correct |
2304 ms |
12392 KB |
Output is correct |
28 |
Correct |
865 ms |
18116 KB |
Output is correct |
29 |
Correct |
2560 ms |
18396 KB |
Output is correct |
30 |
Correct |
1457 ms |
13452 KB |
Output is correct |
31 |
Correct |
1449 ms |
12024 KB |
Output is correct |
32 |
Correct |
129 ms |
5544 KB |
Output is correct |
33 |
Correct |
876 ms |
18032 KB |
Output is correct |
34 |
Correct |
2569 ms |
18396 KB |
Output is correct |
35 |
Correct |
1551 ms |
13356 KB |
Output is correct |
36 |
Correct |
1435 ms |
11864 KB |
Output is correct |
37 |
Correct |
767 ms |
19784 KB |
Output is correct |
38 |
Correct |
2072 ms |
23404 KB |
Output is correct |