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的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
随机推荐
- c++基础STL
今天给大家介绍几个容器,包含的头文件为<vector>,<stack>,<queue>,<map>,<list>,<deque> ...
- 理解Redux以及如何在项目中的使用
今天我们来聊聊Redux,这篇文章是一个进阶的文章,建议大家先对redux的基础有一定的了解,在这里给大家推荐一下阮一峰老师的文章: http://www.ruanyifeng.com/blog/20 ...
- CASE WHEN 批量更新
单个值: UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHE ...
- PHPExcel 导入Excel数据 (导出下一篇我们继续讲解)
一:使用composer下载 phpoffice/phpexcel 或者直接下载安装包 composer require phpoffice/phpexcel 二 1:导入数据 原理:读取文件,获取文 ...
- 网站用户行为分析——HBase的安装与配置
Hbase介绍 HBase是一个分布式的.面向列的开源数据库,源于Google的一篇论文<BigTable:一个结构化数据的分布式存储系统>.HBase以表的形式存储数据,表有行和列组成, ...
- HashMap底层实现原理及扩容机制
HashMap的数据结构:数组+链表+红黑树:Java7中的HashMap只由数组+链表构成:Java8引入了红黑树,提高了HashMap的性能:借鉴一张图来说明,原文:https://www.jia ...
- 【blockly教程】第六章 Blockly的进阶
6.1 模块化程序设计 一个较大的程序一般应分为若干个程序模块,每一个模块用来实现一个特定的功能.所有的高级语言中都有子程序这个概念,用子程序实现模块的功能.比如在C语言中,子程序的作用是由函数完成 ...
- linux redhat NFS网络共享搭建
nfs网络共享 测试环境: 服务端:redhat6.7 ip:192.168.1.100 客户端:redhat6.7 ip:192.168.1.110 一.服务端 1.创建共享文件夹 权限666即可 ...
- SQL Server 中对 FOR XML和FROM的转换处理
在SQL Server中对XML的再操作转换: 方法1: --生成XML SELECT * FROM [T_BAS_预算科目] FOR XML PATH --把XML转成SQL表 declare @X ...
- git配置config记住密码
设置记住密码(默认15分钟): git config --global credential.helper cache如果想自己设置时间,可以这样做: git config credential.he ...