Nowcoder城市网络 树上倍增
Posted kaka0010
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nowcoder城市网络 树上倍增相关的知识,希望对你有一定的参考价值。
原题链接:https://ac.nowcoder.com/acm/problem/13331
题意
有一棵树,树上每个节点都有一个权值,1为根节点。每次选择两个节点u,v保证v在u通往根节点的路径上,每次从u出发,身上携带价格为w的珠宝,每次遇到权值比你大的节点就可以交换一次,问一共会交换多少次。
分析
看到这类问题一般就会往倍增上面思考,难点就是如何找出第一个大于你权值的祖先节点。在一个序列上我们知道用单调栈来实现,在树上其实可以用更简单的倍增解决
- 如果父节点就比你大,直接将f[x][0]=fa
- 如果父节点不满足,将当前节点设为fa,然后一直倍增找到第一个大于你的点
思路是比较好想的,实现起来会有一些细节需要处理。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ul;
typedef pair<int, int> PII;
const int inf = 0x3f3f3f3f;
const int N = 5e5 + 10;
const int M = 1e6 + 10;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
#define lowbit(i) (i & -i)
#define Debug(x) cout << (x) << endl
#define fi first
#define se second
#define mem memset
#define endl '\\n'
int a[N], f[N][21], dep[N];
vector<int> g[N];
void dfs(int x, int fa) {
dep[x] = dep[fa] + 1;
if (a[fa] > a[x]) f[x][0] = fa;
else {
int now = fa;
for (int i = 20; ~i; i--) {
if (f[now][i] && a[f[now][i]] <= a[x]) {
now = f[now][i];
}
}
f[x][0] = f[now][0];
}
for (int i = 1; i <= 20; i++) f[x][i] = f[f[x][i-1]][i-1];
for (auto v : g[x]) {
if (v == fa) continue;
dfs(v, x);
}
}
inline void solve() {
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n-1; i++) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
while (m--) {
int u, v, w;
cin >> u >> v >> w;
int now;
if (a[u] > w) {
now = u;
} else {
now = u;
for (int i = 20; ~i; i--) {
if (f[now][i] && a[f[now][i]] <= w) {
now = f[now][i];
}
}
if (dep[f[now][0]] < dep[v]) {
cout << 0 << endl;
continue;
}
now = f[now][0];
}
int ans = 1;
for (int i = 20; ~i; i--) {
if (f[now][i] && dep[f[now][i]] >= dep[v]) {
ans += (1 << i);
now = f[now][i];
}
}
cout << ans << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
signed test_index_for_debug = 1;
char acm_local_for_debug = 0;
do {
if (acm_local_for_debug == '$') exit(0);
if (test_index_for_debug > 20)
throw runtime_error("Check the stdin!!!");
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
solve();
#endif
return 0;
}
以上是关于Nowcoder城市网络 树上倍增的主要内容,如果未能解决你的问题,请参考以下文章