#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];
int d[2][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 < n; ++i) {
if (st[p] <= st[i] && ed[i] <= ed[p]) {
d[0][i] = h[i] - h[p];
}
if (st[p + n] <= st[i] && ed[i] <= ed[p + n]) {
d[1][i] = h[i] - h[p + 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 (X[i] == d[0][j] || X[i] == d[1][j]) ++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 |
24 ms |
21680 KB |
Output is correct |
2 |
Correct |
23 ms |
21628 KB |
Output is correct |
3 |
Correct |
21 ms |
21752 KB |
Output is correct |
4 |
Correct |
22 ms |
21488 KB |
Output is correct |
5 |
Correct |
22 ms |
21596 KB |
Output is correct |
6 |
Correct |
24 ms |
21768 KB |
Output is correct |
7 |
Correct |
22 ms |
21496 KB |
Output is correct |
8 |
Correct |
22 ms |
21652 KB |
Output is correct |
9 |
Correct |
25 ms |
21752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
21680 KB |
Output is correct |
2 |
Correct |
23 ms |
21628 KB |
Output is correct |
3 |
Correct |
21 ms |
21752 KB |
Output is correct |
4 |
Correct |
22 ms |
21488 KB |
Output is correct |
5 |
Correct |
22 ms |
21596 KB |
Output is correct |
6 |
Correct |
24 ms |
21768 KB |
Output is correct |
7 |
Correct |
22 ms |
21496 KB |
Output is correct |
8 |
Correct |
22 ms |
21652 KB |
Output is correct |
9 |
Correct |
25 ms |
21752 KB |
Output is correct |
10 |
Correct |
21 ms |
21496 KB |
Output is correct |
11 |
Correct |
36 ms |
24724 KB |
Output is correct |
12 |
Correct |
56 ms |
26260 KB |
Output is correct |
13 |
Correct |
70 ms |
35676 KB |
Output is correct |
14 |
Correct |
189 ms |
38824 KB |
Output is correct |
15 |
Correct |
185 ms |
39112 KB |
Output is correct |
16 |
Correct |
134 ms |
33000 KB |
Output is correct |
17 |
Correct |
132 ms |
31592 KB |
Output is correct |
18 |
Correct |
62 ms |
26616 KB |
Output is correct |
19 |
Correct |
187 ms |
39992 KB |
Output is correct |
20 |
Correct |
191 ms |
40160 KB |
Output is correct |
21 |
Correct |
151 ms |
33916 KB |
Output is correct |
22 |
Correct |
130 ms |
31616 KB |
Output is correct |
23 |
Correct |
183 ms |
40452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
21680 KB |
Output is correct |
2 |
Correct |
23 ms |
21628 KB |
Output is correct |
3 |
Correct |
21 ms |
21752 KB |
Output is correct |
4 |
Correct |
22 ms |
21488 KB |
Output is correct |
5 |
Correct |
22 ms |
21596 KB |
Output is correct |
6 |
Correct |
24 ms |
21768 KB |
Output is correct |
7 |
Correct |
22 ms |
21496 KB |
Output is correct |
8 |
Correct |
22 ms |
21652 KB |
Output is correct |
9 |
Correct |
25 ms |
21752 KB |
Output is correct |
10 |
Correct |
21 ms |
21496 KB |
Output is correct |
11 |
Correct |
36 ms |
24724 KB |
Output is correct |
12 |
Correct |
56 ms |
26260 KB |
Output is correct |
13 |
Correct |
70 ms |
35676 KB |
Output is correct |
14 |
Correct |
189 ms |
38824 KB |
Output is correct |
15 |
Correct |
185 ms |
39112 KB |
Output is correct |
16 |
Correct |
134 ms |
33000 KB |
Output is correct |
17 |
Correct |
132 ms |
31592 KB |
Output is correct |
18 |
Correct |
62 ms |
26616 KB |
Output is correct |
19 |
Correct |
187 ms |
39992 KB |
Output is correct |
20 |
Correct |
191 ms |
40160 KB |
Output is correct |
21 |
Correct |
151 ms |
33916 KB |
Output is correct |
22 |
Correct |
130 ms |
31616 KB |
Output is correct |
23 |
Correct |
183 ms |
40452 KB |
Output is correct |
24 |
Correct |
29 ms |
21584 KB |
Output is correct |
25 |
Correct |
603 ms |
24824 KB |
Output is correct |
26 |
Correct |
997 ms |
26388 KB |
Output is correct |
27 |
Correct |
2178 ms |
35704 KB |
Output is correct |
28 |
Correct |
3608 ms |
38876 KB |
Output is correct |
29 |
Correct |
3820 ms |
39048 KB |
Output is correct |
30 |
Correct |
2436 ms |
33152 KB |
Output is correct |
31 |
Correct |
2236 ms |
31976 KB |
Output is correct |
32 |
Correct |
997 ms |
26600 KB |
Output is correct |
33 |
Correct |
3591 ms |
38836 KB |
Output is correct |
34 |
Correct |
3821 ms |
39176 KB |
Output is correct |
35 |
Correct |
2411 ms |
33552 KB |
Output is correct |
36 |
Correct |
2176 ms |
31724 KB |
Output is correct |
37 |
Correct |
3997 ms |
40484 KB |
Output is correct |
38 |
Correct |
3844 ms |
45892 KB |
Output is correct |