#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int T, st[N], ed[N], h[N], in[N];
int e[N], nxt[N], par[N], deg[N], pos[N];
vector<int> vec[N], cir[N], G[N];
int find(int u) { return u == par[u] ? u : par[u] = find(par[u]); }
void join(int u, int v) { par[find(u)] = find(v); }
void dfs(int u, int r) {
st[u] = ++T, in[u] = r;
for (auto v : G[u]) {
if (deg[v]) continue;
h[v] = h[u] + 1, dfs(v, r);
}
ed[u] = ++T;
}
void solve(int root) {
if (vec[root].size() == 1) return;
for (auto i : vec[root]) {
deg[nxt[i]]++, G[nxt[i]].push_back(i);
}
queue<int> qu;
for (auto i : vec[root]) if (!deg[i]) qu.push(i);
while (qu.size()) {
int u = qu.front(); qu.pop();
deg[nxt[u]]--;
if (!deg[nxt[u]]) qu.push(nxt[u]);
}
for (auto i : vec[root]) {
if (!deg[i]) continue;
int cur = i;
while (1) {
pos[cur] = cir[root].size();
cir[root].push_back(cur);
cur = nxt[cur];
if (cur == i) break;
}
break;
}
for (auto i : cir[root]) dfs(i, i);
}
void count_routes(int n, int m, int p, int R[][2], int q, int X[]) {
for (int i = 0; i < n * 2; ++i) e[i] = nxt[i] = -1;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < 2; ++j) {
int u = R[i][j];
if (e[u] == -1) e[u] = i;
else if (e[u + n] == -1) e[u + n] = i;
}
}
for (int i = 0; i < n; ++i) {
int j;
j = R[e[i]][0] + R[e[i]][1] - i;
if (e[j] == e[i]) {
if (e[j + n] == -1) nxt[i] = j;
else nxt[i] = j + n;
}
else nxt[i] = j;
if (e[i + n] != -1) {
j = R[e[i + n]][0] + R[e[i + n]][1] - i;
if (e[j] == e[i + n]) {
if (e[j + n] == -1) nxt[i + n] = j;
else nxt[i + n] = j + n;
}
else nxt[i + n] = j;
}
}
for (int i = 0; i < n * 2; ++i) par[i] = i;
for (int i = 0; i < n * 2; ++i) if (nxt[i] != -1) join(i, nxt[i]);
for (int i = 0; i < n * 2; ++i) vec[find(i)].push_back(i);
for (int i = 0; i < n * 2; ++i) if (par[i] == i) solve(i);
// for (int i = 0; i < n * 2; ++i) cout << nxt[i] << ' '; cout << '\n';
for (int i = 0; i < q; ++i) {
int res = 0;
for (int j = 0; j < n; ++j) {
// int cur = j, tmp = X[i];
// while (tmp--) cur = nxt[cur];
// if (cur == p || cur == p + n) res++;
if (h[j] >= X[i]) {
if (par[j] == par[p] && st[p] <= st[j] && ed[j] <= ed[p] && h[j] - h[p] == X[i]) ++res;
else if (par[j] == par[p + n] && st[p + n] <= st[j] && ed[j] <= ed[p + n] && h[j] - h[p + n] == X[i]) ++res;
}
else {
int tmp = cir[par[j]][(pos[in[j]] + X[i] - h[j]) % cir[par[j]].size()];
if (tmp == p || tmp == p + n) res++;
}
}
answer(res);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
21724 KB |
Output is correct |
2 |
Correct |
22 ms |
21628 KB |
Output is correct |
3 |
Correct |
22 ms |
21724 KB |
Output is correct |
4 |
Correct |
21 ms |
21500 KB |
Output is correct |
5 |
Correct |
27 ms |
21468 KB |
Output is correct |
6 |
Correct |
30 ms |
21812 KB |
Output is correct |
7 |
Correct |
22 ms |
21496 KB |
Output is correct |
8 |
Correct |
23 ms |
21592 KB |
Output is correct |
9 |
Correct |
24 ms |
21728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
21724 KB |
Output is correct |
2 |
Correct |
22 ms |
21628 KB |
Output is correct |
3 |
Correct |
22 ms |
21724 KB |
Output is correct |
4 |
Correct |
21 ms |
21500 KB |
Output is correct |
5 |
Correct |
27 ms |
21468 KB |
Output is correct |
6 |
Correct |
30 ms |
21812 KB |
Output is correct |
7 |
Correct |
22 ms |
21496 KB |
Output is correct |
8 |
Correct |
23 ms |
21592 KB |
Output is correct |
9 |
Correct |
24 ms |
21728 KB |
Output is correct |
10 |
Correct |
22 ms |
21472 KB |
Output is correct |
11 |
Correct |
35 ms |
24680 KB |
Output is correct |
12 |
Correct |
63 ms |
26696 KB |
Output is correct |
13 |
Correct |
70 ms |
36208 KB |
Output is correct |
14 |
Correct |
192 ms |
39008 KB |
Output is correct |
15 |
Correct |
189 ms |
39188 KB |
Output is correct |
16 |
Correct |
135 ms |
33632 KB |
Output is correct |
17 |
Correct |
128 ms |
31688 KB |
Output is correct |
18 |
Correct |
61 ms |
26660 KB |
Output is correct |
19 |
Correct |
184 ms |
38620 KB |
Output is correct |
20 |
Correct |
185 ms |
38760 KB |
Output is correct |
21 |
Correct |
148 ms |
33652 KB |
Output is correct |
22 |
Correct |
134 ms |
32384 KB |
Output is correct |
23 |
Correct |
197 ms |
41704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
21724 KB |
Output is correct |
2 |
Correct |
22 ms |
21628 KB |
Output is correct |
3 |
Correct |
22 ms |
21724 KB |
Output is correct |
4 |
Correct |
21 ms |
21500 KB |
Output is correct |
5 |
Correct |
27 ms |
21468 KB |
Output is correct |
6 |
Correct |
30 ms |
21812 KB |
Output is correct |
7 |
Correct |
22 ms |
21496 KB |
Output is correct |
8 |
Correct |
23 ms |
21592 KB |
Output is correct |
9 |
Correct |
24 ms |
21728 KB |
Output is correct |
10 |
Correct |
22 ms |
21472 KB |
Output is correct |
11 |
Correct |
35 ms |
24680 KB |
Output is correct |
12 |
Correct |
63 ms |
26696 KB |
Output is correct |
13 |
Correct |
70 ms |
36208 KB |
Output is correct |
14 |
Correct |
192 ms |
39008 KB |
Output is correct |
15 |
Correct |
189 ms |
39188 KB |
Output is correct |
16 |
Correct |
135 ms |
33632 KB |
Output is correct |
17 |
Correct |
128 ms |
31688 KB |
Output is correct |
18 |
Correct |
61 ms |
26660 KB |
Output is correct |
19 |
Correct |
184 ms |
38620 KB |
Output is correct |
20 |
Correct |
185 ms |
38760 KB |
Output is correct |
21 |
Correct |
148 ms |
33652 KB |
Output is correct |
22 |
Correct |
134 ms |
32384 KB |
Output is correct |
23 |
Correct |
197 ms |
41704 KB |
Output is correct |
24 |
Correct |
26 ms |
21576 KB |
Output is correct |
25 |
Correct |
619 ms |
24988 KB |
Output is correct |
26 |
Correct |
1100 ms |
26956 KB |
Output is correct |
27 |
Correct |
2266 ms |
36692 KB |
Output is correct |
28 |
Correct |
3713 ms |
39860 KB |
Output is correct |
29 |
Correct |
3947 ms |
39772 KB |
Output is correct |
30 |
Correct |
2488 ms |
34196 KB |
Output is correct |
31 |
Correct |
2251 ms |
32364 KB |
Output is correct |
32 |
Correct |
1057 ms |
26896 KB |
Output is correct |
33 |
Correct |
3805 ms |
39328 KB |
Output is correct |
34 |
Correct |
3941 ms |
39528 KB |
Output is correct |
35 |
Correct |
2499 ms |
34128 KB |
Output is correct |
36 |
Correct |
2233 ms |
32312 KB |
Output is correct |
37 |
Correct |
4144 ms |
41320 KB |
Output is correct |
38 |
Correct |
3959 ms |
45924 KB |
Output is correct |