POJ 1987 Distance Statistics 树分治
Description
Input
* Line M+2: A single integer, K.
Output
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
10
Sample Output
5
Hint
题解:
POJ 1741
http://www.cnblogs.com/zxhl/p/5692688.html
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 4e4+, M = 1e2+, mod = 1e9+, inf = 1e9+;
typedef long long ll; int ans, n,m,root , t = ,K,siz[N],head[N],f[N],deep[N],d[N],allnode,vis[N];
struct edg{int to,next,v,w;}e[N * ];
void add(int u,int v,int w) {e[t].to=v;e[t].v=w;e[t].next=head[u];head[u]=t++;} void getroot(int x,int fa) {
siz[x] = ;
f[x] = ;
for(int i=head[x];i;i=e[i].next) {
int to = e[i].to;
if(to == fa || vis[to]) continue;
getroot(to,x);
siz[x] += siz[to];
f[x] = max(f[x] , siz[to]);
}
f[x] = max(f[x] , allnode - siz[x]);
if(f[x] < f[root]) root = x;
}
void getdeep(int x,int fa) {
if(d[x] <= K) deep[++deep[]]=d[x];
for(int i=head[x];i;i=e[i].next) {
int to = e[i].to;
if(to == fa || vis[to]) continue;
d[to] = d[x] + e[i].v;
getdeep(to,x);
}
}
int cal(int x,int now) {
d[x]=now;deep[] = ;
getdeep(x,);
sort(deep+,deep+deep[]+);
int all = ;
for(int l=,r=deep[];l<r;) {
if(deep[l]+deep[r] <= K) {all+=r-l;l++;}
else r--;
}
return all;
}
void work(int x) {
ans+=cal(x,);
vis[x] = ;
for(int i=head[x];i;i=e[i].next) {
int to = e[i].to;
if(vis[to]) continue;
ans-=cal(to,e[i].v);
allnode = siz[to];
root = ;
getroot(to,root);
work(root);
}
}
void init()
{
memset(head,,sizeof(head));
t = ;
ans = root = ;
memset(vis,,sizeof(vis));
}
int main()
{
while(~scanf("%d%d",&n,&m)) {
init();
for(int i=;i<n;i++) {
int a,b,c;char ch[];
scanf("%d%d%d%s",&a,&b,&c,ch);
add(a,b,c) , add(b,a,c);
}
scanf("%d",&K);
allnode=n;f[]=inf;
getroot(,);
work(root);
printf("%d\n",ans);
} }
POJ 1987 Distance Statistics 树分治的更多相关文章
- POJ 1987 Distance Statistics(树的点分治)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 上场CF的C题是一个树的分治... 今天刚好又 ...
- POJ 1987 Distance Statistics
http://poj.org/problem?id=1987 题意:给一棵树,求树上有多少对节点满足距离<=K 思路:点分治,我们考虑把每个距离都存起来,然后排序,一遍扫描计算一下,注意还要减掉 ...
- POJ 1741 Tree【树分治】
第一次接触树分治,看了论文又照挑战上抄的代码,也就理解到这个层次了.. 以后做题中再慢慢体会学习. 题目链接: http://poj.org/problem?id=1741 题意: 给定树和树边的权重 ...
- POJ 1741 Tree ——(树分治)
思路参考于:http://blog.csdn.net/yang_7_46/article/details/9966455,不再赘述. 复杂度:找树的重心然后分治复杂度为logn,每次对距离数组dep排 ...
- POJ 1987 BZOJ 3365 Distance Statistics 树的分治(点分治)
题目大意:(同poj1741,刷一赠一系列) CODE: #include <cstdio> #include <cstring> #include <iostream& ...
- POJ 1741 Tree (树分治入门)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8554 Accepted: 2545 Description ...
- poj 2114 Boatherds (树分治)
链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #includ ...
- BZOJ 3365 Distance Statistics 点分治
这道题是一道点分治的题目,难度不大,可以拿来练手. 关键是对于找出来的重心的删除操作需要删掉这条边,这很重要. 还有每次找重心的时候,不但要考虑他的子节点的siz,还要考虑父节点的siz. 然后就A了 ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
随机推荐
- Shell 读取文本内容
在Linux中有很多方法逐行读取一个文件的方法,其中最常用的就是下面的脚本里的方法,而且是效率最高,使用最多的方法.为了给大家一个直观的感受,我们将通过生成一个大的文件的方式来检验各种方法的执行效率. ...
- HBase独立集群部署
HBase是分布式.面向列式存储的开源数据库,来源于Google的论文BigTable,HBase运行于Hadoop平台之上,不同于一般的关系数据库,是一个适合非结构化数据存储的分布式数据库 安装Hb ...
- Delphi XE5 android 获取网络状态
unit Androidapi.JNI.Network; interface function IsConnected: Boolean; function IsWiFiConnected: Bool ...
- ABAP 自建透明表维护
*&---------------------------------------------------------------------* *& Report ZMMR011 ...
- iOS SHA1加密实现方法
使用方法 先导入头文件 #import "SHA1.h" //SHA1测试 NSString* sh1=[SHA1 getSha1String:"]; NSLog(@&q ...
- php 获取IP
<?php echo 'your ip is :'; if (@$_SERVER["HTTP_X_FORWARDED_FOR"]) $ip = $_SERVER[" ...
- Effective C++ -----条款52:写了placement new 也要写 placement delete
当你写一个placement operator new ,请确定也写出了对应的placement operator delete.如果没有这样做,你的程序可能会发生隐微而时断时续的内存泄漏. 当你声明 ...
- 如何让两个 并列的div高度相等
哪个div Height值大,就将其值赋给Height值小的div,从而使2个div高度始终保持一致. function $(id){ return document.getElementById(i ...
- ORACLE 远程导入导出数据库
Oracle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的数据库服务器中. ...
- jquery中dom元素的attr和prop方法的理解
一.背景 在编写使用高版本[ jQuery 1.6 开始新增了一个方法 prop()]的jquery插件进行编写js代码的时候,经常不知道dom元素的attr和prop方法到底有什么区别?各自有什么应 ...