#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 150069;
const int K = 30;
int up[2 * N][K], vis[N], n, nxt[2 * N], deg[2 * N];
vector<int> t[N];
void dfs(int v) {
vis[v] = 1;
if (t[t[v][0]][0] == v && (int)t[t[v][0]].size() > 1)
nxt[v] = n + t[v][0], deg[n + t[v][0]]++;
else
nxt[v] = t[v][0], deg[t[v][0]]++;
if (t[v].size() > 1) {
if (t[t[v][1]][0] == v && (int)t[t[v][1]].size() > 1)
nxt[n + v] = n + t[v][1], deg[n + t[v][1]]++;
else
nxt[n + v] = t[v][1], deg[t[v][1]]++;
}
for (auto u : t[v]) {
if (!vis[u])
dfs(u);
}
}
void build(int v) {
vis[v] = 1;
up[v][0] = nxt[v];
if (nxt[v] != -1 && !vis[nxt[v]]) {
build(nxt[v]);
for (int i = 1;i < K;i++)
up[v][i] = up[up[v][i - 1]][i - 1];
}
}
void count_routes(int n1, int m, int p, int r[][2], int q, int k[])
{
++p;
n = n1;
for (int i = 0;i < m;i++) {
r[i][0]++, r[i][1]++;
t[r[i][0]].push_back(r[i][1]);
t[r[i][1]].push_back(r[i][0]);
}
for (int i = 1;i <= 2 * n;i++)
nxt[i] = -1;
for (int i = 1;i <= n;i++) {
if (!vis[i])
dfs(i);
}
//graf ima 2 * N cvorova!
for (int i = 1;i <= n;i++)
vis[i] = 0;
vector<int> ord;
queue<int> que;
for (int i = 1;i <= 2 * n;i++) {
if (deg[i] == 0)
que.push(i);
}
while (!que.empty()) {
auto v = que.front();
ord.push_back(v);
que.pop();
if (nxt[v] != -1) {
deg[nxt[v]]--;
if (deg[nxt[v]] == 0)
que.push(nxt[v]);
}
}
for (auto u : ord) {
if (!vis[u])
build(u);
}
for (int i = 0;i < q;i++) {
int ans = 0;
for (int j = 1;j <= n;j++) {
int d = j;
for (int l = 0;l < K;l++) {
if ((k[i] >> l & 1) && d != -1)
d = up[d][l];
}
ans += (d == p || d == n + p);
}
answer(ans);
}
}
/*
6 6 0
1 2
0 1
0 3
3 4
4 5
1 5
1
3
2
5 5 2
1 0
1 2
3 2
1 3
4 2
2
3 1
1 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
10840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
10840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
10840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |