#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
typedef long long ll;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
// void answer(int x) {
// cout << x << endl;
// }
void count_routes(int n, int m, int p, int r[][2], int q, int g[]) {
vvi adj(n);
vector<pair<int, int>> edges;
for (int i=0;i<m;i++) {
int u = r[i][0];
int v = r[i][1];
adj[u].push_back(v);
adj[v].push_back(u);
edges.push_back({u, v});
edges.push_back({v, u});
}
vi fmax(n, -1);
vi smax(n, -1);
for (int i=0;i<n;i++) {
fmax[i] = adj[i][0];
if (adj[i].size() > 1) smax[i] = adj[i][1];
}
int E = (int)edges.size();
map<pair<int, int>, int> lookup;
for (int i=0;i<E;i++) {
lookup[edges[i]] = i;
}
vi succ(E, -1);
for (int i=0;i<E;i++) {
auto [u, v] = edges[i];
if (smax[v] == -1 || fmax[v] != u) succ[i] = lookup[{v, fmax[v]}];
else succ[i] = lookup[{v, smax[v]}];
}
int goal = lookup[{p, fmax[p]}];
int goal2 = -1;
if (smax[p] != -1) goal2 = lookup[{p, smax[p]}];
// get cycle length
auto get_clen = [&](int start) {
int baby = start;
int giant = start;
int ans = 0;
do {
baby = succ[baby];
giant = succ[succ[giant]];
ans++;
} while (baby != giant);
return ans;
};
auto check_in_cycle = [&](int v, int clen) {
int w = v;
for (int i=0;i<clen;i++) w = succ[w];
return w == v;
};
int clen1 = get_clen(goal);
bool in_cycle1 = check_in_cycle(goal, clen1);
int clen2 = 1;
bool in_cycle2 = false;
if (goal2 != -1){
clen2 = get_clen(goal2);
in_cycle2 = check_in_cycle(goal2, clen2);
}
vvi radj(E);
for (int i=0;i<E;i++) radj[succ[i]].push_back(i);
auto get_closest = [&](int fin, bool cycle, int clen) {
vi dists(E, -1);
dists[fin] = 0;
queue<int> bfs;
bfs.push(fin);
while (!bfs.empty()) {
int cur = bfs.front(); bfs.pop();
for (int w : radj[cur]) {
if (dists[w] != -1) continue;
dists[w] = dists[cur] + 1;
bfs.push(w);
}
}
vi ans(E+1, 0);
for (int i=0;i<n;i++) {
int e = lookup[{i, fmax[i]}];
int d = dists[e];
if (d >= 0) ans[d]++;
}
if (cycle) {
for (int i=clen;i<=E;i++) ans[i] += ans[i-clen];
}
return ans;
};
vi freq1 = get_closest(goal, in_cycle1, clen1);
vi freq2;
if (goal2 != -1) {
freq2 = get_closest(goal2, in_cycle2, clen2);
}
auto sub_query = [&](int k, vi& freq, bool cycle, int clen) {
if (k > E) {
if (!cycle) return 0;
k %= clen;
k += ((E - k)/clen) * clen;
}
return freq[k];
};
auto do_query = [&](int k) {
int ans = 0;
ans += sub_query(k, freq1, in_cycle1, clen1);
if (goal2 != -1) ans += sub_query(k, freq2, in_cycle2, clen2);
return ans;
};
vi G(g, g+q);
for (int k : G) {
answer(do_query(k));
}
}
// int main() {
// std::ios::sync_with_stdio(false);
// std::cin.tie(NULL);
// {
// int N = 6;
// int M = 6;
// int P = 0;
// int R[][2] = {
// {1, 2},
// {0, 1},
// {0, 3},
// {3, 4},
// {4, 5},
// {1, 5}
// };
// int Q = 1;
// int G[] = {3};
// count_routes(N, M, P, R, Q, G);
// }
// cout << "-----------" << endl;
// {
// int N = 5;
// int M = 5;
// int P = 2;
// int R[][2] = {
// {1, 0},
// {1, 2},
// {3, 2},
// {1, 3},
// {4, 2}
// };
// int Q = 2;
// int G[] = {3, 1};
// count_routes(N, M, P, R, Q, G);
// }
// return 0;
// }
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
10 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
10 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
27 ms |
8140 KB |
Output is correct |
12 |
Correct |
83 ms |
17352 KB |
Output is correct |
13 |
Correct |
102 ms |
31424 KB |
Output is correct |
14 |
Correct |
298 ms |
49592 KB |
Output is correct |
15 |
Correct |
303 ms |
51388 KB |
Output is correct |
16 |
Correct |
253 ms |
44476 KB |
Output is correct |
17 |
Correct |
305 ms |
42776 KB |
Output is correct |
18 |
Correct |
72 ms |
17280 KB |
Output is correct |
19 |
Correct |
275 ms |
49732 KB |
Output is correct |
20 |
Correct |
281 ms |
51384 KB |
Output is correct |
21 |
Correct |
276 ms |
44240 KB |
Output is correct |
22 |
Correct |
279 ms |
42936 KB |
Output is correct |
23 |
Correct |
321 ms |
52924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
10 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
27 ms |
8140 KB |
Output is correct |
12 |
Correct |
83 ms |
17352 KB |
Output is correct |
13 |
Correct |
102 ms |
31424 KB |
Output is correct |
14 |
Correct |
298 ms |
49592 KB |
Output is correct |
15 |
Correct |
303 ms |
51388 KB |
Output is correct |
16 |
Correct |
253 ms |
44476 KB |
Output is correct |
17 |
Correct |
305 ms |
42776 KB |
Output is correct |
18 |
Correct |
72 ms |
17280 KB |
Output is correct |
19 |
Correct |
275 ms |
49732 KB |
Output is correct |
20 |
Correct |
281 ms |
51384 KB |
Output is correct |
21 |
Correct |
276 ms |
44240 KB |
Output is correct |
22 |
Correct |
279 ms |
42936 KB |
Output is correct |
23 |
Correct |
321 ms |
52924 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
24 ms |
8404 KB |
Output is correct |
26 |
Correct |
81 ms |
17352 KB |
Output is correct |
27 |
Correct |
102 ms |
31424 KB |
Output is correct |
28 |
Correct |
311 ms |
49584 KB |
Output is correct |
29 |
Correct |
338 ms |
51644 KB |
Output is correct |
30 |
Correct |
295 ms |
44224 KB |
Output is correct |
31 |
Correct |
291 ms |
42940 KB |
Output is correct |
32 |
Correct |
79 ms |
17396 KB |
Output is correct |
33 |
Correct |
245 ms |
49704 KB |
Output is correct |
34 |
Correct |
273 ms |
51396 KB |
Output is correct |
35 |
Correct |
286 ms |
44252 KB |
Output is correct |
36 |
Correct |
260 ms |
42804 KB |
Output is correct |
37 |
Correct |
291 ms |
52928 KB |
Output is correct |
38 |
Correct |
324 ms |
54968 KB |
Output is correct |