点分治模板 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(* ̄▽ ̄*)ブ ...
随机推荐
- Python学习之输入输出入门 A+B篇
描述 求两个整数之和. 输入 输入数据只包括两个整数A和B. 输出 两个整数的和. 样例输入 1 2 样例输出 3 a=input().split() print(int(a[0])+int(a[1 ...
- linux下使用tcpdump抓包分析tcp的三次握手
首先贴上tcp 三次握手的原理图服务器开启ftp服务并执行tcpdump抓包服务器:192.168.3.14 ftp服务客户端:192.168.3.100 服务器执行以下命令,客户端访问服务器ftp: ...
- Android学习笔记长按事件的处理
常见的长按事件 代码示例: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns ...
- Docker拉取镜像加速
关于Docker拉取镜像加速 打开桌面 docker 小图标 选中框框 根据下图 添加国内的加速源即可 Docker加速源 #网易 http://hub-mirror.c.163.com #Docke ...
- linux kernel update
linux内核升级 最近HW行动,报出来的linux系统内核漏洞,环境中全部是2.6.32-431.el6.x86_64的主机,需要全部升级到754版本,这也是第一次进行内核升级操作. 先找了一台和生 ...
- js中each函数的用法
官方说明: jQuery.each(object, [callback]) 概述 通用例遍方法,可用于例遍对象和数组. 不同于例遍 jQuery 对象的 $().each() 方法,此方法可用于例遍任 ...
- C语言实现类
#ifndef __DEFINE__H__ #define __DEFINE__H__ #define vector3(type) \ typedef struct vector3_##type { ...
- MySQL-数据库和表的基本操作
数据库和表的基本操作 数据库基础知识 创建数据库 CREATE DATABASE 数据库名称 ; 查看数据库(显示数据库名列表) SHOW DATABASES ; 查看某数据库信息(显示创建的信息) ...
- 状态机模式 与 ajax 的结合运用
太神奇了,昨晚做了个梦,梦中我悟出一个道理:凡是涉及到异步操作而且需要返回值的函数,一定要封装成 Promise 的形式,假如返回值取决于多个异步操作的结果,那么需要对每个异步操作进行状态的设计,而且 ...
- 入门大数据---Hive数据查询详解
一.数据准备 为了演示查询操作,这里需要预先创建三张表,并加载测试数据. 数据文件 emp.txt 和 dept.txt 可以从本仓库的resources 目录下载. 1.1 员工表 -- 建表语句 ...