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种情况 路径过当前根 ...
随机推荐
- 【BZOJ】1537: [POI2005]Aut- The Bus
[算法]DP+线段树求区间max(二维偏序) [题解] 状态转移方程:f[i]=max(f[j]+v[i]),x[j]<x[i]&&y[j]<y[i]. 观察j的条件限制显 ...
- Mysql储存过程6: in / out / inout
in 为向函数传送进去的值 out 为函数向外返回的值 intout 传送进去的值, 并且还返回这个值 )) begin then select 'true'; else select 'false' ...
- struct msghdr和struct cmsghdr【转载】
理解struct msghdr当我第一次看到他时,他看上去似乎是一个需要创建的巨大的结构.但是不要怕.其结构定义如下:struct msghdr { void *msg_name ...
- 64_t5
texlive-mkpattern-svn15878.1.2-33.fc26.2.noarch..> 24-May-2017 15:54 38178 texlive-mkpic-bin-svn3 ...
- http之100-continue
[http之100-continue] 1.http 100-continue用于客户端在发送POST数据给服务器前,征询服务器情况,看服务器是否处理POST的数据,如果不处理,客户端则不上传POST ...
- 2017百度春招<不等式排列>
题目: 度度熊最近对全排列特别感兴趣,对于1到n的一个排列,度度熊发现可以在中间根据大小关系插入合适的大于和小于符号(即 '>' 和 '<' )使其成为一个合法的不等式数列.但是现在度度熊 ...
- Linux 基础目录和命令
Linux 标准目录结构 初学Linux,首先需要弄清Linux 标准目录结构 / root --- 启动Linux时使用的一些核心文件.如操作系统内核.引导程序Grub等. home --- 存 ...
- 模块定义文件.def
一作用 DLL中导出函数的声明有两种方式:一种为在函数声明中加上__declspec(dllexport),这里不再举例说明:另外一种方式是采用模块定义(.def) 文件声明,.def文件为链接器提供 ...
- ~Delphi const 杂谈~
来自:http://www.cnblogs.com/tibetwolf/articles/1785744.html ------------------------------------------ ...
- 三、ansible简要使用
1.ansible服务器生成公钥与私钥 ssh-keygen -t rsa 2.拷贝ansible公钥到客户机上 ssh-copy-id -i root@192.168.1.1 3.添加主机到ansi ...