hdu 5877 (dfs+树状数组) Weak Pair
题目:这里
题意:
给出一个n个结点的树和一个数k,每个结点都有一个权值,问有多少对点(u,v)满足u是v的祖先结点且二者的权值之积小于等于k、
从根结点开始dfs,假设搜的的点的权值是v,我们需要的是在此之前搜的点中小于等于k/v的数的个数,于是用树状数组查询,查完后将v加入树状数组以供下个
点查询,回溯的时候再一个个的删除将其树状数组。由于这个权值和k的范围比较大,所以得先离散化后在加入树状数组。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std; typedef long long ll;
const int M = 1e5 + ;
int head[M],cas,du[M],has[M],len;
ll a[M],b[M],k,sum;
bool vis[M]; int max(int x,int y){return x>y?x:y;}
struct Edge{
int to,next;
}edge[M*]; void add(int u,int v)
{
edge[++cas].next=head[u];
edge[cas].to=v;
head[u]=cas;
} int lowbit(int x) {return x&(-x);} void shuadd(int x,int y)
{
while (x<=len){
has[x]+=y;
x+=lowbit(x);
}
} int getsum(int x){
int ans=;
while (x>){
ans+=has[x];
x-=lowbit(x);
}
return ans;
} int sreach(ll x)
{
int l=,r=len,ans=;
while (l<=r){
int mid=(l+r)/;
if (b[mid]<=x) ans=max(ans,mid),l=mid+;
else r=mid-;
}
return ans;
} void dfs(int u)
{
int pos;
if (a[u]!=) pos=sreach(k/a[u]);
else pos=len;
sum+=getsum(pos);
//cout<<sum<<endl;
shuadd(sreach(a[u]),);
for (int i=head[u] ; i ; i=edge[i].next)
{
int v=edge[i].to;
if (vis[v]) continue;
vis[v]=true;
dfs(v);vis[v]=false;
shuadd(sreach(a[v]),-);
}
} int main()
{
int t;
scanf("%d",&t);
while (t--){
int n,j=;cas=;
scanf("%d%I64d",&n,&k);
for (int i= ; i<=n ; i++) {
scanf("%I64d",&a[i]);
if (a[i]==) continue;
b[i]=a[i];
}
sort(b+,b+n+);
len=unique(b+,b+n+)-b;
sort(b+,b+len);len--;
// cout<<len<<endl;
memset(du,,sizeof(du));
memset(vis,false,sizeof(vis));
memset(head,,sizeof(head));
memset(has,,sizeof(has));
for (int i= ; i<n ; i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
du[v]++;
}
//cout<<len<<endl;
sum=;
for (int i= ; i<=n ; i++)
{
if (du[i]) continue;
dfs(i);
}
printf("%I64d\n",sum);
}
return ;
}
hdu 5877 (dfs+树状数组) Weak Pair的更多相关文章
- Weak Pair (dfs+树状数组)
Weak Pair (dfs+树状数组) 题意 这个题目是要求:一颗树上,有n个节点,给出每个节点的权值.另外给出一个值k,问有多少对节点满足: \(power[u]*power[v]<=k\) ...
- codeforces 1076E Vasya and a Tree 【dfs+树状数组】
题目:戳这里 题意:给定有n个点的一棵树,顶点1为根.m次操作,每次都把以v为根,深度dep以内的子树中所有的顶点(包括v本身)加x.求出最后每个点的值为多少. 解题思路:考虑到每次都只对点及其子树操 ...
- HDU - 5877 Weak Pair (dfs+树状数组)
题目链接:Weak Pair 题意: 给出一颗有根树,如果有一对u,v,如果满足u是v的父节点且vec[u]×vec[v]<=k,则称这对结点是虚弱的,问这棵树中有几对虚弱的结点. 题解: 刚开 ...
- HDU 5877 Weak Pair DFS + 树状数组 + 其实不用离散化
http://acm.hdu.edu.cn/listproblem.php?vol=49 给定一颗树,然后对于每一个节点,找到它的任何一个祖先u,如果num[u] * num[v] <= k.则 ...
- HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Tota ...
- 2016 大连网赛---Weak Pair(dfs+树状数组)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...
- hdu_5877_Weak Pair(离散+DFS+树状数组)
题目链接:hdu_5877_Weak Pair 题意: 给你一棵树,让你找有多少对满足那两个条件的weak pair 题解: 有人用Treap,我不会,然后我用树状数组+离散来替代Treap,用DFS ...
- HDU 6203 2017沈阳网络赛 LCA,DFS+树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意:n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V ...
- CF Edu54 E. Vasya and a Tree DFS+树状数组
Vasya and a Tree 题意: 给定一棵树,对树有3e5的操作,每次操作为,把树上某个节点的不超过d的子节点都加上值x; 思路: 多开一个vector记录每个点上的操作.dfs这颗树,同时以 ...
随机推荐
- STL之序列容器vector
首先来看看vector的模板声明: template <class T, class Alloc = allocator<T>> class vector { //… }; v ...
- The C Programming Language Exercise
1-9 : Write a program to copy its input to its output, replacing each string of one or more blanks b ...
- Android IOS WebRTC 音视频开发总结(八十六)-- WebRTC中RTP/RTCP协议实现分析
本文主要介绍WebRTC中的RTP/RTCP协议,作者:weizhenwei ,文章最早发表在编风网,微信ID:befoio 支持原创,转载必须注明出处,欢迎关注我的微信公众号blacker(微信ID ...
- js实现继承的两种方式
这是面试时面试官会经常问到问题: js的继承方式大致可分为两种:对象冒充和原型方式: 一.先说对象冒充,又可分为3种:临时属性方式.call().apply(): 1.临时属性方式: 当构造对象son ...
- c++ 打印堆栈代码
namespace google {namespace glog_internal_namespace_ {void DumpStackTraceToString(std::string* stack ...
- smarty 模板的入门使用
<?php require_once 'inc/libs/Smarty.class.php'; $s=new Smarty(); // echo $s::SMARTY_VERSION; // e ...
- 常用到的git,mvn,postgres,vim命令总结
mvn: 打包: mvn package 如果想在打包的时候跳过测试: mvn package -Dmaven.test.skip=true 使用的junit测试框架, 测试: mvn test 如果 ...
- CVPR 2007 Learning to detect a salient object
Dataset: MSRA A&B are introduced in this paper. A conditional Random Field based method was prop ...
- 使用UltraISO制作U盘启动盘——转载
现在流行用U盘来安装系统,但要用U盘来安装系统的前提条件下是如何将镜像文件写入到U盘里,UltraISO能很好的满足你的需求. 步骤/方法 鼠标右键“以管理员身份运行”UltraISO图标 打 ...
- 将golang程序注册为windows服务
1.go get bitbucket.org/kardianos/service 2.参考里面的exmaple,就可以了 我承认我有点蛋疼