#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
vector<vector<int>> adj(N);
for (int i = 0; i < M; ++i) {
adj[R[i][0]].push_back(R[i][1]);
adj[R[i][1]].push_back(R[i][0]);
}
set<pair<int,int>> nodes;
for (int i = 0; i < N; ++i) {
nodes.insert({-1, i});
for (int v : adj[i]) nodes.insert({i, v});
}
int siz = nodes.size();
vector<bool> inP(siz, false);
map<pair<int,int>,int> idx;
int idx_ = 0;
for (auto node : nodes) {
idx[node] = idx_;
if (node.second == P) inP[idx_] = true;
++idx_;
}
nodes.clear();
const int J = 29;
vector<vector<int>> succ(J+1, vector<int>(siz, -1));
for (int i = 0; i < N; ++i) {
succ[0][idx[{-1, i}]] = idx[{i, adj[i][0]}];
for (int v : adj[i]) {
if (v != adj[i][0] or adj[i].size() == 1)
succ[0][idx[{v, i}]] = idx[{i, adj[i][0]}];
else
succ[0][idx[{v, i}]] = idx[{i, adj[i][1]}];
}
}
for (int j = 1; j <= J; ++j) {
for (int u = 0; u < siz; ++u) {
succ[j][u] = succ[j-1][ succ[j-1][u] ];
}
}
for(int it = 0; it < Q; ++it) {
int K = G[it];
int cnt = 0;
for (int i = 0; i < N; ++i) {
int u = idx[{-1, i}];
for (int j = J; j >= 0; --j) {
if (K & (1 << j)) u = succ[j][u];
}
if (inP[u]) ++cnt;
}
answer(cnt);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
1368 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
9 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
1368 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
9 ms |
3676 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
36 ms |
16632 KB |
Output is correct |
12 |
Correct |
96 ms |
32236 KB |
Output is correct |
13 |
Correct |
164 ms |
55692 KB |
Output is correct |
14 |
Correct |
358 ms |
90816 KB |
Output is correct |
15 |
Correct |
395 ms |
93604 KB |
Output is correct |
16 |
Correct |
309 ms |
78188 KB |
Output is correct |
17 |
Correct |
301 ms |
74592 KB |
Output is correct |
18 |
Correct |
100 ms |
32084 KB |
Output is correct |
19 |
Correct |
352 ms |
90684 KB |
Output is correct |
20 |
Correct |
360 ms |
93484 KB |
Output is correct |
21 |
Correct |
320 ms |
78036 KB |
Output is correct |
22 |
Correct |
333 ms |
74576 KB |
Output is correct |
23 |
Correct |
400 ms |
97416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
1368 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
9 ms |
3676 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
36 ms |
16632 KB |
Output is correct |
12 |
Correct |
96 ms |
32236 KB |
Output is correct |
13 |
Correct |
164 ms |
55692 KB |
Output is correct |
14 |
Correct |
358 ms |
90816 KB |
Output is correct |
15 |
Correct |
395 ms |
93604 KB |
Output is correct |
16 |
Correct |
309 ms |
78188 KB |
Output is correct |
17 |
Correct |
301 ms |
74592 KB |
Output is correct |
18 |
Correct |
100 ms |
32084 KB |
Output is correct |
19 |
Correct |
352 ms |
90684 KB |
Output is correct |
20 |
Correct |
360 ms |
93484 KB |
Output is correct |
21 |
Correct |
320 ms |
78036 KB |
Output is correct |
22 |
Correct |
333 ms |
74576 KB |
Output is correct |
23 |
Correct |
400 ms |
97416 KB |
Output is correct |
24 |
Correct |
10 ms |
344 KB |
Output is correct |
25 |
Correct |
4881 ms |
16720 KB |
Output is correct |
26 |
Execution timed out |
5047 ms |
32080 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |