HDU 5877 Weak Pair(弱点对)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Description

题目描述

You are given a rooted tree of N nodes, labeled from 1 to N. To the ith node a non-negative value ai is assigned. An ordered pair of nodes (u,v) is said to be weak if

(1) u is an ancestor of v (Note: In this problem a node u is not considered an ancestor of itself);

(2) au × av ≤ k.

Can you find the number of weak pairs in the tree?

给你有N个节点的有根树,编号从1到N。第i个节点会被分配一个非负数ai。一个满足如下条件的有序点对(u, v)则被认为是弱点对

(1)uv的先祖节点(注意:这个问题中u不能为自身的先祖节点)。

(2) au X av k

你能找出这棵树里有多少弱点对吗?

Input

输入

There are multiple cases in the data set.

The first line of input contains an integer T denoting number of test cases.

For each case, the first line contains two space-separated integers, N and k, respectively.

The second line contains N space-separated integers, denoting a1 to aN.

Each of the subsequent lines contains two space-separated integers defining an edge connecting nodes u and v , where node u is the parent of node v.

Constrains:

1≤N≤105

0≤ai≤109

0≤k≤1018

多组测试用例。

输入的第一行有一个整数T表示测试用例的数量。

对于每个测试用例,第一行有两个用空格分隔的整数,Nk

第二行有N个用空格分隔的整数,表示a1aN

随后每(N-1)行有两个用空格分隔的数uv表示连接两节点的一条边,其中uv的父节点。

范围如下:

1≤N≤105

0≤ai≤109

0≤k≤1018

Output

输出

For each test case, print a single integer on a single line denoting the number of weak pairs in the tree.

对于每个测试用例,输出一个表示树中弱点对数量的整数在单独一行。

Sample Input - 输入样例

Sample Output - 输出样例

1
2 3
1 2
1 2

1

【题解】

DFS + 离散化线段树

用DFS从上往下(从根往叶子)添加与释放节点进线段树,利用线段树查找符合的区间中元素数量。

因为单个数的值比较大,但是总数比较少,线段树还算可以接受。根据总数进行离散化,其实就是排个序+map。出于想用upper_bound的强迫症没有把k/ai的结果一并加入离散化(注意0),不然代码量应该更少。

【代码 C++】

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define LL __int64
#define mx 100005
std::map<LL, int> mp;
LL data[mx], dMP[mx], opt, k;
int n; struct Edge{
int to, next;
}edge[mx];
int iE, head[mx], d[mx], tr[mx << ], fid;
void addEdge(int u, int v){
edge[iE].to = v; edge[iE].next = head[u]; head[u] = iE++;
} void cnt(int l, int r, int now){
if (r <= fid){ opt += tr[now]; return; }
int mid = l + r >> ;
if (fid <= mid) return cnt(l, mid, now << );
opt += tr[now << ]; return cnt(mid + , r, now << | );
}
void sub(int l, int r, int now){
--tr[now];
if (l == r) return;
int mid = l + r >> ;
if (fid <= mid) return sub(l, mid, now << );
return sub(mid + , r, now << | );
}
void add(int l, int r, int now){
++tr[now];
if (l == r) return;
int mid = l + r >> ;
if (fid <= mid) return add(l, mid, now << );
return add(mid + , r, now << | );
} void DFS(int now){
int u;
LL temp;
if (data[now] == || (temp = k / data[now]) >= dMP[n]) fid = n;
else fid = std::upper_bound(dMP + , dMP + + n, temp) - dMP - ;
if (fid) cnt(, n, );
fid = mp[data[now]]; add(, n, );
for (u = head[now]; ~u; u = edge[u].next) DFS(edge[u].to);
fid = mp[data[now]]; sub(, n, );
} int main(){
int t, i, u, v;
scanf("%d", &t);
while (t--){
mp.clear(); memset(tr, , sizeof(tr));
scanf("%d%I64d", &n, &k);
for (i = ; i <= n; ++i) scanf("%I64d", &data[i]);
memcpy(dMP, data, sizeof(data));
std::sort(dMP + , dMP + n + );
for (i = ; i <= n; ++i) mp[dMP[i]] = i; memset(head, -, sizeof(head)); memset(d, , sizeof(d));
iE = ;
for (i = ; i < n; ++i){
scanf("%d%d", &u, &v);
addEdge(u, v); ++d[v];
} for (i = ; d[i]; ++i);
opt = ; DFS(i);
printf("%I64d\n", opt);
}
return ;
}

HDU 5877 Weak Pair(弱点对)的更多相关文章

  1. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  2. hdu 5877 Weak Pair (Treap)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5877 题面; Weak Pair Time Limit: 4000/2000 MS (Java/Other ...

  3. 2016 ACM/ICPC Asia Regional Dalian Online HDU 5877 Weak Pair treap + dfs序

    Weak Pair Problem Description   You are given a rooted tree of N nodes, labeled from 1 to N. To the  ...

  4. HDU - 5877 Weak Pair (dfs+树状数组)

    题目链接:Weak Pair 题意: 给出一颗有根树,如果有一对u,v,如果满足u是v的父节点且vec[u]×vec[v]<=k,则称这对结点是虚弱的,问这棵树中有几对虚弱的结点. 题解: 刚开 ...

  5. HDU 5877 Weak Pair (2016年大连网络赛 J dfs+反向思维)

    正难则反的思想还是不能灵活应用啊 题意:给你n个点,每个点有一个权值,接着是n-1有向条边形成一颗有根树,问你有多少对点的权值乘积小于等于给定的值k,其中这对点必须是孩子节点与祖先的关系 我们反向思考 ...

  6. hdu 5877 Weak Pair dfs序+树状数组+离散化

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Prob ...

  7. HDU 5877 Weak Pair(树状数组)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...

  8. HDU 5877 Weak Pair(树状数组+dfs+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=5877 题意: 给出一棵树,每个顶点都有权值,现在要你找出满足要求的点对(u,v)数,u是v的祖先并且a[u]*a ...

  9. HDU 5877 Weak Pair DFS + 树状数组 + 其实不用离散化

    http://acm.hdu.edu.cn/listproblem.php?vol=49 给定一颗树,然后对于每一个节点,找到它的任何一个祖先u,如果num[u] * num[v] <= k.则 ...

随机推荐

  1. android 学习随笔十(网络:get、post提交数据)

    1.get public class Tools { public static String getTextFromStream(InputStream is){ byte[] b = new by ...

  2. 由 "select *" 引发的“惨案”

    今天凌晨做发布, 要合并多个分数据库的表数据到主数据库中, 有 30+ 分数据库. 前面都比较顺利, 在临近结束时,突然发现一个字段的值插入错误. 有一个表 T,字段分别为 (f1, f2, f3, ...

  3. Intel+Ardruino 101

    为了传说中的那啥, 啊, 嗯.. #include <CurieBLE.h>const int ledPin = 13; // set ledPin to on-board LED  LE ...

  4. PHP 加密的几种方式

    在使用PHP开发Web应用的中,很多的应用都会要求用户注册,而注册的时候就需要我们对用户的信息进行处理了,最常见的莫过于就是邮箱和密码了,本文意在讨论对密码的处理:也就是对密码的加密处理. MD5 相 ...

  5. Maven3.x 插件开发入门

    Maven工具有很多插件,各种各样的插件,让我们开发调试过程中非常方便,但是终究是有你想要的但是现目前插件不能满足的(可能性非常非常低),这个时候就需要使用其他的替代工具,或者是自己来开发一个Mave ...

  6. 【Pro ASP.NET MVC 3 Framework】.学习笔记.8.SportsStore:管理

    管理功能,如何身份认证,对controller和action方法过滤安全的访问,并在用户需要时提供证书. 1 添加分类管理 方便管理的controller,有两类页面,List页面和edit页面. 1 ...

  7. 很好用的查看音频波形的软件cool edit pro

  8. Linux下命令行安装WebLogic 10.3.6

    1.创建用户useradd weblogic;创建用户成功linux系统会自动创建一个和用户名相同的分组,并将该用户分到改组中.并会在/home路径下创建一个和用户名相同的路径,比如我们创建的webl ...

  9. HDU 4336:Card Collector(容斥原理)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Special Judge Problem Descriptio ...

  10. [xcode]instruments来检验你的app

      原文网址:http://www.cocoachina.com/industry/20140114/7696.html     比较了好多关于instruments 还是发现老外写的比较牛逼.于是果 ...