poj 1741 Tree(树的点分治)

给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对。

首先对于一个树中的点对,要么经过根结点,要么不经过。所以我们可以把经过根节点的符合点对统计出来。接着对于每一个子树再次运算。如果不用点分治的技巧,时间复杂度可能退化成\(O(n^2)\)(链)。如果对于子树重新选根,找到树的重心,就一定可以保证时间复杂度在\(O(nlogn)\)内。

具体技巧是:首先选出树的重心,将重心视为根。接着计算出每个结点的深度,以此统计答案。由于子树中可能出现重复情况,需要在子树中相应的减去一部分ans。具体实现在代码中。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn=1e4+5; struct Graph{
struct Edge{
int to, next, v; Graph *bel;
Edge& operator ++(){
return *this=bel->edge[next]; }
}edge[maxn*2];
int cntedge, fir[maxn];
void addedge(int x, int y, int v){
Edge &e=edge[++cntedge];
e.to=y; e.next=fir[x]; e.v=v;
fir[x]=cntedge; e.bel=this;
}
Edge& getlink(int x){ return edge[fir[x]]; }
void RESET(){ cntedge=0; memset(fir, 0, sizeof(fir)); }
}g; int n, k, size[maxn], w[maxn], dep[maxn];
int cnt[maxn], tail;
bool done[maxn]; //获取子树大小
int getsize(int now, int par, int num){
size[now]=0; w[now]=0;
Graph::Edge e=g.getlink(now);
for (; e.to; ++e){
if (e.to==par||done[e.to]) continue;
size[now]+=getsize(e.to, now, num);
w[now]=max(w[now], size[e.to]);
}
++size[now]; w[now]=max(w[now], num-size[now]);
return size[now];
} //获取根的位置
int getroot(int now, int par){
Graph::Edge e=g.getlink(now);
int root=now, tmp=now;
for (; e.to; ++e){
if (e.to==par||done[e.to]) continue;
tmp=getroot(e.to, now);
if (w[tmp]<w[root]) root=tmp; //mdzzle
}
return root;
} //获取子树中结点深度,并创造深度数组
void getdep(int now, int par, int step){
Graph::Edge e=g.getlink(now);
for (; e.to; ++e)
if (e.to!=par&&!done[e.to])
getdep(e.to, now, step+e.v);
dep[now]=step;
cnt[tail++]=step;
} //通过深度数组统计和小于等于k的数对
int getans(int k, int tail){
sort(cnt, cnt+tail);
--tail; int ans=0;
for (int l=0; l<tail; ++l){
while (cnt[l]+cnt[tail]>k&&l<tail) --tail;
ans+=tail-l;
}
return ans;
} int solve(int now, int num){
getsize(now, 0, num); //获取当前树的子树大小
if (size[now]==1) return 0;
now=getroot(now, 0); //凭借子树大小找到重心
tail=0; //把深度数组复原
getdep(now, 0, 0); //获取每个结点的深度,构建深度数组
//获取答案,别忘记减掉重复的点对
int ans=getans(k, tail);
done[now]=true; //堵住当前点
Graph::Edge e=g.getlink(now);
for (; e.to; ++e) if (!done[e.to]){
tail=0; getdep(e.to, 0, 0);
ans-=getans(k-e.v*2, tail);
ans+=solve(e.to, size[e.to]);
}
return ans;
} int main(){
while (~scanf("%d%d", &n, &k)&&n&&k){
int t1, t2, t3; g.RESET();
for (int i=1; i<=n; ++i) done[i]=false;
for (int i=1; i<n; ++i){
scanf("%d%d%d", &t1, &t2, &t3);
g.addedge(t1, t2, t3);
g.addedge(t2, t1, t3);
}
printf("%d\n", solve(1, n));
}
return 0;
}

poj 1741 Tree(树的点分治)的更多相关文章

  1. POJ 1741 Tree(树的点分治,入门题)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description ...

  2. POJ 1741 Tree 树的分治(点分治)

    题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...

  3. poj 1741(树的点分治)

    Tree Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...

  4. POJ 1741/1987 树的点分治

    树的点分治,主要思想是每次找子树的重心,计算经过根节点的情况数,再减去点对属于同一子树的情况. #include <iostream> #include <vector> #i ...

  5. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  6. POJ 1741 Tree 树的分治

    原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...

  7. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  8. poj 1741 Tree (树的分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 30928   Accepted: 10351 Descriptio ...

  9. POJ 1741 Tree 树形DP(分治)

    链接:id=1741">http://poj.org/problem?id=1741 题意:给出一棵树,节点数为N(N<=10000),给出N-1条边的两点和权值,给出数值k,问 ...

随机推荐

  1. 一个可以拖拽的div

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Linux_服务器_08_网卡eth1修改为eth0

    一.现象 二.解决步骤 1.修改 70-persistent-net.rules 执行命令: vim /etc/udev/rules.d/-persistent-net.rules 找到与ifconf ...

  3. JavaUtil_01_MD5加密

    一.百度翻译MD5工具类 昨天做java微信开发,引用百度翻译API给公众号添加翻译功能时,需要使用MD5生成签名.注意,使用MD5生成签名后一定要转成小写,不然百度翻译后台不会认你这个签名的,会报无 ...

  4. 批量处理JDBC语句提高处理速度

    当需要成批插入或者更新记录时.可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理.通常情况下比单独提交处理更有效率 JDBC的批量处理语句包括下面两个方法: –      ...

  5. noip2017列队(线段树)

    维护一个方阵,支持 1.删掉一个点,剩下的点先向左看齐再向前看齐 2.询问一个位置上是哪个点 $n,m,q \leq 3 \times 10^5$ sol: 我们每行前$m-1$列维护一个线段树,最后 ...

  6. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

  7. iOS中使用NSInvocation

    在iOS中可以使用NSInvocation进行动态调用方法. /* NSInvocation is much slower than objc_msgSend()... Do not use it i ...

  8. 【转】 Pro Android学习笔记(四八):ActionBar(1):Home图标区

    目录(?)[-] Home Icon 源代码 TextView的滚动 返回主activity或指定activity     ActionBar在Android 3.0 SDK中为平板引入,在4.0中也 ...

  9. 江苏省大学生程序设计竞赛(JSCPC)赛后感

    仔细的算了一下,这是我第6次参加与acm有关的比赛了,每一次的经历,在我看来都是一次对自己能力的认识与评估,身边学计算机专业的同龄人对这项比赛的热爱,专注,勇气以及所获得的成就让要好好努力,更要加倍付 ...

  10. 【总结整理】JQuery基础学习---DOM篇

    前言: 先介绍下需要用到的浏览器提供的一些原生的方法(这里不处理低版本的IE兼容问题) 创建流程比较简单,大体如下: 创建节点(常见的:元素.属性和文本) 添加节点的一些属性 加入到文档中 流程中涉及 ...