poj 1741 Tree(点分治)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 15548 | Accepted: 5054 |
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
input contains several test cases. The first line of each test case
contains two integers n, k. (n<=10000) The following n-1 lines each
contains three integers u,v,l, which means there is an edge between node
u and v of length l.
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
【思路】
设i到当前root的距离为d[i],i属于belong[i]->belong[i]为当前root的儿子且i在belong[i]为根的树中。设Sum{E}为满足条件E的点对数。
情况分为两种:
1) 经过根节点
2) 不经过根节点,在根节点的一颗子树中。
其中2)可以递归求解。
对于1)我们要求的是Sum{d[i]+d[j]<=k && belong[i]!=belong[j]},即为
Sum{d[i]+d[j]<=k} - Sum{ d[i]+d[j]<=k && belong[i]==belong[j]}
前后两项都可以转化为求一个序列a中满足d[a[i]]+d[a[j]]<=k的点对数。
先将a按照d值排序,基于单调性,我们可以给出一个O(n)的统计方法。于是问题得到解决。
总的时间复杂度为O(nlog2n)
【代码】
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std; const int N = +;
const int INF = 1e9; struct Edge{
int u,v,w;
Edge(int u=,int v=,int w=):u(u),v(v),w(w){};
};
int n,K,l1,l2,tl,ans;
int siz[N],d[N],list[N],f[N],can[N];
vector<Edge> es;
vector<int> g[N];
void adde(int u,int v,int w) {
es.push_back(Edge(u,v,w));
int m=es.size(); g[u].push_back(m-);
} void init() {
ans=; es.clear();
memset(can,,sizeof(can));
for(int i=;i<=n;i++) g[i].clear();
}
void dfs1(int u,int fa) {
siz[u]=;
list[++tl]=u;
for(int i=;i<g[u].size();i++) {
int v=es[g[u][i]].v;
if(v!=fa && can[v]) {
dfs1(v,u);
f[v]=u; siz[u]+=siz[v];
}
}
}
int getroot(int u,int fa) { //寻找u子树重心
int pos,mn=INF;
tl=;
dfs1(u,fa);
for(int i=;i<=tl;i++) {
int y=list[i],d=;
for(int j=;j<g[y].size();j++) {
int v=es[g[y][j]].v;
if(v!=f[y] && can[v]) d=max(d,siz[v]);
}
if(y!=u) d=max(d,siz[u]-siz[y]); //上方
if(d<mn) mn=d , pos=y; //使大子结点数最小
}
return pos;
}
void dfs2(int u,int fa,int dis) {
list[++l1]=u; d[u]=dis;
for(int i=;i<g[u].size();i++) {
int v=es[g[u][i]].v;
if(v!=fa && can[v]) dfs2(v,u,dis+es[g[u][i]].w);
}
}
int getans(int* a,int l,int r) {
int res=,j=r;
for(int i=l;i<=r;i++) {
while(d[a[i]]+d[a[j]]>K && j>i) j--;
res+=j-i; if(i==j) break;
}
return res;
}
bool cmp(const int& x,const int& y) { return d[x]<d[y]; }
void solve(int u,int fa) {
int root=getroot(u,fa);
l1=l2=;
for(int i=;i<g[root].size();i++) { //统计 d[i]+d[j]<=K && belong[i]==belong[j]
int v=es[g[root][i]].v;
if(can[v]) {
l2=l1;
dfs2(v,root,es[g[root][i]].w); //insert[以v为根的子树]
sort(list+l2+,list+l1+,cmp);
ans-=getans(list,l2+,l1);
}
}
list[++l1]=root; d[root]=can[root]=;
sort(list+,list+l1+,cmp);
ans+=getans(list,,l1); //统计d[i]+d[j]<=K
for(int i=;i<g[root].size();i++) { //递归<-分治
int v=es[g[root][i]].v;
if(v!=fa && can[v]) solve(v,root);
}
} int main() {
while(scanf("%d%d",&n,&K)== && (n&&K)) {
int u,v,w;
init();
for(int i=;i<n-;i++) {
scanf("%d%d%d",&u,&v,&w);
adde(u,v,w) , adde(v,u,w);
}
solve(,-);
printf("%d\n",ans);
}
return ;
}
poj 1741 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 ...
- [bzoj 1468][poj 1741]Tree [点分治]
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ 1741 Tree(点分治点对<=k)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ 1741 Tree ——点分治
[题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...
- [poj 1741]Tree 点分治
题意 求树上距离不超过k的点对数,边权<=1000 题解 点分治. 点分治的思想就是取一个树的重心,这种路径只有两种情况,就是经过和不经过这个重心,如果不经过重心就把树剖开递归处 ...
- POJ - 1741 - Tree - 点分治 模板
POJ-1741 题意: 对于带权的一棵树,求树中距离不超过k的点的对数. 思路: 点分治的裸题. 将这棵树分成很多小的树,分治求解. #include <algorithm> #incl ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量
POJ 1741. Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34141 Accepted: 11420 ...
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
随机推荐
- IntelliJ IDEA 部署Tomcat及创建一个web工程
一.部署Tomcat 二.新建一个web工程 1.新建一个Project 2.现在建立一个简单的web工程,所以只勾选下面选中的,此外,本版本(IntelliJ IDEA 14.1.5只支持3.1版本 ...
- 30分钟让你了解MongoDB基本操作
今天记录下MongoDB的基本操作,这只是最基本的,所以是应该掌握的. 数据库 数据库是一个物理容器集合.每个数据库都有自己的一套文件系统上的文件.一个单一的MongoDB服务器通常有多个数据库. 集 ...
- [转载]汇编eax寄存器和AX,AH,AL之间的关系
00000000 00000000 00000000 00000000|===============EAX===============|---32个0,4个字节,2个字,1个双字 ...
- spoj 2178
好水...... #include<cstdio> #include<cstdlib> #include<cstring> #include<algorith ...
- C/C++中几种经典的垃圾回收算法
1.引用计数算法 引用计数(Reference Counting)算法是每个对象计算指向它的指针的数量,当有一个指针指向自己时计数值加1:当删除一个指向自己的指针时,计数值减1,如果计数值减为0,说明 ...
- eclipse查看jar包中class的中文注释乱码问题的解决
1,问题来源是在eclipse中直接查看springside的class(由eclipse自动反编译)里面注释的乱码问题: Preferences-General-Workspace-Text fil ...
- jsp关于include html、jsp等文件出现乱码问题的解决方案
一般来说使用jsp标签<jsp:include>引入一个jsp文件: ①可以在被引入的jsp中加入:<%@ page contentType="text/html;char ...
- 双机高可用、负载均衡、MySQL(读写分离、主从自动切换)架构设计
前几天网友来信说帮忙实现这样一个架构:只有两台机器,需要实现其中一台死机之后另一台能接管这台机器的服务,并且在两台机器正常服务时,两台机器都能用上.于是设计了如下的架构. 架构简介 此架构主要是由ke ...
- pyqt实践——从裸机到打包安装
1 安装python 安装python-2.7.6.msi默认在c盘 设置环境变量,path后追加c:/python27.可以在命令行直接认识命令python 2 安装pyqt PyQt4-4.10- ...
- nginx+lua+redis实现logserver
http://www.baidu.com/s?wd=nginx lua&pn=10&oq=nginx lua&tn=baiduhome_pg&ie=utf-8& ...