题意:找树上有多少对距离小于K的对数
解析:树分治模板题,见注释

代码

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
typedef __int64 LL;
const int INF=1e9+;
const int maxn=;
int N,K,ans;
struct edge{ int v,w,next; }E[*maxn]; //边
int head[maxn],eid;
void init()
{
ans=eid=;
for(int i=;i<=N;i++) head[i]=-;
}
void AddEdge(int u,int v,int w)
{
E[++eid].v=v; E[eid].w=w;
E[eid].next=head[u]; head[u]=eid;
}
struct TBS
{
int root,did,deep[maxn],dist[maxn]; //root选取的根
int tot,s[maxn],cen[maxn]; //s树的大小,cen左右两边的节点最大值
bool used[maxn]; //是否已被访问
void init(int n) //初始化
{
tot=n;
cen[]=INF;
memset(used,false,sizeof(used));
}
void GetRoot(int u,int fa)//找到重心
{
s[u]=; //树的大小
cen[u]=;
for(int i=head[u];i!=-;i=E[i].next)
{
int v=E[i].v;
if(v==fa||used[v]) continue;
GetRoot(v,u);
s[u]+=s[v];
cen[u]=max(cen[u],s[v]);
}
cen[u]=max(cen[u],tot-s[u]);
if(cen[u]<cen[root]) root=u; //更新重心
}
void GetDeep(int u,int fa)
{
deep[++did]=dist[u]; //保存下来
for(int i=head[u];i!=-;i=E[i].next)
{
int v=E[i].v,w=E[i].w;
if(v==fa||used[v]) continue;
dist[v]=dist[u]+w; //距离所选重心的距离
GetDeep(v,u);
}
}
int Cal(int u,int w)
{
dist[u]=w; did=;
GetDeep(u,);
sort(deep+,deep+did+);
int ret=;
for(int l=,r=did;l<r;)
{
if(deep[l]+deep[r]<=K){ ret+=r-l; l++; } //计算小于K的点对
else r--;
}
return ret;
}
void Work(int u)
{
used[u]=true;
ans+=Cal(u,);
for(int i=head[u];i!=-;i=E[i].next)
{
int v=E[i].v,w=E[i].w;
if(used[v]) continue;
ans-=Cal(v,w); //去重
tot=s[v];
root=;
GetRoot(v,u); //找下一个重心
Work(root);
}
}
}tbs;
int main()
{
while(scanf("%d%d",&N,&K)!=EOF)
{
if(!N&&!K) break;
init();
int u,v,w;
for(int i=;i<N;i++)
{
scanf("%d%d%d",&u,&v,&w);
AddEdge(u,v,w);
AddEdge(v,u,w);
}
tbs.init(N);
tbs.GetRoot(,);
tbs.Work(tbs.root);
printf("%d\n",ans);
}
return ;
}

Poj1741-Tree(树分治)的更多相关文章

  1. POJ1741 Tree 树分治模板

    http://poj.org/problem?id=1741   题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数.   dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...

  2. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  3. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  4. POJ 1741 Tree 树分治

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

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

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

  6. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

  7. HDU4871 Shortest-path tree(树分治)

    好久没做过树分治的题了,对上一次做是在南京赛里跪了一道很裸的树分治题后学的一道,多校的时候没有看这道题,哪怕看了感觉也看不出来是树分治,看出题人给了解题报告里写了树分治就做一下好了. 题意其实就是给你 ...

  8. HDU4670 Cube number on a tree 树分治

    人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...

  9. [POJ1741]Tree(点分治)

    树分治之点分治入门 所谓点分治,就是对于树针对点的分治处理 首先找出重心以保证时间复杂度 然后递归处理所有子树 对于这道题,对于点对(u,v)满足dis(u,v)<=k,分2种情况 路径过当前根 ...

  10. POJ1741 经典树分治

    题意:有一棵树,每条边有一个距离,求dis(u,v)<=k的点的对数 题解:树分治,对于一颗树上的两点,要么在同一颗子树上,要么在不同子树上,要么一个点是根,另一个在某一子树上,对于第一种情况我 ...

随机推荐

  1. hdu5023--A Corrupt Mayor's Performance Art

    来源:2014 ACM/ICPC Asia Regional Guangzhou Online 题意:长度为n的一个线段,1-30为颜色代号.初始状态每个单位长度颜色都为2,然后有q次操作,P操作把区 ...

  2. Hibernate中Session的几个方法的简单说明

    Hibernate对普通JDBC的操作进行了轻量级的封装,使得开发人员可以利用Session对象以面向对象的思想来实现对关系数据库的操作. 一般通过Configuration对象读取相应的配置文件来创 ...

  3. python socket学习

    import socket localip=socket.gethostbyname(socket.gethostname()) print (localip) iplist=socket.getho ...

  4. 去掉MySQL字段中的空格

    mysql replace 函数   语法:replace(object,search,replace)   意思:把object中出现search的全部替换为replace   案例: SQL Co ...

  5. python中os模块常用方法

    #!/usr/bin/python## os module test import os print 'os.name: ', os.nameprint 'os.getcwd(): ', os.get ...

  6. 【转】10分钟了解设计模式(C#)

    10分钟了解设计模式(C#) 最近把<大话设计模式>重温了下(看完序才知道原来作者是也是博客园园友,这本书的最早博客版本在这里).体会最深的就是面向接口编程的重要性,如何在自己的项目中进行 ...

  7. [Redux] Passing the Store Down with <Provider> from React Redux

    Previously, we wrote the Provider component by ourself: class Provider extends Component { getChildC ...

  8. java基础之集合

    集合的定义,集合的应用,集合的分类,集合的遍历,集合的特性

  9. .Net调用Office Com组件的原理及问题检索com类工厂组件检索 COM 类工厂中 CLSID 为 {XXX} 的组件失败

    我是在本地32位操作系统+vs2010+office2007做创建并下载Excel,ppt文件的操作没有问题,发布到64位系统的服务器上报错,最开始报错:: 1:Retrieving the COM ...

  10. Python代码一定要对齐

    不然会出现错误: IndentationError: unindent does not match any outer indentation level PS:新的Python语法,是不支持的代码 ...