点分治模板 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(* ̄▽ ̄*)ブ ...
随机推荐
- IOS App如何调用python后端服务
本篇文章旨在通过一个小的Demo形式来了解ios app是如何调用python后端服务的,以便我们在今后的工作中可以清晰的明白ios app与后端服务之间是如何实现交互的,今天的示例是拿登录功能做一个 ...
- Linux创建与删除
一.创建文件夹.文本.用户.组 mkdir 创建文件夹 touch 创建文本 useradd 创建用户 例1:创建一个文件夹xiaomi 创建文件夹:mkdir xiaomi 例2:创建一个文件如ad ...
- 练习C++的vector语法-约瑟夫问题
//测试vector,约瑟夫问题 #include <iostream> #include <vector> using namespace std; int main() { ...
- JMeter接口压测和性能监测
JMeter接口压力测试总结 一.安装JMeter 1. 在客户端机器上安装JMeter压测工具,我这里安装的版本是apache-jmeter-5.2.1,由于JMeter是JAVA语言开发的 ...
- v-model 指令来实现双向数据绑定
<div id="app"> <p>{{ message }}</p> <input v-model="message" ...
- [FireDAC][Phys][MSSQL]-310._数据库安装工具_问题需要解决_连载_3
//先来看看我们碰到的问题,再来求解答SQL脚本执行失败,[FireDAC][Phys][MSSQL]-310. Cannot execute command returning result set ...
- MyISAM 和 InnoDB 索引结构及其实现原理
数据库索引,是数据库管理系统中一个排序的数据结构,以协助快速查询.更新数据库表中数据. 索引的实现通常使用B_TREE. B_TREE索引加速了数据访问,因为存储引擎不会再去扫描整张表得到需要的数据; ...
- if test表达式逻辑判断不能用&&
用&&会报错 用and 例如: <if test="age!=null and name!=null">
- ZWave对COMAND CLASS的处理流程
文章主题 在开发一个 ZWave Device 的过程中,对 COMAND CLASS(单词太长了,后面就简写为 CC 啦) 的处理是最基本.最重要的工作.这篇文章以最最简单的 CC:COMMNAD ...
- 使用addEventListener绑定事件是关于this和event记录
DOM元素使用addEventListener绑定事件的时候经常会碰到想把当前作用域传到函数内部,可以使用以下两种放下: var bindAsEventListener=function (objec ...