cf 739B Alyona and a tree

本文探讨了在树形数据结构中利用二分搜索寻找控制祖先的问题,并通过树上差分技术高效更新路径上的节点计数,实现O(logn)复杂度的解决方案。

一 原题

http://codeforces.com/contest/739/problem/B

二 分析

题意:给你一棵树,每个顶点和每条边都有一个权值。我们称点u控制点v,如果它们之间满足如下两个条件:1)v在以u为根节点的子树中,2)从u到v的简单路径上边的权重和dist(u, v)不超过v点的权重。顶点范围2e5,权值大小1-1e9

一个顶点s到它的各个祖先的距离是有序的,很容易想到可以二分找到最远能控制它的祖先t。找到后要做的事就是把从s的父亲到t路径上的点都ans++,直接操作是O(n)的。问题转换为在树上给定一条简单路径,如果高效的更新这条路径上所有的点。这是裸的树上差分操作。

三 代码

/*=====================================
*   
*   AUTHOR : maxkibble
*   CREATED: 2018.09.30 12:28:24
*
=====================================*/


#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, long long> pil;
typedef pair<long long, int> pli;

const int maxn = 2e5 + 5;

int n;
int fa[maxn];
ll a[maxn];
vector<pil> t[maxn];
ll d[maxn];
vector<pli> tr;
int cnt[maxn];

void dfs1(int cur, int pre, ll len) {
  d[cur] = d[pre] + len;
  auto ptr = lower_bound(tr.begin(), tr.end(), make_pair(d[cur] - a[cur], 0));
  if (ptr != tr.end()) {
    // differential operation here
    cnt[fa[ptr->se]]--;
    cnt[tr[tr.size() - 1].se]++;
  }
  tr.pb({d[cur], cur});
  for (auto nxt: t[cur]) {
    if (nxt.fi == pre) continue;
    dfs1(nxt.fi, cur, nxt.se);
  }
  tr.pop_back();
}

void dfs2(int cur, int pre) {
  for (auto nxt: t[cur]) {
    if (nxt.fi == pre) continue;
    dfs2(nxt.fi, cur);
    cnt[cur] += cnt[nxt.fi];
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 2; i <= n; i++) {
    int p;
    ll w;
    cin >> p >> w;
    fa[i] = p;
    t[i].pb({p, w});
    t[p].pb({i, w});
  }
  dfs1(1, 0, 0);
  dfs2(1, 0);
  for (int i = 1; i <= n; i++) cout << cnt[i] << " ";
  cout << endl;
  return 0;
}

 

#include<iostream> #include<vector> #define int long long using namespace std; int n,a[200005],fath[200005][31],dis[200005][31]; int cnt[200005],ans[200005]; vector <int> tree[200005]; void dfs(int now) { ans[now] = cnt[now]; for(auto i : tree[now]) { dfs(i); ans[now] += ans[i]; } return; } signed main() { cin >> n; for(int i = 1;i <= n;i++) cin >> a[i]; for(int i = 2;i <= n;i++) { cin >> fath[i][0] >> dis[i][0]; tree[fath[i][0]].push_back(i); } for(int j = 1;j <= 30;j++) { for(int i = 1;i <= n;i++) { if(fath[i][j - 1] != 1) { fath[i][j] = fath[fath[i][j - 1]][j - 1]; dis[i][j] = dis[fath[i][j - 1]][j - 1] + dis[i][j - 1]; } else { dis[i][j] = -1; } } } for(int i = 2;i <= n;i++) { int now = i,away = 0; for(int j = 30;j >= 0;j--) { if(dis[now][j] != -1 && away + dis[i][j] <= a[i]) away += dis[i][j],now = fath[now][j]; } cnt[fath[i][0]]++; cnt[fath[now][0]]--; } dfs(1); for(int i = 1;i <= n;i++) cout << ans[i] << ' '; } # CF739B Alyona and a tree ## 题目描述 Alyona有一棵有 $n$ 个节点的树。这棵树的根节点是 $1$。在每个节点里,Alyona写了一个正整数,在节点 $i$ 她写了正整数 $a_i$ 。另外,她在这棵树上的每条边上写了一个正整数(不同边上可能有不同的数)。 让我们定义 $dist(v,u)$ 作为从 $v$ 到 $u$ 的简单路径上的边权和。 当且仅当 $u$ 在 $v$ 的子树中并且 $dist(v,u)\leq a_u$,顶点 $v$ 控制顶点 $u(v\neq u)$ 。 Alyona想在某些顶点定居。为了做到这件事,她想知道在每个节点 $v$ 能控制几个节点。 ## 输入格式 第一行包含一个整数 $n (1\leq n\leq 2\times 10^5)$ 第二行有 $n$ 个整数 $a_1,a_2,\ldots,a_n(1\leq a_i\leq 10^9)$ ,作为节点 $i$ 的数。 下面的 $n-1$ 行,每行有两个整数。第 $i$ 行包含整数 $p_i,w_i(1\leq p_i\leq n,1\leq w_i\leq 10^9)$ ,分别为节点 $i+1$ 的在树上的父节点和 $p_i$ 和 $(i+1)$ 的边上的数字。 数据保证是个树。 ## 输出格式 输出 $n$ 个整数,第 $i$ 个数为节点 $i$ 能控制的点数。 ## 输入输出样例 #1 ### 输入 #1 ``` 5 2 5 1 4 6 1 7 1 1 3 5 3 6 ``` ### 输出 #1 ``` 1 0 1 0 0 ``` ## 输入输出样例 #2 ### 输入 #2 ``` 5 9 7 8 6 5 1 1 2 1 3 1 4 1 ``` ### 输出 #2 ``` 4 3 2 1 0 ``` ## 说明/提示 在样例中,节点 $1$ 控制了节点 $3$ ,节点 $3$ 控制节点 $5$ (注意,这并不代表节点 $1$ 控制了节点 $5$ ) Translated by @lolte
最新发布
10-15
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值