#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<
using namespace std;
void count_routes(int n, int m, int p, int r[][2], int q, int qrs[]) {
vector<vector<int>> to(n);
for (int i = 0; i < m; ++i) {
int a = r[i][0], b = r[i][1];
if ((int)to[a].size() < 2) to[a].emplace_back(b);
if ((int)to[b].size() < 2) to[b].emplace_back(a);
}
vector<int> nxt(2 * n, -1);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 2 && j < (int)to[i].size(); ++j) {
int x = to[i][j];
int node = (to[x][0] == i) * n + x;
nxt[j * n + i] = node;
}
}
for (int i = n; i < 2 * n; ++i) if (nxt[i] == -1) nxt[i] = nxt[i - n];
vector<vector<int>> adj(2 * n);
for (int i = 0; i < 2 * n; ++i) {
adj[nxt[i]].emplace_back(i);
}
vector<bool> vis(2 * n);
function<void(int, int , int&, vector<int>&)>
DFS = [&](int node, int stop, int &cycle_len, vector<int> &dist) {
if (vis[node]) {
return;
}
vis[node] = true;
for (int &x : adj[node]) {
if (x == stop) {
cycle_len = 1 + dist[node];
continue;
}
dist[x] = 1 + dist[node];
DFS(x, stop, cycle_len, dist);
}
};
const int INF = (int)1e9 + 10;
vector<int> cycle(2, INF);
vector<vector<int>> dist(2, vector<int>(2 * n, -1));
dist[0][p] = dist[1][p + n] = 0;
DFS(p, p, cycle[0], dist[0]);
fill(vis.begin(), vis.end(), false);
DFS(p + n, p + n, cycle[1], dist[1]);
vector<vector<int>> cnt_dist(2, vector<int>(2 * n));
for (int i = 0; i < 2; ++i) {
for (int node = 0; node < n; ++node) { // doar nodurile "bune" conteaza ca doar din ele pot incepe
if (dist[i][node] == -1) continue;
++cnt_dist[i][dist[i][node]];
}
}
for (int it = 0; it < q; ++it) {
int k = qrs[it];
int ans = 0;
for (int i = 0; i < 2; ++i) {
for (int d = k % cycle[i]; d <= n && d <= k; d += cycle[i])
ans += cnt_dist[i][d];
}
answer(ans);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
14 ms |
4944 KB |
Output is correct |
12 |
Correct |
31 ms |
7768 KB |
Output is correct |
13 |
Correct |
70 ms |
35036 KB |
Output is correct |
14 |
Correct |
130 ms |
26072 KB |
Output is correct |
15 |
Correct |
171 ms |
26444 KB |
Output is correct |
16 |
Correct |
113 ms |
18732 KB |
Output is correct |
17 |
Correct |
100 ms |
15776 KB |
Output is correct |
18 |
Correct |
31 ms |
7768 KB |
Output is correct |
19 |
Correct |
128 ms |
26072 KB |
Output is correct |
20 |
Correct |
160 ms |
26444 KB |
Output is correct |
21 |
Correct |
113 ms |
18476 KB |
Output is correct |
22 |
Correct |
105 ms |
15756 KB |
Output is correct |
23 |
Correct |
132 ms |
28900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
14 ms |
4944 KB |
Output is correct |
12 |
Correct |
31 ms |
7768 KB |
Output is correct |
13 |
Correct |
70 ms |
35036 KB |
Output is correct |
14 |
Correct |
130 ms |
26072 KB |
Output is correct |
15 |
Correct |
171 ms |
26444 KB |
Output is correct |
16 |
Correct |
113 ms |
18732 KB |
Output is correct |
17 |
Correct |
100 ms |
15776 KB |
Output is correct |
18 |
Correct |
31 ms |
7768 KB |
Output is correct |
19 |
Correct |
128 ms |
26072 KB |
Output is correct |
20 |
Correct |
160 ms |
26444 KB |
Output is correct |
21 |
Correct |
113 ms |
18476 KB |
Output is correct |
22 |
Correct |
105 ms |
15756 KB |
Output is correct |
23 |
Correct |
132 ms |
28900 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
15 ms |
4944 KB |
Output is correct |
26 |
Correct |
32 ms |
7760 KB |
Output is correct |
27 |
Correct |
73 ms |
35036 KB |
Output is correct |
28 |
Correct |
168 ms |
26204 KB |
Output is correct |
29 |
Correct |
199 ms |
26572 KB |
Output is correct |
30 |
Correct |
114 ms |
18732 KB |
Output is correct |
31 |
Correct |
109 ms |
15648 KB |
Output is correct |
32 |
Correct |
32 ms |
7760 KB |
Output is correct |
33 |
Correct |
165 ms |
26168 KB |
Output is correct |
34 |
Correct |
193 ms |
26444 KB |
Output is correct |
35 |
Correct |
115 ms |
18476 KB |
Output is correct |
36 |
Correct |
111 ms |
15776 KB |
Output is correct |
37 |
Correct |
131 ms |
29028 KB |
Output is correct |
38 |
Correct |
145 ms |
49256 KB |
Output is correct |