#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
#define int int64_t
using namespace std;
constexpr int maxn = 2 * 150000 + 10;
vector<int> grafo[maxn];
int n, m, f[maxn][2], x[maxn];
int p, k;
bool mark[maxn];
stack<int> st, cycle;
vector<int> mod[maxn][2];
int eh_ciclo[2];
inline void build_reverse_graph() {
for (int i = 0; i < 2*n; i++)
grafo[ x[i] ].push_back(i);
}
bool dfs(int u, int aux) {
mark[u] = 1;
st.push(u);
int v = x[u];
if (v == aux) {
while (!st.empty() && st.top() != aux) {
cycle.push(st.top());
st.pop();
}
if (!st.empty()) cycle.push(st.top()), st.pop();
return true;
}
if (mark[v])
return false;
return dfs(v, aux);
}
int check_cycle(int p) {
while (!st.empty()) st.pop();
while (!cycle.empty()) cycle.pop();
for (int i = 0; i < 2*n; i++)
mark[i] = 0;
return (dfs(p, p) ? cycle.size() : 0);
}
map<int, int> ans[2];
int dist[maxn];
void bfs(int u) {
for (int i = 0; i < 2*n; i++)
dist[i] = 0x3f3f3f3f, mark[i] = 0;
queue<int> fila;
fila.push(u);
dist[u] = 0;
while (!fila.empty()) {
int u = fila.front();
fila.pop();
if (mark[u]) continue;
mark[u] = true;
if (u < n)
ans[k][ dist[u] ]++;
for (auto v : grafo[u]) {
if (dist[v] > dist[u] + 1) {
dist[v] = dist[u] + 1;
fila.push(v);
}
}
}
}
void count_routes(int32_t N, int32_t M, int32_t P, int32_t R[][2], int32_t Q, int32_t G[]) {
memset(f, -1, sizeof f);
n = N, m = M;
p = P;
for (int i = 0; i < m; i++) {
int a, b;
a = R[i][0], b = R[i][1];
if (f[a][0] == -1) f[a][0] = b;
else if (f[a][1] == -1) f[a][1] = b;
if (f[b][0] == -1) f[b][0] = a;
else if (f[b][1] == -1) f[b][1] = a;
}
for (int u = 0; u < n; u++) {
if (f[u][1] == -1) f[u][1] = f[u][0];
int v = f[u][0];
if (f[v][0] == u)
x[u] = v+n;
else
x[u] = v;
}
for (int u = n; u < 2*n; u++) {
int u_ = u - n;
int v = f[u_][1];
if (f[v][0] == u_)
x[u] = v+n;
else
x[u] = v;
}
//for (int i = 0; i < 2*n; i++)
//cout << (i < n ? i : i-n) << (i < n ? " " : "' ") << (x[i] < n ? x[i] : x[i]-n)
//<< (x[i] < n ? "\n" : "'\n");
build_reverse_graph();
eh_ciclo[0] = check_cycle(p);
eh_ciclo[1] = check_cycle(p+n);
bfs(p);
k++;
bfs(p+n);
for (int l = 0; l < 2; l++) {
if (eh_ciclo[l]) {
for (auto & e : ans[l])
mod[e.first%eh_ciclo[l]][l].push_back(e.first);
}
}
for (int i = 0 ; i < Q; i++) {
int g = G[i];
int resp = 0;
for (int l = 0; l < 2; l++) {
if (eh_ciclo[l]) {
for (auto& e : mod[g%eh_ciclo[l]][l])
if (g >= e) resp += ans[l][e];
} else
resp += ans[l][g];
}
answer(resp);
}
}
//int main() {
//return 0;
//}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
26348 KB |
Output is correct |
2 |
Correct |
18 ms |
26348 KB |
Output is correct |
3 |
Correct |
18 ms |
26264 KB |
Output is correct |
4 |
Correct |
17 ms |
26220 KB |
Output is correct |
5 |
Correct |
19 ms |
26220 KB |
Output is correct |
6 |
Correct |
18 ms |
26348 KB |
Output is correct |
7 |
Correct |
18 ms |
26220 KB |
Output is correct |
8 |
Correct |
18 ms |
26220 KB |
Output is correct |
9 |
Correct |
19 ms |
26348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
26348 KB |
Output is correct |
2 |
Correct |
18 ms |
26348 KB |
Output is correct |
3 |
Correct |
18 ms |
26264 KB |
Output is correct |
4 |
Correct |
17 ms |
26220 KB |
Output is correct |
5 |
Correct |
19 ms |
26220 KB |
Output is correct |
6 |
Correct |
18 ms |
26348 KB |
Output is correct |
7 |
Correct |
18 ms |
26220 KB |
Output is correct |
8 |
Correct |
18 ms |
26220 KB |
Output is correct |
9 |
Correct |
19 ms |
26348 KB |
Output is correct |
10 |
Correct |
17 ms |
26220 KB |
Output is correct |
11 |
Correct |
33 ms |
28396 KB |
Output is correct |
12 |
Correct |
48 ms |
29420 KB |
Output is correct |
13 |
Correct |
124 ms |
60396 KB |
Output is correct |
14 |
Correct |
99 ms |
37072 KB |
Output is correct |
15 |
Correct |
131 ms |
39148 KB |
Output is correct |
16 |
Correct |
110 ms |
36076 KB |
Output is correct |
17 |
Correct |
110 ms |
34668 KB |
Output is correct |
18 |
Correct |
38 ms |
30060 KB |
Output is correct |
19 |
Correct |
96 ms |
38764 KB |
Output is correct |
20 |
Correct |
137 ms |
39148 KB |
Output is correct |
21 |
Correct |
113 ms |
35820 KB |
Output is correct |
22 |
Correct |
112 ms |
34668 KB |
Output is correct |
23 |
Correct |
101 ms |
40044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
26348 KB |
Output is correct |
2 |
Correct |
18 ms |
26348 KB |
Output is correct |
3 |
Correct |
18 ms |
26264 KB |
Output is correct |
4 |
Correct |
17 ms |
26220 KB |
Output is correct |
5 |
Correct |
19 ms |
26220 KB |
Output is correct |
6 |
Correct |
18 ms |
26348 KB |
Output is correct |
7 |
Correct |
18 ms |
26220 KB |
Output is correct |
8 |
Correct |
18 ms |
26220 KB |
Output is correct |
9 |
Correct |
19 ms |
26348 KB |
Output is correct |
10 |
Correct |
17 ms |
26220 KB |
Output is correct |
11 |
Correct |
33 ms |
28396 KB |
Output is correct |
12 |
Correct |
48 ms |
29420 KB |
Output is correct |
13 |
Correct |
124 ms |
60396 KB |
Output is correct |
14 |
Correct |
99 ms |
37072 KB |
Output is correct |
15 |
Correct |
131 ms |
39148 KB |
Output is correct |
16 |
Correct |
110 ms |
36076 KB |
Output is correct |
17 |
Correct |
110 ms |
34668 KB |
Output is correct |
18 |
Correct |
38 ms |
30060 KB |
Output is correct |
19 |
Correct |
96 ms |
38764 KB |
Output is correct |
20 |
Correct |
137 ms |
39148 KB |
Output is correct |
21 |
Correct |
113 ms |
35820 KB |
Output is correct |
22 |
Correct |
112 ms |
34668 KB |
Output is correct |
23 |
Correct |
101 ms |
40044 KB |
Output is correct |
24 |
Correct |
19 ms |
26220 KB |
Output is correct |
25 |
Correct |
28 ms |
28652 KB |
Output is correct |
26 |
Correct |
38 ms |
30060 KB |
Output is correct |
27 |
Correct |
132 ms |
61420 KB |
Output is correct |
28 |
Correct |
98 ms |
38892 KB |
Output is correct |
29 |
Correct |
169 ms |
39148 KB |
Output is correct |
30 |
Correct |
104 ms |
36204 KB |
Output is correct |
31 |
Correct |
89 ms |
34796 KB |
Output is correct |
32 |
Correct |
41 ms |
30316 KB |
Output is correct |
33 |
Correct |
97 ms |
38764 KB |
Output is correct |
34 |
Correct |
132 ms |
39148 KB |
Output is correct |
35 |
Correct |
105 ms |
36076 KB |
Output is correct |
36 |
Correct |
107 ms |
34904 KB |
Output is correct |
37 |
Correct |
115 ms |
40044 KB |
Output is correct |
38 |
Correct |
148 ms |
66028 KB |
Output is correct |