#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int inf = 1e9;
const int MxN = 2e5 + 10;
const int MxL = 32;
int n, m, p, q, ans[MxN], g[MxN];
pii up[MxN][MxL][2];
vector<pii> adj[MxN];
int cnt = -1;
pii dfs(int u, int l, int op) {
if (l == 0) {
return up[u][l][op] = pii(adj[u][op].second, u);
}
if (up[u][l][op] != pii(-1, -1)) return up[u][l][op];
pii cur = pii(u, -1);
cur = dfs(cur.first, l - 1, op);
int new_op = 0;
if (cur.second == adj[cur.first][0].second) new_op = 1;
cur = dfs(cur.first, l - 1, new_op);
return up[u][l][op] = cur;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
n = N, m = M, p = P;
for (int i = 0; i < M; i++) {
int u = R[i][0];
int v = R[i][1];
adj[u].push_back({i, v});
adj[v].push_back({i, u});
}
q = Q;
for (int i = 0; i < Q; i++) {
g[i] = G[i];
}
for (int i = 0; i < n; i++) {
sort(adj[i].begin(), adj[i].end());
if (adj[i].size() == 1) adj[i].push_back({-1, adj[i][0].second});
adj[i].resize(2);
}
for (int j = 0; j < MxL; j++) {
for (int i = 0; i < n; i++) {
up[i][j][0] = up[i][j][1] = pii(-1, -1);
dfs(i, j, 0); dfs(i, j, 1);
}
}
for (int i = 0; i < q; i++) {
int ans = 0;
for (int j = 0; j < n; j++) {
pii cur = pii(j, -1);
int op = 0;
for (int k = 0; k < MxL; k++) {
if (g[i] & (1 << k)) {
cur = up[cur.first][k][op];
if (cur.second == adj[cur.first][0].second) op = 1;
else op = 0;
}
}
if (cur.first == p) ans++;
}
answer(ans);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8784 KB |
Output is correct |
2 |
Correct |
2 ms |
8784 KB |
Output is correct |
3 |
Correct |
3 ms |
8784 KB |
Output is correct |
4 |
Correct |
2 ms |
8784 KB |
Output is correct |
5 |
Correct |
2 ms |
8784 KB |
Output is correct |
6 |
Correct |
3 ms |
8784 KB |
Output is correct |
7 |
Correct |
2 ms |
8784 KB |
Output is correct |
8 |
Correct |
2 ms |
8784 KB |
Output is correct |
9 |
Correct |
6 ms |
9064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8784 KB |
Output is correct |
2 |
Correct |
2 ms |
8784 KB |
Output is correct |
3 |
Correct |
3 ms |
8784 KB |
Output is correct |
4 |
Correct |
2 ms |
8784 KB |
Output is correct |
5 |
Correct |
2 ms |
8784 KB |
Output is correct |
6 |
Correct |
3 ms |
8784 KB |
Output is correct |
7 |
Correct |
2 ms |
8784 KB |
Output is correct |
8 |
Correct |
2 ms |
8784 KB |
Output is correct |
9 |
Correct |
6 ms |
9064 KB |
Output is correct |
10 |
Correct |
2 ms |
8784 KB |
Output is correct |
11 |
Correct |
44 ms |
19892 KB |
Output is correct |
12 |
Correct |
70 ms |
31232 KB |
Output is correct |
13 |
Correct |
285 ms |
56400 KB |
Output is correct |
14 |
Correct |
285 ms |
83792 KB |
Output is correct |
15 |
Correct |
248 ms |
85576 KB |
Output is correct |
16 |
Correct |
203 ms |
62024 KB |
Output is correct |
17 |
Correct |
132 ms |
53816 KB |
Output is correct |
18 |
Correct |
53 ms |
31584 KB |
Output is correct |
19 |
Correct |
263 ms |
85372 KB |
Output is correct |
20 |
Correct |
248 ms |
85720 KB |
Output is correct |
21 |
Correct |
188 ms |
62024 KB |
Output is correct |
22 |
Correct |
128 ms |
53592 KB |
Output is correct |
23 |
Correct |
350 ms |
92128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8784 KB |
Output is correct |
2 |
Correct |
2 ms |
8784 KB |
Output is correct |
3 |
Correct |
3 ms |
8784 KB |
Output is correct |
4 |
Correct |
2 ms |
8784 KB |
Output is correct |
5 |
Correct |
2 ms |
8784 KB |
Output is correct |
6 |
Correct |
3 ms |
8784 KB |
Output is correct |
7 |
Correct |
2 ms |
8784 KB |
Output is correct |
8 |
Correct |
2 ms |
8784 KB |
Output is correct |
9 |
Correct |
6 ms |
9064 KB |
Output is correct |
10 |
Correct |
2 ms |
8784 KB |
Output is correct |
11 |
Correct |
44 ms |
19892 KB |
Output is correct |
12 |
Correct |
70 ms |
31232 KB |
Output is correct |
13 |
Correct |
285 ms |
56400 KB |
Output is correct |
14 |
Correct |
285 ms |
83792 KB |
Output is correct |
15 |
Correct |
248 ms |
85576 KB |
Output is correct |
16 |
Correct |
203 ms |
62024 KB |
Output is correct |
17 |
Correct |
132 ms |
53816 KB |
Output is correct |
18 |
Correct |
53 ms |
31584 KB |
Output is correct |
19 |
Correct |
263 ms |
85372 KB |
Output is correct |
20 |
Correct |
248 ms |
85720 KB |
Output is correct |
21 |
Correct |
188 ms |
62024 KB |
Output is correct |
22 |
Correct |
128 ms |
53592 KB |
Output is correct |
23 |
Correct |
350 ms |
92128 KB |
Output is correct |
24 |
Correct |
10 ms |
8784 KB |
Output is correct |
25 |
Execution timed out |
5081 ms |
20084 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |