点分治模板 POJ 1741
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e6+5;
struct asd{
int from,to,next,val;
}b[maxn];
int head[maxn],tot=1;
void ad(int aa,int bb,int cc){
b[tot].from=aa;
b[tot].to=bb;
b[tot].val=cc;
b[tot].next=head[aa];
head[aa]=tot++;
}
int n,k,ans,Tsiz,Root;
int siz[maxn],wt[maxn],a[maxn];
bool vis[maxn];
int cnt;
void get_root(int now,int fa){
siz[now]=1;
wt[now]=0;
for(int i=head[now];i!=-1;i=b[i].next){
int u=b[i].to;
if(vis[u] || u==fa) continue;
get_root(u,now);
siz[now]+=siz[u];
wt[now]=max(wt[now],siz[u]);
}
wt[now]=max(wt[now],Tsiz-siz[now]);
if(wt[Root]>wt[now]) Root=now;
//printf("Root=%d\n",Root);
}
void dfs(int now,int fa,int d){
a[++cnt]=d;
//printf("%d %d\n",now,a[cnt]);
for(int i=head[now];i!=-1;i=b[i].next){
int u=b[i].to;
//printf("%d %d\n",now,cnt);
if(u!=fa && !vis[u]) dfs(u,now,d+b[i].val);
}
}
int js(int now,int d){
cnt=0;
dfs(now,0,d);
sort(a+1,a+cnt+1);
/*for(int i=1;i<=cnt;i++){
printf("%d %d\n",now,a[i]);
}*/
//printf("now=%d cnt=%d\n",now,cnt);
int sum=0;
for(int i=1,j=cnt;;i++){
//printf("now=%d a[%d]=%d a[%d]=%d\n",now,i,a[i],j,a[j]);
while(j && a[i]+a[j]>k) j--;
if(i>j) break;
sum+=j-i+1;
}
//printf("sum=%d\n",sum);
return sum;
}
void dfss(int now){
//printf("ans=%d\n",ans);
ans+=js(now,0);
vis[now]=1;
for(int i=head[now];i!=-1;i=b[i].next){
int u=b[i].to;
if(!vis[u]){
ans-=js(u,b[i].val);
Root=0;
Tsiz=siz[u];
get_root(u,0);
dfss(Root);
}
}
}
int main(){
while(scanf("%d%d",&n,&k)!=EOF && n){
ans=0;
tot=1;
memset(head,-1,sizeof(head));
memset(&b,0,sizeof(struct asd));
memset(vis,0,sizeof(vis));
for(int i=1;i<n;i++){
int aa,bb,cc;
scanf("%d%d%d",&aa,&bb,&cc);
ad(aa,bb,cc),ad(bb,aa,cc);
}
wt[0]=0x3f3f3f3f;
Root=0;
Tsiz=n;
get_root(1,0);
//printf("Root=%d\n",Root);
dfss(Root);
printf("%d\n",ans-n);
}
return 0;
}
点分治模板 POJ 1741的更多相关文章
- 树的点分治 (poj 1741, 1655(树形dp))
poj 1655:http://poj.org/problem?id=1655 题意: 给无根树, 找出以一节点为根, 使节点最多的树,节点最少. 题解:一道树形dp,先dfs 标记 所有节点的子 ...
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量
POJ 1741. Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34141 Accepted: 11420 ...
- 【POJ 1741】 Tree (树的点分治)
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...
- poj 1741 树的点分治(入门)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18205 Accepted: 5951 Description ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
- poj 1741 楼教主男人八题之中的一个:树分治
http://poj.org/problem? id=1741 Description Give a tree with n vertices,each edge has a length(posit ...
- 点分治——POJ 1741
写的第一道点分治的题目,权当认识点分治了. 点分治,就是对每条过某个点的路径进行考虑,若路径不经过此点,则可以对其子树进行考虑. 具体可以看menci的blog:点分治 来看一道例题:POJ 1741 ...
- 数据结构(树,点分治):POJ 1741 Tree
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). D ...
- POJ 1741 Tree ——点分治
[题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...
随机推荐
- Hadoop之hadoop fs和hdfs dfs、hdfs fs三者区别
适用范围 案例 备注 小记 hadoop fs 使用范围最广,对象:可任何对象 hadoop dfs 只HDFS文件系统相关 hdfs fs 只HDFS文件系统相关(包括与 ...
- Java基础?看完以后再也不惧怕面试了
前言 这篇文章主要是Java基础部分,主要分为3个部分:Java集合.Java多线.JVM:这些东西帮助我面试成功率提升了很多.后面还有中间件Spring.Redis.RocketMQ等等吧,祝愿大家 ...
- MATLAB实例:聚类网络连接图
MATLAB实例:聚类网络连接图 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 本文给出一个简单实例,先生成2维高斯数据,得到数据之后,用模糊C均值( ...
- js实现初始化调用摄像头
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- linux安装QT-Designer两种方法
1.安装Qt-Creator, Qt-Creator自带了qt-designer 2.安装qt5-default,qttools5-dev-tools
- python基础:如何使用 pip 安装第三方库
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 在这个生活中处处都是大数据和人工智能的时代,总是能在各种角落看到 Pyth ...
- <WP8开发学习笔记>ApplicationBar(任务栏)的切换以及“黑条问题”
ApplicationBar(以下简称AppBar)是WP应用相当常见的控件,也很方便.常见的做法是pivot或者panorama的页面切换的时候,AppBar跟随切换对应的按钮或者不显示按钮,如下图 ...
- webtatic源
我们在安装php时,系统的yum源中php版本太老,但是编译安装又太烦,这时我们可以使用webtatic源来yum安装较新版本的php. webtatic源: https://mirror.webta ...
- @loj - 3043@「ZJOI2019」线段树
目录 @description@ @solution@ @accepted code@ @details@ @description@ 九条可怜是一个喜欢数据结构的女孩子,在常见的数据结构中,可怜最喜 ...
- VMWare虚拟机开启时显示模块“Disk”启动失败的解决方案
找到虚拟机所在的目录, 将 .vmx文件打开 将文件vmci0.present = "TRUE"改为 vmci0.present = "FALSE" 删除以.l ...