题意:找树上有多少对距离小于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. python手记(39)

    #!/usr/bin/env python #-*- coding: utf-8 -*- #code:myhaspl@qq.com import cv2 import numpy as np fn=& ...

  2. phpcms:五、网站首页(index.html)

    1.经典案例:图文列表:{pc:content  action="position" posid="2" order="listorder DESC& ...

  3. web项目跨域访问

    1.同域相互访问 假设A.html 与 b.html domain都是localhost (同域) A.html中iframe 嵌入 B.html,name=myframe A.html有js fun ...

  4. 《学习opencv》笔记——矩阵和图像操作——cvCalcCovarMatrix,cvCmp and cvCmpS

    矩阵和图像的操作 (1)cvCalcCovarMatrix函数 其结构 void cvCalcCovarMatrix(计算给定点的均值和协方差矩阵 const CvArr** vects,//给定向量 ...

  5. RPG JS跨平台测试

    RPG JS虽然说是跨平台的,但是在具体的测试中效果并不理想. 以官方提供的Demo为例 问题一 手机的屏幕太小,导致画面上的人物都很小,连点击都很不准确.在9寸的平板上才可以看得比较清楚. 问题二 ...

  6. 漫话Unity3D(一)

    前言 使用Unity已经有将近半年时间了,尽管项目还仅仅是个半成品,可是Unity差点儿相同玩熟了.这里把使用过程中碰到的问题梳理一遍.不会涉及到太过详细的功能和代码,可是假设开发一个网游又都会涉及到 ...

  7. 细说php(六) 数组

    一.数组概述 1.1 数组是复合类型 1.2 数组中能够存储随意长度的数据, 也能够存储随意类型的数据 二.数组的类型 2.1 索引数组: 下标是顺序整数作为索引 <?php $user[0] ...

  8. linux上网络配置不生效的怪异现象处理

    1.在Linux上.在ifcfg-eth0上设置IP地址等信息 具体配置信息例如以下已 [root@rac01 Desktop]#more/etc/sysconfig/network-scripts/ ...

  9. SqlBulkCopy使用心得 (大量数据导入)

    文章转载原地址:http://www.cnblogs.com/mobydick/archive/2011/08/28/2155983.html 最近做的项目由于之前的设计人员懒省事,不按照范式来,将一 ...

  10. <!DOCTYPE html>的问题

    <!DOCTYPE> 声明必须位于 HTML5 文档中的第一行,也就是位于 <html> 标签之前.该标签告知浏览器文档所使用的 HTML 规范. doctype 声明不属于 ...