题解:P11108 [ROI 2023] 蜗牛与富士山 (Day 2)
About 本题
容易注意到,一个叶节点能否在不超过
代码
#include<bits/stdc++.h>
#define LoveFurina ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F(x, l, r) for(int x = l; x <= r; ++x)
#define B(x, r, l) for(int x = r; x >= l; --x)
#define ll long long
using namespace std;
const int N = 2e5 + 10;
int n, k, q;
struct Node {
int s[2]; // 0:左节点 1:右节点(方便处理是否同向)
} a[N];
int cnt[N];
inline int dfs(int u, int last, int tot) {
if (!a[u].s[0]) return cnt[u] = tot <= k;
int res = 0;
F (i, 0, 1) {
int &v = a[u].s[i];
res += dfs(v, i, tot + (u != 1 && i != last ? 1 : 0)); // 需注意从根节点出发走左右节点均不算作转向
}
return cnt[u] = res;
}
int main() {
LoveFurina;
cin >> n >> k >> q;
F (i, 1, n) {
int t; cin >> t;
if (t) cin >> a[i].s[0] >> a[i].s[1];
}
dfs(1, 0, 0);
while (q--) {
int u; cin >> u;
cout << cnt[u] << '\n';
}
return 0;
}
About 拓展 \tiny \text{(非进阶型可不看)}
- 当
k 不是全局固定而是对于每个询问独立且范围不变时,对于离线询问和在线询问分别有哪些做法呢?