POJ1741 Tree (点分治)
Tree
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 25772 | Accepted: 8566 |
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
code
#include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; const int N = ; struct Edge{
int to,nxt,w;
}e[N<<];
int head[N],son[N],f[N],deth[N],d[N];
bool vis[N];
int ans,tot,Root,sum,n,k; void init() {
memset(head,,sizeof(head));
memset(vis,false,sizeof(vis));
ans = tot = Root = ;
}
void add_edge(int u,int v,int w) {
e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
}
void getroot(int u,int pa) {
son[u] = ;f[u] = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v==pa||vis[v]) continue;
getroot(v,u);
son[u] += son[v];
f[u] = max(f[u],son[v]);
}
f[u] = max(f[u],sum-son[u]);
if (f[u] < f[Root]) Root = u;
}
void getdeth(int u,int pa) {
deth[++deth[]] = d[u];
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;
if (v==pa||vis[v]) continue;
d[v] = d[u] + w;
getdeth(v,u);
}
}
int calcc(int u,int w) {
deth[] = ;
d[u] = w;
getdeth(u,);
sort(deth+,deth+deth[]+);
int l = ,r = deth[],ret = ;
while (l < r) {
if (deth[l] + deth[r] <= k) ret += r-l,l++;
else r--;
}
return ret;
}
void work(int u) {
ans += calcc(u,);
vis[u] = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;
if (vis[v]) continue;
ans -= calcc(v,w);
sum = son[v];
Root = ;
getroot(v,);
work(Root);
}
}
int main() {
while (scanf("%d%d",&n,&k)!=EOF && !(n==&&k==)) {
init();
for (int a,b,c,i=; i<n; ++i) {
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);add_edge(b,a,c);
}
f[] = 1e9;
sum = n;
getroot(,);
work(Root);
printf("%d\n",ans);
}
return ;
}
POJ1741 Tree (点分治)的更多相关文章
- [POJ1741]Tree(点分治)
树分治之点分治入门 所谓点分治,就是对于树针对点的分治处理 首先找出重心以保证时间复杂度 然后递归处理所有子树 对于这道题,对于点对(u,v)满足dis(u,v)<=k,分2种情况 路径过当前根 ...
- [poj1741]Tree(点分治+容斥原理)
题意:求树中点对距离<=k的无序点对个数. 解题关键:树上点分治,这个分治并没有传统分治的合并过程,只是分成各个小问题,并将各个小问题的答案相加即可,也就是每层的复杂度并不在合并的过程,是在每层 ...
- [bzoj1468][poj1741]Tree[点分治]
可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树 ...
- POJ1741 Tree 树分治模板
http://poj.org/problem?id=1741 题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数. dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- POJ1741 Tree(树分治——点分治)题解
题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...
- [poj1741][tree] (树/点分治)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ1741 tree 【点分治】
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25286 Accepted: 8421 Description ...
- POJ1741 Tree(树的点分治基础题)
Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v) ...
- POJ1741——Tree(树的点分治)
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...
随机推荐
- java 多线程之取消与关闭
要使线程安全,快速,可靠的停下来并不是一件容易的事情.java并没有提供任何机制来安全的终止线程.但是java提供了中断(interrupt)使一个线程可以终止另一个线程的当前工作 每个线程都有一个b ...
- HhashMap HashTable ConcurrentHashMap
hashMap hashTable concurrentHashMap hashMap的效率高于hashTable,hashMap是线程不安全的,并发时hashMap put方法容易引起死循环,导致c ...
- 移动端真机调试工具--DebugGap (VIDE)
越来越多的移动端开发工作,需要我们有一个好的调试工具,以解决各类真机才会遇到的BUG,最近使用了一款DebugGap 的工具,非常方便,在这里推荐给大家. 官网地址 DebugGap . 按需求下载 ...
- 表格<table>
<table> <tr> <th>表头1</th> <th>表头2</th> <th>表头3</th> ...
- Python高效开发实战——Django、Tornado、Flask、Twisted
今天要推荐的就是这本书,内容涉及四种主流的Python Web开发框架,零基础完成网站搭建.数据库设计.前后端开发,全方位领悟Python原理与应用. 最新最全的框架实战,尽在这本书,可搜索亚马逊.京 ...
- nmap --script http-enum,http-headers,http-methods,http-php-version -p 80 目标域
从http服务器上收集到更多地信息 nmap --script http-enum,http-headers,http-methods,http-php-version -p 80 目标域
- 解决mysql连接输入密码提示Warning: Using a password on the command line interface can be insecure
有时候客户端连接mysql需要指定密码时(如用zabbix监控mysql)5.6后数据库会给出个警告信息 mysql -uroot -pxxxx Warning: Using a password o ...
- mybatis-mybatis-config.xml详细介绍
1.mybatis-config.xml 1.1:配置,配置可以是引入外部文件,也可以是在本文件内写配置 <!-- <properties resource="jdbc.prop ...
- LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- flex在众多手机浏览器上的兼容方案(亲测华为手机自带浏览器)
如果项目使用构建工具,可加autoprefixer来处理,[autoprefixer使用指南] 纯手写css兼容代码,需给每个使用的属性加上属性前缀 /*display: flex;写法*/ span ...