POJ 1741/1987 树的点分治
树的点分治,主要思想是每次找子树的重心,计算经过根节点的情况数,再减去点对属于同一子树的情况。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <cassert>
#include <sstream>
using namespace std; const int N=;
struct Edge {
int to,next;
int w;
Edge(){}
Edge (int _t,int _n,int _w=) {
to=_t;
next=_n;
w=_w;
}
}edge[N<<];
int idx,head[N];
void addEdge (int u,int v,int w) {
++idx;
edge[idx]=Edge(v,head[u],w);
head[u]=idx;
}
bool vis[N]; int K;// 输入中的k int dis[N],disCnt;
void getDis(int u,int f,int d) {
dis[disCnt++]=d;
for (int k=head[u];k;k=edge[k].next) {
int v=edge[k].to;
if (vis[v]||v==f) continue;
getDis(v,u,d+edge[k].w);
}
} int bal,cmp;
int getBal(int u,int f) {
int son=;
int dp=;
for (int k=head[u];k;k=edge[k].next) {
int v=edge[k].to;
if (vis[v]||v==f) continue;
int subSon=getBal(v,u);
son+=subSon;
dp=max(dp,subSon);
}
dp=max(dp,disCnt-son);
if (dp<cmp){
cmp=dp;
bal=u;
}
return son;
} int calc(int u,int initDis) {
disCnt=;
getDis(u,-,initDis);
sort(dis,dis+disCnt);
int ret=;
int l=,r=disCnt-;
while (l<r) {
if (dis[l]+dis[r]>K) r--;
else ret+=r-l++;
}
return ret;
} int ans=;
void solve(int u) {
cmp=~0U>>;
getBal(u,-);
ans+=calc(bal,);
vis[bal]=true;
for (int k=head[bal];k;k=edge[k].next) {
int v=edge[k].to;
if (vis[v]) continue;
ans-=calc(v,edge[k].w);// 减去子树内的重复计算的情况
solve(v);
}
} void init(int n) {
idx=;memset(head,,sizeof head);
memset(vis,false,sizeof vis);
disCnt=n;// 由于在计算重心时,可以直接用之前算dis时的disCnt,所以这里初始化为n
ans=;
}
int main () {
int n;
while (scanf("%d %d",&n,&K)!=EOF) {
if (n==&&K==) break;
init(n);
for (int i=;i<n;i++) {
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
addEdge(u,v,w);
addEdge(v,u,w);
}
solve();
printf("%d\n",ans);
}
return ;
}
POJ 1741/1987 树的点分治的更多相关文章
- poj 1741(树的点分治)
Tree Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...
- POJ 1741 Tree(树的点分治,入门题)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21357 Accepted: 7006 Description ...
- 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 树的分治
原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- Tree POJ - 1741【树分治】【一句话说清思路】
因为该博客的两位作者瞎几把乱吹(" ̄︶ ̄)人( ̄︶ ̄")用彼此的智慧总结出了两条全新的定理(高度复杂度定理.特异根特异树定理),转载请务必说明出处.(逃 Pass:anuonei, ...
- poj 1741 Tree (树的分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30928 Accepted: 10351 Descriptio ...
- POJ 1741 Tree 树形DP(分治)
链接:id=1741">http://poj.org/problem?id=1741 题意:给出一棵树,节点数为N(N<=10000),给出N-1条边的两点和权值,给出数值k,问 ...
随机推荐
- 69个微信小程序常见问题
本文转自 遇到小程序方面的问题,该去哪里提问呢? 若是能得到微信官方的解答,想必是最叫人安心的.而微信也确实提供了这么一个地方. 在微信公众平台的开发者社区,就置顶了一个「小程序常见问题 FAQ」帖. ...
- Protobuf 在Ubuntu 14上的编译与使用
前言 一直知道Google开源的一个与语言无关的数据交换协议:protobuf.只知道是一种不同于json和XML的格式,还有就是性能特别的好(这在Java和C++的实现确实是!) 最近闲下来看了下G ...
- 字符集&各种编码&编码解码
要理解乱码问题,首先需要理解几个概念:字符集.编码.编码规则.乱码 1. 字符集: 字符(Character)是各种文字和符号的总称,包括各国家文字.标点符号.图形符号.数字等.字符集(Charact ...
- java学习笔记----运算符
一.算数运算符 特别说明: 加 ,减 ,乘 ,除 与数学运算一致 取余符号看被除数 自加(减)运算:++a,--a;先做自加(自减)运算在做其他运算 a++,a--;先做其他运算在做自加(自减)运算 ...
- mac开发环境配置
折腾了好几天了,终于安装一部分了,mac装的win10,太占空间了,看到学习资源使用的工具,自己搜了一下安装了,在学习使用git的时候,都说mac比win好用多了,我tm为啥抱着mac装win费劲呢! ...
- CodeBlocks常用重要快捷键大全!!
CodeBlocks常用操作快捷键 编辑部分: Ctrl + A:全选 Ctrl + C:复制 Ctrl + X: 剪切 Ctrl + V:粘贴 Ctrl + Z:撤销(后退一步) Ctrl + S: ...
- 小程序新能力-个人开发者尝鲜微信小程序
个人开发者的福利 微信小程序,刚听到这个新名词的时候,我就兴冲冲的去找入口,看看自己能不能搞个微信小程序的HelloWorld,毕竟能在微信上把自己写的一些小工具跑起来还是满炫酷的. 没想,网上一查, ...
- C++ 拷贝控制和资源管理,智能指针的简单实现
C++ 关于拷贝控制和资源管理部分的笔记,并且介绍了部分C++ 智能指针的概念,然后实现了一个基于引用计数的智能指针.关于C++智能指针部分,后面会有专门的研究. 通常,管理类外资源的类必须定义拷贝控 ...
- 虚拟机Linux 的一些基础命令和注释
cd命令 cd ==回到初始,主目录 cd - ==回到上一级目录交替 cd ~ ==回到root家目录 cd . ==当前目录 cd .. ==进入上一级目录 ls命令 ls == ...
- 关于ng-class的用法
ng-class的使用几种方式 (1):利用双向数据绑定(className根据chang2的值去匹配类) <div class="{{className}}">... ...