POJ1741 Tree 树分治模板
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
//using namespace std;
const int maxn=;
const double eps=1e-;
const int modn=;
int n,k;
struct nod{
int y,next;
int v;
}e[maxn*];
int head[maxn]={},siz[maxn]={},ma[maxn]={},dis[maxn]={};
int tot=,tot1,root=,now,ans;
bool vis[maxn]={};
void init(int x,int y,int v){
e[++tot].y=y;
e[tot].next=head[x];
e[tot].v=v;
head[x]=tot;
}
void getsiz(int x,int fa){ //某子树的大小
int y;
siz[x]=;
ma[x]=;
for(int i=head[x];i;i=e[i].next){
y=e[i].y;
if(y!=fa&&!vis[y]){
getsiz(y,x);
siz[x]+=siz[y];
ma[x]=std::max(siz[y],ma[x]);
}
}
}
void cen(int r,int x,int fa){ //找重心
int y;
if(siz[r]-siz[x]>ma[x]){
ma[x]=siz[r]-siz[x];
}
if(ma[x]<now){
root=x;now=ma[x];
}
for(int i=head[x];i;i=e[i].next){
y=e[i].y;
if(y!=fa&&!vis[y]){
cen(r,y,x);
}
}
}
void getdis(int x,int fa,int di){ //子树中各个点到该子树根的距离
int v,y;
dis[++tot1]=di;
for(int i=head[x];i;i=e[i].next){
v=e[i].v;y=e[i].y;
if(y!=fa&&!vis[y]){
getdis(y,x,di+v);
}
}
}
int sum(int x,int d){ //点对数
tot1=;
getdis(x,,d);
std::sort(dis+,dis++tot1);
int i=,j=tot1;
int cnt=;
while(i<j){
while(dis[i]+dis[j]>k&&i<j){
j--;
}
cnt+=j-i;
i++;
}
return cnt;
}
void dfs(int x){
int y;
now=maxn;root=x;
getsiz(x,);
cen(x,x,);
ans+=sum(root,);
vis[root]=;
for(int i=head[root];i;i=e[i].next){
y=e[i].y;
if(!vis[y]){
ans-=sum(y,e[i].v);
dfs(y);
}
}
}
void yu(){
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
tot=;ans=;
}
int main(){
while((~scanf("%d%d",&n,&k))&&(n!=||k!=)){
yu();
int x,y,v;
for(int i=;i<n;i++){
scanf("%d%d%d",&x,&y,&v);
init(x,y,v);
init(y,x,v);
}
dfs();
printf("%d\n",ans);
}
return ;
}
POJ1741 Tree 树分治模板的更多相关文章
- poj 1744 tree 树分治
Tree Time Limit: 1000MS Memory Limit: 30000K Description Give a tree with n vertices,each ed ...
- 【POJ1741】Tree 树分而治之 模板略?
做广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog. ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- HDU 4812 D Tree 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- HDU4871 Shortest-path tree(树分治)
好久没做过树分治的题了,对上一次做是在南京赛里跪了一道很裸的树分治题后学的一道,多校的时候没有看这道题,哪怕看了感觉也看不出来是树分治,看出题人给了解题报告里写了树分治就做一下好了. 题意其实就是给你 ...
- HDU4670 Cube number on a tree 树分治
人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...
- [POJ1741]Tree(点分治)
树分治之点分治入门 所谓点分治,就是对于树针对点的分治处理 首先找出重心以保证时间复杂度 然后递归处理所有子树 对于这道题,对于点对(u,v)满足dis(u,v)<=k,分2种情况 路径过当前根 ...
随机推荐
- JSTL标签库笔记
1. 概述 JSTL(Jsp Standard Tag Library)即JSP标准标签库,只能运行在支持JSP1.2↑和Servlet2.3↑规范的容器上. 通常情况下我们在编写JSP页面的时候,在 ...
- android内存回收顺序
最近做项目的时候,经常会考虑到系统回收进程,释放资源等问题.特别查找了相关资料,了解下android内存回收顺序以及回收场景. 下面内容都为网络查找资料,若有错误,欢迎指出. 以下顺序,依次被回收的可 ...
- 在Oracle中查询表的大小
SELECT segment_name AS TABLENAME,round(BYTES/1024/1024,2) FROM user_segments WHERE segment_name='表名 ...
- django框架之中间件
中间件简介 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在djang ...
- juey点击tr选中里面的radio
//点击一行选中银行卡 $("tr").bind("click",function(){ $("input:radio").attr(&qu ...
- ORACLE数据库导出导入数据
准备工作: 1.登录管理员system 2.create directory dbdata as 'C:\oracle\tempData';--创建备份文件夹 3.grant read,write o ...
- Windows: 在系统启动时运行程序、定时计划任务、定时关机
lesca今天介绍一些让系统在启动时,而非登录时,加载用户自定义的应用程序或脚本的方法,推荐度从前到后依次递减. 1. Windows任务计划(task scheduler) 用户可以按以下步骤进行操 ...
- group by的运用
select a.* from zeai_photo a inner join (select max(id) mid,userid from zeai_photo group by userid) ...
- Mysql授权允许远程访问
MySQL Community Edition(GPL) 在我们使用mysql数据库时,有时我们的程序与数据库不在同一机器上,这时我们需要远程访问数据库.缺省状态下,mysql的用户是没有远程访问的权 ...
- HDU 4553 约会安排(线段树区间合并+双重标记)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4553 题目大意:就是有三种操作: ①DS x,安排一段长度为x的空闲时间跟屌丝一起,输出这段时间的起点 ...