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. 优秀开源项目之一:视频监控系统iSpy

    iSpy是一个开源的视频监控软件,目前已经支持中文.自己用了一下,感觉还是很好用的.翻译了一下它的介绍. iSpy将PC变成一个完整的安全和监控系统 iSpy使用您的摄像头和麦克风来检测和记录声音或运 ...

  2. luogu2627 修剪草坪

    dp[i]表示1~i最大效率 记一下前缀和 转移就是f[i]=max(f[i],f[j-1]-sum[j])+sum[i] (i-k<=j<=i) 发现括号里的只与j有关 开一个单调队列维 ...

  3. java将白色背景图片转换成透明图片

    package evecom.image; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.Buffe ...

  4. Gym - 101196G :That's One Hanoi-ed Teacher (递推)

    题意:给定当前汉诺塔的状态,问还有多少步走完,不合法输出“No”. 思路:显然可以一层一层试探下去的.我们设三个柱子为“起始”,“中转”,“终点”,当前状态的最大的盘子不可能在中转站处:如果在起始站, ...

  5. ACM学习历程—Rotate(HDU 2014 Anshan网赛)(几何)

    Problem Description Noting is more interesting than rotation! Your little sister likes to rotate thi ...

  6. navicat导入sql文件

    Hello,大家好.Navicat是我们平时使用较多的一个数据库客户端工具,平时小天我主要是用来连接mysql的,使用的时候还是很方便的. 今天小天我就给大家分享一个Navicat如何导入导出sql文 ...

  7. windows7下c++11环境搭建

    1.安装codeblocks 13.12 2.下载安装tdm-gcc-4.8.1-3 3.配置coldblocks的编译器(settings->compiler->compiler set ...

  8. Azure xplate cli创建虚拟机

    Azure的管理工具多种多样.其中Portal和PowerShell是经常使用的. 但用MAC和Linux的同学最喜欢的还是Bash环境. Azure也提供了跨平台的命令行工具:Azure xplat ...

  9. QTP连接数据库

    '注意:其中DSN=数据源名:UID=用户名:PWD=用户密码 Dim Conn Set Conn=CreateObject("ADODB.Connection") Const C ...

  10. jdbc 新认识

    以前一直用jdbc,没有深入看看,原来jdbc是java自己的接口规范,db厂商按照接口进行开发对应的驱动,jdbc可以获取db中的元信息,执行sql,获取结果,操作db等等.示例如下. public ...