#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int u, v;
Edge() {}
Edge(int u_, int v_) : u(u_), v(v_) {}
};
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});
}
map<pair<int,int>,int> idx;
map<int,pair<int,int>> inv;
int idx_ = 0;
for (auto node : nodes) {
idx[node] = idx_;
inv[idx_] = node;
++idx_;
}
int siz = nodes.size();
const int J = 20;
vector<vector<int>> succ(siz, vector<int>(J, -1));
for (int i = 0; i < N; ++i) {
succ[idx[{-1, i}]][0] = idx[{i, adj[i][0]}];
for (int v : adj[i]) {
if (v != adj[i][0])
succ[idx[{v, i}]][0] = idx[{i, adj[i][0]}];
else
succ[idx[{v, i}]][0] = idx[{i, adj[i][1]}];
}
}
for (int j = 1; j < J; ++j) {
for (int u = 0; u < siz; ++u) {
succ[u][j] = succ[ succ[u][j-1] ][j-1];
}
}
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}];
int k = K;
for (int j = J-1; j >= 0; --j) {
if (k & (1 << j)) {
k -= 1 << j;
u = succ[u][j];
}
}
auto node = inv[u];
if (node.second == P) ++cnt;
}
answer(cnt);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
2 ms |
1116 KB |
Output is correct |
3 |
Correct |
3 ms |
1372 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1884 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
1372 KB |
Output is correct |
9 |
Correct |
13 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
2 ms |
1116 KB |
Output is correct |
3 |
Correct |
3 ms |
1372 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1884 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
1372 KB |
Output is correct |
9 |
Correct |
13 ms |
5468 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
61 ms |
23680 KB |
Output is correct |
12 |
Incorrect |
172 ms |
47092 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
2 ms |
1116 KB |
Output is correct |
3 |
Correct |
3 ms |
1372 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1884 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
1372 KB |
Output is correct |
9 |
Correct |
13 ms |
5468 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
61 ms |
23680 KB |
Output is correct |
12 |
Incorrect |
172 ms |
47092 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |