poj 1744 tree 树分治
| Time Limit: 1000MS | Memory Limit: 30000K | |
Description
Define dist(u,v)=The min distance between node u and v.
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k.
Write a program that will count how many pairs which are valid for a given tree.
Input
The last test case is followed by two zeros.
Output
Sample Input
5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0
Sample Output
8
Source
树分治模板题;
分治算法在树的路径问题中的应用
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e4+,M=2e6+,inf=1e9+;
const LL INF=1e18+,mod=1e9+; struct is
{
int v,w,nex;
}edge[N<<];
int head[N],edg;
void add(int u,int v,int w)
{
edge[++edg]=(is){v,w,head[u]};head[u]=edg;
} int son[N],msi[N],d[N];
int vis[N],deep[N];
int n,K,ans,root,sum;
void groot(int u,int fa)
{
son[u]=,msi[u]=;
for(int i=head[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
if(v==fa||vis[v])continue;
groot(v,u);
son[u]+=son[v];
msi[u]=max(msi[u],son[v]);
}
msi[u]=max(msi[u],sum-son[u]);
if(msi[u]<msi[root])root=u;
}
void gdeep(int x,int fa)
{
deep[++deep[]]=d[x];
for(int i=head[x];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
int w=edge[i].w;
if(v==fa||vis[v])continue;
d[v]=d[x]+w;
gdeep(v,x);
}
} int rootans(int x,int base)
{
d[x]=base;deep[]=;
gdeep(x,);
sort(deep+,deep++deep[]);
int ans=,l=,r=deep[];
while(l<r)
{
if(deep[l]+deep[r]<=K)
{
ans+=r-l;
l++;
}
else r--;
}
return ans;
}
void dfs(int u)
{
ans+=rootans(u,);
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
int w=edge[i].w;
if(vis[v])continue;
ans-=rootans(v,w);
root=;sum=son[v];
groot(v,u);
dfs(root);
}
}
void init(int n)
{
memset(head,-,sizeof(head));
memset(msi,,sizeof(msi));
memset(son,,sizeof(son));
memset(d,,sizeof(d));
memset(vis,,sizeof(vis));
memset(deep,,sizeof(deep));
sum=n;root=;edg=;
msi[]=inf;
ans=;
}
int main()
{
while(~scanf("%d%d",&n,&K))
{
if(n==&&K==)break;
init(n);
for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
groot(,);
dfs(root);
printf("%d\n",ans);
}
return ;
}
poj 1744 tree 树分治的更多相关文章
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- poj 1741 Tree (树的分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30928 Accepted: 10351 Descriptio ...
- 【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 3237 Tree(树链拆分)
题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...
- POJ 1741 Tree ——点分治
[题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...
- HDU4871 Shortest-path tree(树分治)
好久没做过树分治的题了,对上一次做是在南京赛里跪了一道很裸的树分治题后学的一道,多校的时候没有看这道题,哪怕看了感觉也看不出来是树分治,看出题人给了解题报告里写了树分治就做一下好了. 题意其实就是给你 ...
- HDU4670 Cube number on a tree 树分治
人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...
随机推荐
- HDU 1207 汉诺塔II (递推)
经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往上按大小顺序摞着64片黄金圆盘.上 ...
- StructureStreaming与kafka集成读取数据必要的jar包
<dependency> <!--structurStreaming读取kafka1.0以下必须的jar--> <groupId>org.apache.spark& ...
- mysql命令(三)
创建一个名字为Student库: create database Student; 用以下地命令来查看创建的数据库是否成功: show databases; 进入数据库: use Student; 用 ...
- 一线互联网常见的 14 个 Java 面试题,你颤抖了吗程序员
跳槽不算频繁,但参加过不少面试(电话面试.face to face 面试),面过大 / 小公司.互联网 / 传统软件公司,面糊过(眼高手低,缺乏实战经验,挂掉),也面过人,所幸未因失败而气馁,在此过程 ...
- php+js 防止被抓包篡改数据,数据签名校验
签名密钥,这个是自己生成的,需要客户端+服务端一致. <?php /** * 获取签名 * @param $data 提交的数据 * @param $key 安全密钥 * @return boo ...
- Git clone 报错 Unable to negotiate with xxx.xxx.xxx.xxx port 12345: no matching cipher found. Their offer: aes128-cbc,3des-cbc,blowfish-cbc
git clone 报错 Unable to negotiate with xxx.xxx.xxx.xxx. port 12345: no matching cipher found. Their o ...
- Python3 join函数和os.path.join用法
Python3 join函数和os.path.join用法 os.path.join()连接两个文件名地址的时候,就比os.path.join("D:\","test. ...
- rabbitmq heartbeat missing with heartbeat = N seconds原因总结
一直以来,在我们大规模使用rabbitmq的服务端应用中,都没有出现rabbitmq心跳超时而造成的的影响,反倒是在rabbitmq-cpp客户端出现过很多次该问题,一直以为客户端lib实现的问题(不 ...
- html5-Input类型
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>html5-Input类型& ...
- [c/c++] programming之路(1)、编写程序打开记事本、计算器等
一.命令行启动程序 通过命令行关闭程序:taskkill /f /im 程序名.exe 二.打开记事本.计算器 #include <stdlib.h> void main(){ syste ...