#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cstring>
#include <cmath>
#include <functional>
#include <cassert>
#include <iomanip>
#include <numeric>
#include <bitset>
#include <sstream>
#include <chrono>
#include <random>
#define ff first
#define ss second
#define PB push_back
#define sz(x) int(x.size())
#define rsz resize
#define fch(xxx, yyy) for (auto xxx : yyy) // abusive notation
#define all(x) (x).begin(),(x).end()
#define eps 1e-9
// more abusive notation (use at your own risk):
// #define int ll
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
// debugging
void __print(int x) {std::cerr << x;}
void __print(ll x) {std::cerr << x;} /* remember to uncomment this when not using THE MACRO */
void __print(unsigned x) {std::cerr << x;}
void __print(ull x) {std::cerr << x;}
void __print(float x) {std::cerr << x;}
void __print(double x) {std::cerr << x;}
void __print(ld x) {std::cerr << x;}
void __print(char x) {std::cerr << '\'' << x << '\'';}
void __print(const char *x) {std::cerr << '\"' << x << '\"';}
void __print(const string& x) {std::cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V> void __print(const pair<T, V> &x) {std::cerr << '{'; __print(x.ff); std::cerr << ", "; __print(x.ss); std::cerr << '}';}
template<typename T> void __print(const T& x) {int f = 0; std::cerr << '{'; for (auto &i: x) std::cerr << (f++ ? ", " : ""), __print(i); std::cerr << "}";}
void _print() {std::cerr << "]\n";}
template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) std::cerr << ", "; _print(v...);}
void println() {std::cerr << ">--------------------<" << endl;}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// templates
template <class T> bool ckmin(T &a, const T &b) {return b<a ? a = b, 1 : 0;}
template <class T> bool ckmax(T &a, const T &b) {return b>a ? a = b, 1 : 0;}
template <class T> using gr = greater<T>;
template <class T> using vc = vector<T>;
template <class T> using p_q = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vc<T>, gr<T>>;
template <class T1, class T2> using pr = pair<T1, T2>;
mt19937_64 rng_ll(chrono::steady_clock::now().time_since_epoch().count());
int rng(int M) {return (int)(rng_ll()%M);} /*returns any random number in [0, M) */
// const variables
constexpr int INF = (int)2e9;
constexpr int MOD = 998244353;
constexpr ll LL_INF = (ll)3e18;
constexpr int mod = (int)1e9 + 7;
constexpr ll inverse = 500000004LL; // inverse of 2 modulo 1e9 + 7
void setIO(const string& str) {// fast input/output
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (str.empty()) return;
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
//#include "gardenlib.h"
//#include "garden.h"
struct DSU {
int N;
vi parent, siz;
vc<array<int, 2>> change;
DSU() {}
DSU(int n) { // assuming ONE INDEXING
N = n;
parent.clear(), siz.clear();
parent.rsz(N+1), siz.rsz(N+1, 1);
iota(1 + all(parent), 1);
}
int find_set(int v) {
if (parent[v] == v) return v;
else return parent[v] = find_set(parent[v]); // LogN
// if you want O(alpha(N)) implementation, change this to parent[v] = find_set(parent[v])
}
bool unite(int a, int b) {
a = find_set(a), b = find_set(b);
if (a == b) return false;
if (siz[a] < siz[b]) swap(a, b);
parent[b] = a;
siz[a] += siz[b];
change.PB({a, b});
return true;
}
void roll_back() {
array<int, 2> p = change.back();
change.pop_back();
parent[p[1]] = p[1];
siz[p[0]] -= siz[p[1]];
}
bool connected(int a, int b) {
return find_set(a) == find_set(b);
}
bool connected() {
return siz[find_set(1)] == N;
}
};
int N, M, P, Q, L, pp;
vc<vc<pii>> adj;
vi f, beauty, vst, dp, deg;
// 2 * i -> most beautiful
// 2 * i + 1 -> second most beautiful
vc<vi> adj_;
vc<pii> queries;
vi ans;
DSU d;
bool in_cycle = false;
void answer_2005() {
//debug(dp);
if (!in_cycle) {
vi cnt(2 * N, 0);
for (int i = 0; i < N; ++i) {
if (i == pp || dp[2 * i] == INF) continue;
//debug(i);
cnt[dp[2 * i]]++;
}
//debug(cnt);
for (int i = 0; i < Q; ++i) {
if (queries[i].ff > N - 1) ans[queries[i].ss] = 0;
else ans[queries[i].ss] += cnt[queries[i].ff];
}
} else {
vi cnt(L, 0);
vi order(N);
iota(all(order), 0);
sort(all(order), [&](const int& a, const int& b) {
return dp[2 * a] < dp[2 * b];
});
int ptr = 0;
for (int i = 0; i < Q; ++i) {
while (ptr < N && dp[2 * order[ptr]] <= queries[i].ff) {
cnt[dp[2 * order[ptr]]%L]++;
ptr++;
}
ans[queries[i].ss] += cnt[queries[i].ff % L];
}
}
}
void construction() {
d = DSU(2 * N);
adj_.rsz(2 * N), f.rsz(2 * N);
for (int i = 0; i < N; ++i) {
//debug(i);
set<pii> st;
fch(u, adj[i]) {
st.insert({u.ss, u.ff});
}
//debug(adj[i]);
if (sz(st) == 1) {
int u = (*st.begin()).ss, w = (*st.begin()).ff;
f[2 * i] = f[2 * i + 1] = 2 * u + (w == beauty[u]);
} else if (sz(st) >= 2) {
int u1 = (*st.begin()).ss, u2 = (*next(st.begin())).ss;
int w1 = (*st.begin()).ff, w2 = (*next(st.begin())).ff;
f[2 * i] = 2 * u1 + (w1 == beauty[u1]);
f[2 * i + 1] = 2 * u2 + (w2 == beauty[u2]);
}
//debug(f[2 * i], f[2 * i + 1]);
}
for (int i = 0; i < 2 * N; ++i) {
adj_[f[i]].PB(i);
//cerr << i << ' ' << f[i] << '\n';
d.unite(f[i], i);
}
}
void compute() {
in_cycle = false;
vst.clear(), vst.rsz(2 * N, 0), dp.clear(), dp.rsz(2 * N, INF);
for (int i = 0; i < 2 * N; ++i) {
if (!vst[d.find_set(i)]) {
vst[d.find_set(i)] = 1;
int t = f[i];
int h = f[f[i]];
while (t != h) {
t = f[t];
h = f[f[h]];
}
L = 1;
if (t == P) in_cycle = true;
t = f[t];
while (t != h) {
L++;
if (t == P) {
in_cycle = true;
break;
}
t = f[t];
}
if (in_cycle) {
break;
}
}
}
vst.clear(), vst.rsz(2 * N, 0);
queue<int> q;
q.push(P);
dp[P] = 0;
vst[P] = 1;
while (!q.empty()) {
int v = q.front();
q.pop();
fch(u, adj_[v]) {
if (!vst[u]) {
vst[u] = 1;
dp[u] = dp[v] + 1;
q.push(u);
}
}
}
}
void count_routes(int n, int m, int p, int R[][2], int q, int g[]) {
N = n, M = m, P = p, Q = q;
adj.rsz(N), queries.rsz(Q), beauty.rsz(N, M), ans.rsz(Q), deg.rsz(N, 0);
for (int i = 0; i < M; ++i) {
int a = R[i][0], b = R[i][1];
ckmin(beauty[a], i);
ckmin(beauty[b], i);
adj[a].PB({b, i});
adj[b].PB({a, i});
deg[a]++, deg[b]++;
}
for (int i = 0; i < N; ++i) {
if (deg[i] == 1) {
beauty[i] = -1;
}
}
for (int i = 0; i < Q; ++i) {
queries[i] = {g[i], i};
}
sort(all(queries));
construction();
pp = P;
P = 2 * P;
//debug(P);
compute();
answer_2005();
P = 2 * (P >> 1) + 1;
//debug(P);
compute();
answer_2005();
for (int i = 0; i < Q; ++i) {
answer(ans[i]);
}
}
/*
int r[100005][2];
int g[100005];
signed main() { // TIME YOURSELF !!!
setIO("");
int n, m, p, q;
cin >> n >> m >> p >> q;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
r[i][0] = a, r[i][1] = b;
}
for (int i = 0; i < q; ++i) {
cin >> g[i];
}
count_routes(n, m, p, r, q, g);
return 0;
}*/
// TLE -> TRY NOT USING DEFINE INT LONG LONG
// CE -> CHECK LINE 45
// 5000 * 5000 size matrices are kinda big (potential mle)
// Do something, start simpler
Compilation message
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:282:9: error: 'answer' was not declared in this scope
282 | answer(ans[i]);
| ^~~~~~
garden.cpp: In function 'void setIO(const string&)':
garden.cpp:88:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen((str + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
garden.cpp:89:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen((str + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~