#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "garden.h"
#include "gardenlib.h"
void BFS(VVI& gp, int s, VI& dist) {
int n = gp.size();
queue<int> q;
dist = VI(n, -1);
dist[s] = 0;
q.push(s);
while (q.size()) {
int u = q.front();
q.pop();
for (int v : gp[u]) if (v != s) {
dist[v] = dist[u]+1;
q.push(v);
}
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
int n = N;
int m = M;
VVI initgp(n);
rep(i, m) {
int u = R[i][0];
int v = R[i][1];
initgp[u].pb(v);
initgp[v].pb(u);
}
VI gp(2*n, -1);
rep(u, n) {int v;
v = initgp[u][0];
if (initgp[v].size() == 1 || initgp[v][0] != u) {
gp[u] = v;
} else {
gp[u] = v+n;
}
if (initgp[u].size() == 1) continue;
v = initgp[u][1];
if (initgp[v].size() == 1 || initgp[v][0] != u) {
gp[u+n] = v;
} else {
gp[u+n] = v+n;
}
}
auto val = [&](int u) {
if (u < n) return to_string(u);
return to_string(u-n) + "*";
};
VI cycle(2*n);
int lng = 0;
{
int u = P;
int v = P;
do {
u = gp[u];
v = gp[gp[v]];
} while (u != v);
do {
u = gp[u];
cycle[u] = true;
lng++;
} while (u != v);
}
VVI trans(2*n);
rep(u, 2*n) if (gp[u] != -1) trans[gp[u]].pb(u);
VI dist1, dist2;
BFS(trans, P, dist1);
BFS(trans, P+n, dist2);
rep(i, Q) { int k = G[i];
int ans = 0;
if (cycle[P]) {
rep(u, n) if (dist1[u] != -1) {
ans += (dist1[u]%lng == k%lng && k >= dist1[u]);
}
} else {
rep(u, n) {
ans += (dist1[u] == k);
}
}
if (cycle[P+n]) {
rep(u, n) if (dist2[u] != -1) {
ans += (dist2[u]%lng == k%lng && k >= dist2[u]);
}
} else {
rep(u, n) {
ans += (dist2[u] == k);
}
}
answer(ans);
}
}
Compilation message
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:78:10: warning: variable 'val' set but not used [-Wunused-but-set-variable]
78 | auto val = [&](int u) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |