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种情况 路径过当前根 ...
随机推荐
- JavaScript使用数组
for循环遍历 //js的数组里可以存各种类型 var arr =[1,5,true,false,'小明']; //遍历 for(var i=0;i<arr.length;i++){ alert ...
- c++ virtual总结
virtual-关键字用于修饰成员函数时,有以下特性 1.用于修饰的基类的成员函数,被修饰的基类成员函数-其派生类的同名成员函数也默认带有virtual 关键字2.当virtual 用于修饰析构函数( ...
- 2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)
题目链接 Problem Description Steph is extremely obsessed with "sequence problems" that are usu ...
- MongoDB之python简单交互(三)
python连接mongodb有多种orm,主流的有pymongo和mongoengine. pymongo 安装相关模块 pip install pymongo pymongo操作 主要对象 Mon ...
- 64_p7
python-flask-whooshalchemy-0.6-10.fc26.noarch.rpm 12-Feb-2017 11:04 51894 python-flask-wtf-0.10.0-8. ...
- PDFRender4NET的使用之pdf转图片
同样的需要第三方的.dll,http://www.o2sol.com/pdfview4net/download.htm using O2S.Components.PDFRender4NET; usin ...
- 链接 DB App.config 解析
<?xml version="1.0" encoding="utf-8"?><configuration> <startup> ...
- python3.X和python2.7的区别
1.python3.X将thread模块修改为_thread
- ASP连接读写ACCESS数据库实例(转)
(一) 数据库的选择:有许多的数据库你可以选择,SQL SERVER.ACCESS(*.mdb).EXCEL(*.xls).FOXPRO(*.dbf)甚至普通的文本文件(*.txt)都可以达到存储 ...
- MVC – 3.EF(Entity Framework)
1.实体框架(EF)简介 与ADO.NET的关系 全称是ADO.NET Entity Framework,是微软开发的基于ADO.NET的ORM(Object/Relational Mapping)框 ...