POJ 1741 Tree(树的分治)
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
题目大意:给一个带边权的树,问有多少对点满足dist(i,j)<=k
思路:可以参考2009年的国家集训队论文《分治算法在树的路径问题中的应用》——漆子超。
代码(188MS):
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = ;
const int MAXE = ;
const int INF = 0x7fff7fff; int head[MAXN], size[MAXN], maxSize[MAXN];
int list[MAXN], cnt;
bool del[MAXN];
int to[MAXE], next[MAXE], cost[MAXE];
int n, k, ecnt; void init() {
memset(head, -, sizeof(head));
memset(del, , sizeof(del));
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cost[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
} void dfs1(int u, int f) {
size[u] = ;
maxSize[u] = ;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == f || del[v]) continue;
dfs1(v, u);
size[u] += size[v];
maxSize[u] = max(maxSize[u], size[v]);
}
list[cnt++] = u;
} int get_root(int u, int f) {
cnt = ;
dfs1(u, f);
int ret, maxr = INF;
for(int i = ; i < cnt; ++i) {
int &x = list[i];
if(max(maxSize[x], size[u] - size[x]) < maxr) {
ret = x;
maxr = max(maxSize[x], size[u] - size[x]);
}
}
return ret;
} void dfs2(int u, int f, int dis) {
list[cnt++] = dis;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == f || del[v]) continue;
dfs2(v, u, dis + cost[p]);
}
} int calc(int a, int b) {
int j = b - , ret = ;
for(int i = a; i < b; ++i) {
while(list[i] + list[j] > k && i < j) --j;
ret += j - i;
if(j == i) break;
}
return ret;
} int ans = ; void work(int u, int f) {
int root = get_root(u, f);
del[root] = true;
int last = ; cnt = ;
for(int p = head[root]; ~p; p = next[p]) {
int &v = to[p];
if(del[v]) continue;
dfs2(v, root, cost[p]);
sort(list + last, list + cnt);
ans -= calc(last, cnt);
last = cnt;
}
list[cnt++] = ;
sort(list, list + cnt);
ans += calc(, cnt);
for(int p = head[root]; ~p; p = next[p]) {
int &v = to[p];
if(del[v]) continue;
work(v, root);
}
} int main() {
while(scanf("%d%d", &n, &k) != EOF) {
if(n == && k == ) break;
init();
for(int i = ; i < n; ++i) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
add_edge(u, v, c);
}
ans = ;
work(, );
printf("%d\n", ans);
}
}
POJ 1741 Tree(树的分治)的更多相关文章
- POJ 1741 Tree 树的分治
原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...
- poj 1741 Tree (树的分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30928 Accepted: 10351 Descriptio ...
- POJ 1741 Tree 树的分治(点分治)
题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- POJ 1741 Tree(树的点分治,入门题)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21357 Accepted: 7006 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 树上点分治
题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...
- POJ 1741 Tree (点分治)
Tree Time Limit: 1000MS Memory ...
- poj 1741 Tree(点分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15548 Accepted: 5054 Description ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
随机推荐
- Bootstrap Data Table简单使用(动态加载数据)
直接上代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- 『ACM C++』 PTA 天梯赛练习集L1 | 050-51
加油加油,努力刷题 ------------------------------------------------L1-050------------------------------------ ...
- 竞赛题解 - Ikki's Story IV-Panda's Trick
Ikki's Story IV-Panda's Trick - 竞赛题解 也算是2-sat学习的一个节点吧 终于能够自己解决一道2-sat的题了 ·题目 一个圆上有n个点按顺时针编号为 0~n-1 , ...
- idea 引入多项目
1.先导入总包 2.右侧mavenmaven,选择parent的pom.xml 3.右上角“Project Structure”检查SDK
- keepalived+haproxy 安装配置
1.安装配置keepalived 修改配置文件/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_de ...
- mqtt使用二(集成到java代码中)
1.我采用的是springboot,首先pom文件中添加mqtt需要用到的依赖 <dependency> <groupId>org.springframework.boot&l ...
- CentOS 7.x下升级Python版本到3.x系列(新老版本共存)
由于python官方已宣布2.x系列即将停止支持,为了向前看,我们升级系统的python版本为3.x系列服务器系统为当前最新的CentOS 7.4 1.安装前查看当前系统下的python版本号 # p ...
- linux系统快速安装宝塔
宝塔面板分linux面板和windows面板,安装宝塔linux面板首先要访问宝塔官网查看对应版本进行选择 宝塔面板的安装需要注意的地方有: 1.纯净系统 2.确保是干净的操作系统,没有安装过其它环境 ...
- 10种简单的Java性能优化
你是否正打算优化hashCode()方法?是否想要绕开正则表达式?Lukas Eder介绍了很多简单方便的性能优化小贴士以及扩展程序性能的技巧. 最近“全网域(Web Scale)”一词被炒得火热,人 ...
- web頁面優化以及SEO
轉載:https://blog.csdn.net/xustart7720/article/details/79960591 浏览器访问优化浏览器请求处理流程如下图: Etag:實體標籤.ETag是HT ...