# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
397243 | rocks03 | Tropical Garden (IOI11_garden) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define nl cout << "\n"
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXK = 30;
const int MAXN = 2e5+100;
int N, M, P, nxt[2][MAXK][MAXN], wei[2][MAXK][MAXN];
vector<pii> g[MAXN];
void build(){
rep(i, 0, N){
sort(all(g[i]), greater<pii>());
while(SZ(g[i]) > 2) g[i].pop_back();
if(SZ(g[i]) < 2) g[i].pb(g[i][0]);
nxt[0][0][i] = g[i][0].ss;
wei[0][0][i] = g[i][0].ff;
nxt[1][0][i] = g[i][1].ss;
wei[1][0][i] = g[i][1].ff;
}
rep(k, 0, MAXK - 1){
rep(i, 0, N){
int u;
u = nxt[0][k][i];
nxt[0][k + 1][i] = nxt[0][k][u];
wei[0][k + 1][i] = wei[0][k][u];
if(wei[0][k][i] == wei[0][0][u]){
nxt[0][k + 1][i] = nxt[1][k][u];
wei[0][k + 1][i] = wei[1][k][u];
}
u = nxt[1][k][i];
nxt[1][k + 1][i] = nxt[0][k][u];
wei[1][k + 1][i] = wei[0][k][u];
if(wei[1][k][i] == wei[0][0][u]){
nxt[1][k + 1][i] = nxt[1][k][u];
wei[1][k + 1][i] = wei[1][k][u];
}
}
}
}
int jump(int v, int K){
int w = -1;
per(k, MAXK - 1, 0){
if(K >> k & 1){
if(wei[0][0][v] != w){
w = wei[0][k][v];
v = nxt[0][k][v];
} else{
w = wei[1][k][v];
v = nxt[1][k][v];
}
}
}
return v;
}
int query(int K){
int ans = 0;
rep(i, 0, N){
if(jump(i, K) == P)
ans++;
}
return ans;
}
void solve(){
cin >> N >> M >> P;
per(m, M - 1, 0){
int u, v;
cin >> u >> v;
g[u].pb({m, v});
g[v].pb({m, u});
}
build();
int Q; cin >> Q;
rep(q, 0, Q){
int K; cin >> K;
cout << query(K) << "\n";
}
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
solve();
return 0;
}