#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
using namespace std;
int n, m, p, ans = 0;
vector<int> adj[150005], radj[300005];
int succ[300005], vis1[300005], vis2[300005], dep1[300005], dep2[300005], cyc1 = -1, cyc2 = -1;
void dfs1(int i, int st, int d) {
if (vis1[i] && st == i && cyc1 == -1) {
cyc1 = d;
return;
}
if (vis1[i]) {
return;
}
vis1[i] = 1;
dep1[i] = d;
for (int j : radj[i]) {
dfs1(j, st, d+1);
}
}
void dfs2(int i, int st, int d) {
if (vis2[i] && st == i && cyc2 == -1) {
cyc2 = d;
return;
}
if (vis2[i]) {
return;
}
vis2[i] = 1;
dep2[i] = d;
for (int j : radj[i]) {
dfs2(j, st, d+1);
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
n = N, m = M, p = P;
for (int i = 0; i < m; i++) {
if (adj[R[i][0]].size() < 2) {
adj[R[i][0]].push_back(R[i][1]);
}
if (adj[R[i][1]].size() < 2) {
adj[R[i][1]].push_back(R[i][0]);
}
}
for (int i = 0; i < n; i++) {
if (adj[i].size() == 1) {
if (i == adj[adj[i][0]][0]) {
succ[i] = adj[i][0]+n;
} else {
succ[i] = adj[i][0];
}
succ[i+n] = succ[i];
} else {
if (i == adj[adj[i][0]][0]) {
succ[i] = adj[i][0]+n;
} else {
succ[i] = adj[i][0];
}
if (i == adj[adj[i][1]][0]) {
succ[i+n] = adj[i][1]+n;
} else {
succ[i+n] = adj[i][1];
}
}
}
for (int i = 0; i < 2*n; i++) {
radj[succ[i]].push_back(i);
}
dfs1(p, p, 0);
dfs2(p+n, p+n, 0);
for (int j = 0; j < Q; j++) {
ans = 0;
for (int i = 0; i < n; i++) {
bool f1 = 0, f2 = 0;
int k = G[j];
if (vis1[i]) {
k -= dep1[i];
if (k == 0) {
f1 = 1;
} else if (cyc1 != -1 && k % cyc1 == 0) {
f1 = 1;
}
}
k = G[j];
if (vis2[i]) {
k -= dep2[i];
if (k == 0) {
f2 = 1;
} else if (cyc2 != -1 && k % cyc2 == 0) {
f2 = 1;
}
}
if (f1 || f2) {
ans++;
}
}
answer(ans);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
11140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
11140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
11140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |