正难则反的思想还是不能灵活应用啊

题意:给你n个点,每个点有一个权值,接着是n-1有向条边形成一颗有根树,问你有多少对点的权值乘积小于等于给定的值k,其中这对点必须是孩子节点与祖先的关系

我们反向思考,可以知道任一点都只对其每个祖先有贡献。所以我们可以转化为求每个点与其每个祖先的乘积小于等于给定的值k的对数。

我们dfs遍历这颗树使用树状数组维护,dfs遍历孩子就添点回溯就删点,接着对每个点计算树状数组里不大于(k/此点)的个数。注意值太大我们需要离散化,而且我们可以把每个点m与k/m都离散化出来,然后就是每次树状数组更新这个点的个数(加减1),查询不大于(k/此点)的个数

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
struct node
{
int pos;
ll val;
} nod[Max];
int bit[Max],vis[Max],n;
ll k,ans,rop[Max];
int head[Max],nnext[Max],to[Max],e;
bool cmp(struct node p1,struct node p2)
{
if(p1.val==p2.val)
return p1.pos<p2.pos;
return p1.val<p2.val;
}
void Add(int u,int v)
{
to[e]=v;
nnext[e]=head[u];
head[u]=e++;
return;
}
int lowbit(int x)
{
return x&(-x);
}
void AddBit(int x,int y)
{
while(x<=(n<<))
{
bit[x]+=y;
x+=lowbit(x);
}
return;
}
int Sum(int x)
{
int sum=;
while(x)
{
sum+=bit[x];
x-=lowbit(x);
}
return sum;
}
void dfs(int son,int fat)
{
AddBit(rop[son],);//遍历孩子添边
for(int i=head[son]; i!=-; i=nnext[i])
{
if(to[i]!=fat)
{
ans+=Sum(rop[to[i]+n]);
dfs(to[i],son);
}
}
AddBit(rop[son],-);//回溯删边
return;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(vis,,sizeof(vis));
scanf("%d %I64d",&n,&k);
for(int i=; i<=n; ++i)
{
scanf("%I64d",&nod[i].val);
nod[i].pos=i;
}
for(int i=; i<=n; ++i)//和k/num[u]一起离散化
{
if(nod[i].val)//关键注意除数为0
nod[i+n].val=k/nod[i].val;
else
nod[i+n].val=0ll;
nod[i+n].pos=i+n;
}
sort(nod+,nod++(n<<),cmp);
for(int i=; i<=(n<<); ++i)//离散化
rop[nod[i].pos]=i;
e=;
memset(head,-,sizeof(head));
int u,v;
for(int i=; i<n; ++i)
{
scanf("%d %d",&u,&v);
Add(u,v);
vis[v]++;
}
ans=0ll;
memset(bit,,sizeof(bit));
for(int i=; i<=n; ++i)
{
if(!vis[i])//找根
{
dfs(i,i);
}
}
printf("%I64d\n",ans);
}
return ;
}

Weak Pair

HDU 5877 Weak Pair (2016年大连网络赛 J dfs+反向思维)的更多相关文章

  1. HDU 5877 Weak Pair(弱点对)

    HDU 5877 Weak Pair(弱点对) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Jav ...

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

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

  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 (Treap)

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

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

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

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

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

  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. 2、ACE-实用生活口语-介绍 Introductions

    (1) (2) (3)认识你很荣幸,如何称呼您呢?It's a pleasure to meet you.I'm glad to meet you.May I have your name?How s ...

  2. 《从零开始学Swift》学习笔记(Day 32)——计算属性

    原创文章,欢迎转载.转载请注明:关东升的博客 计算属性本身不存储数据,而是从其他存储属性中计算得到数据. 计算属性概念: 计算属性提供了一个getter(取值访问器)来获取值,以及一个可选的sette ...

  3. NetCore

    正确理解DTO.值对象和POCO https://www.cnblogs.com/redmoon/archive/2015/04/29/4467485.html ASP.NET Core 配置系统 h ...

  4. Java多线程的两种实现方式

    Java总共有两种方式实现多线程 方式1:通过继承Thread类的方式 package com.day04; /** * 通过继承Thread类并复写run方法来是实现多线程 * * @author ...

  5. RabbitMQ指南之三:发布/订阅模式(Publish/Subscribe)

    在上一章中,我们创建了一个工作队列,工作队列模式的设想是每一条消息只会被转发给一个消费者.本章将会讲解完全不一样的场景: 我们会把一个消息转发给多个消费者,这种模式称之为发布-订阅模式. 为了阐述这个 ...

  6. 子单元通过 prop 接口与父单元进行了良好的解耦

    https://cn.vuejs.org/v2/guide/#起步 现在,我们可以使用 v-bind 指令将待办项传到循环输出的每个组件中: <div id="app-7"& ...

  7. Ramsey's_theorem Friendship Theorem 友谊定理

    w https://en.wikipedia.org/wiki/Ramsey's_theorem https://zh.wikipedia.org/wiki/拉姆齐定理 在组合数学上,拉姆齐(Rams ...

  8. jquery.fileDownload plugin: Success msg alert before actual pdf download completed

    Currently , I use jquery fileDownload plugin to download multiple pdf that in a list page, which eve ...

  9. hdu 2112 HDU Today(map与dijkstra的结合使用)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  10. 如何用jquery获取input输入框中的值?

    如何用jquery获取<input id="test" name="test" type="text"/>中输入的值? $(&q ...