#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
const int LOG = 31, INF = (int) 1e9;
vector<vector<int>> edges(N);
for (int i = 0; i < M; ++i) {
edges[R[i][0]].push_back(R[i][1]);
edges[R[i][1]].push_back(R[i][0]);
}
vector<int> next(2 * N, -1);
for (int i = 0; i < N; ++i) {
int x = edges[i][0];
next[2 * i] = (i == edges[x][0] ? 2 * x + 1 : 2 * x);
x = ((int) edges[i].size() == 1 ? edges[i][0] : edges[i][1]);
next[2 * i + 1] = (i == edges[x][0] ? 2 * x + 1 : 2 * x);
}
vector<vector<int>> pred(2 * N);
for (int i = 0; i < 2 * N; ++i) {
pred[next[i]].push_back(i);
}
vector<bool> visited(2 * N, false), active(2 * N, false);
vector<int> st, cyc_sz(2 * N, 0), cyc_dist(2 * N, INF), elem(2 * N, -1),
cyc_root(2 * N, -1), cyc_id(2 * N, -1);
int cycles = -1;
function<void(int)> dfs = [&](int node) {
if (active[node]) {
++cycles;
for (int i = 0; i < (int) st.size(); ++i) {
cyc_id[st[i]] = i;
cyc_sz[st[i]] = (int) st.size();
cyc_dist[st[i]] = 0;
cyc_root[st[i]] = st[i];
elem[st[i]] = cycles;
}
return;
}
visited[node] = true;
active[node] = true;
st.push_back(node);
for (auto x : pred[node]) {
dfs(x);
}
active[node] = false;
st.pop_back();
};
function<void(int, int)> tree_dfs = [&](int node, int root) -> void {
for (auto x : pred[node]) {
if (cyc_dist[x] == 0) continue;
cyc_dist[x] = cyc_dist[node] + 1;
cyc_root[x] = root;
elem[x] = elem[root];
tree_dfs(x, root);
}
};
for (int i = 0; i < N; ++i) {
if (!visited[2 * i]) {
dfs(2 * i);
}
}
for (int i = 0; i < 2 * N; ++i) {
if (cyc_dist[i] == 0) {
tree_dfs(i, i);
}
}
for (int j = 0; j < Q; ++j) {
int k = G[j], cnt = 0;
for (int i = 0; i < N; ++i) {
int node = 2 * i;
if (elem[node] != elem[2 * P]) continue;
int dist = 0;
if (cyc_dist[node] != 0) {
dist += cyc_dist[node];
node = cyc_root[node];
}
for (int p = 2 * P; p <= 2 * P + 1; ++p) {
int pos1 = cyc_id[p], pos2 = cyc_id[node];
int d = dist + (pos1 > pos2 ? pos1 - pos2 : cyc_sz[node] - pos2 + pos1);
if (d >= k && d % cyc_sz[node] == k % cyc_sz[node]) ++cnt;
}
}
answer(cnt);
}
}
Compilation message
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:7:13: warning: unused variable 'LOG' [-Wunused-variable]
7 | const int LOG = 31, INF = (int) 1e9;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |