#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,O3")
#pragma GCC target("avx,avx2")
#include "garden.h"
#include "gardenlib.h"
using namespace std;
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
const int INF = (int) 1e9;
vector<vector<int>> edges(N);
for (int i = 0; i < M; ++i) {
edges[R[i][0]].push_back(R[i][1]);
edges[R[i][1]].push_back(R[i][0]);
}
vector<int> next(2 * N, -1);
for (int i = 0; i < N; ++i) {
int x = edges[i][0];
next[2 * i] = (i == edges[x][0] ? 2 * x + 1 : 2 * x);
x = ((int) edges[i].size() == 1 ? edges[i][0] : edges[i][1]);
next[2 * i + 1] = (i == edges[x][0] ? 2 * x + 1 : 2 * x);
}
vector<vector<int>> pred(2 * N);
for (int i = 0; i < 2 * N; ++i) {
pred[next[i]].push_back(i);
}
vector<bool> visited(2 * N, false), active(2 * N, false);
vector<int> st, cyc_sz(2 * N, 0), cyc_dist(2 * N, INF), elem(2 * N, -1),
cyc_root(2 * N, -1), cyc_id(2 * N, -1);
int cycles = -1, start = 0;
function<void(int)> dfs = [&](int node) {
if (active[node]) {
++cycles;
int sz = (int) st.size();
int bgn = 0;
while (st[bgn] != node) {
++bgn;
}
sz -= bgn;
for (int i = bgn; i < (int) st.size(); ++i) {
cyc_id[st[i]] = i - bgn;
cyc_sz[st[i]] = sz;
cyc_dist[st[i]] = 0;
cyc_root[st[i]] = st[i];
elem[st[i]] = cycles;
}
}
if (visited[node]) {
return;
}
visited[node] = true;
active[node] = true;
st.push_back(node);
dfs(next[node]);
active[node] = false;
st.pop_back();
};
vector<vector<bool>> anc(Q, vector<bool>(2 * N, false));
bool p2 = false, p2_1 = false;
function<void(int, int)> tree_dfs = [&](int node, int root) -> void {
if (node == 2 * P) {
p2 = true;
}
if (node == 2 * P + 1) {
p2_1 = true;
}
if (p2) {
int p_dist = cyc_dist[node] - cyc_dist[2 * P];
for (int i = 0; i < Q; ++i) {
if (G[i] == p_dist) {
anc[i][node] = true;
}
}
}
if (p2_1) {
int p_dist = cyc_dist[node] - cyc_dist[2 * P + 1];
for (int i = 0; i < Q; ++i) {
if (G[i] == p_dist) {
anc[i][node] = true;
}
}
}
for (auto x : pred[node]) {
cyc_dist[x] = cyc_dist[node] + 1;
elem[x] = elem[root];
cyc_root[x] = cyc_root[node];
tree_dfs(x, root);
}
if (node == 2 * P) {
p2 = false;
}
if (node == 2 * P + 1) {
p2_1 = false;
}
};
for (start = 0; start < 2 * N; start += 2) {
if (!visited[start]) {
dfs(start);
}
}
for (int i = 0; i < 2 * N; ++i) {
if (cyc_dist[i] == INF && cyc_dist[next[i]] == 0) {
cyc_dist[i] = 1;
cyc_root[i] = next[i];
elem[i] = elem[next[i]];
tree_dfs(i, i);
}
}
for (int j = 0; j < Q; ++j) {
int k = G[j], cnt = 0;
for (int i = 0; i < N; ++i) {
if (anc[j][2 * i]) {
++cnt;
}
for (int p = 2 * P; p <= 2 * P + 1; ++p) {
int node = 2 * i, dist = 0;
if (elem[node] != elem[p]) continue;
if (cyc_dist[p] != 0) {
continue;
} else if (cyc_dist[node] != 0) {
dist += cyc_dist[node];
node = cyc_root[node];
}
dist += (cyc_id[p] >= cyc_id[node] ? cyc_id[p] - cyc_id[node]
: cyc_sz[node] - cyc_id[node] + cyc_id[p]);
if (k >= dist && dist % cyc_sz[node] == k % cyc_sz[node]) {
++cnt;
}
}
}
answer(cnt);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
704 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
10 ms |
5460 KB |
Output is correct |
12 |
Correct |
22 ms |
8916 KB |
Output is correct |
13 |
Correct |
41 ms |
36800 KB |
Output is correct |
14 |
Correct |
100 ms |
27980 KB |
Output is correct |
15 |
Correct |
94 ms |
28488 KB |
Output is correct |
16 |
Correct |
91 ms |
20556 KB |
Output is correct |
17 |
Correct |
85 ms |
17612 KB |
Output is correct |
18 |
Correct |
28 ms |
8396 KB |
Output is correct |
19 |
Correct |
84 ms |
28012 KB |
Output is correct |
20 |
Correct |
110 ms |
28484 KB |
Output is correct |
21 |
Correct |
67 ms |
20372 KB |
Output is correct |
22 |
Correct |
55 ms |
17580 KB |
Output is correct |
23 |
Correct |
100 ms |
31264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
704 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
10 ms |
5460 KB |
Output is correct |
12 |
Correct |
22 ms |
8916 KB |
Output is correct |
13 |
Correct |
41 ms |
36800 KB |
Output is correct |
14 |
Correct |
100 ms |
27980 KB |
Output is correct |
15 |
Correct |
94 ms |
28488 KB |
Output is correct |
16 |
Correct |
91 ms |
20556 KB |
Output is correct |
17 |
Correct |
85 ms |
17612 KB |
Output is correct |
18 |
Correct |
28 ms |
8396 KB |
Output is correct |
19 |
Correct |
84 ms |
28012 KB |
Output is correct |
20 |
Correct |
110 ms |
28484 KB |
Output is correct |
21 |
Correct |
67 ms |
20372 KB |
Output is correct |
22 |
Correct |
55 ms |
17580 KB |
Output is correct |
23 |
Correct |
100 ms |
31264 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
101 ms |
16724 KB |
Output is correct |
26 |
Correct |
145 ms |
27380 KB |
Output is correct |
27 |
Correct |
3544 ms |
78648 KB |
Output is correct |
28 |
Correct |
997 ms |
94176 KB |
Output is correct |
29 |
Correct |
3432 ms |
95396 KB |
Output is correct |
30 |
Correct |
2102 ms |
65484 KB |
Output is correct |
31 |
Correct |
1943 ms |
55232 KB |
Output is correct |
32 |
Correct |
351 ms |
26912 KB |
Output is correct |
33 |
Correct |
998 ms |
94172 KB |
Output is correct |
34 |
Correct |
3483 ms |
95380 KB |
Output is correct |
35 |
Correct |
2216 ms |
65244 KB |
Output is correct |
36 |
Correct |
1999 ms |
55212 KB |
Output is correct |
37 |
Correct |
848 ms |
104892 KB |
Output is correct |
38 |
Correct |
3129 ms |
123448 KB |
Output is correct |