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 ...
随机推荐
- java web 学习 --第三天(Java三级考试)
第二天的学习内容这里:http://www.cnblogs.com/tobecrazy/p/3446646.html Jsp中的动作标签 <jsp:include> 实现动态包含,在一个文 ...
- 比较两个mysql数据库表结构的差异
需求来源:一个线上系统,一个开发系统,现在要把开发系统更新到线上,但是开发系统的数据库结构与线上的略有差异,所以需要找出两个数据库的表结构差异. 数据库表结构的差异 注:操作均在Linux系统下完成 ...
- Enum:Face The Right Way(POJ 3276)
面朝大海,春暖花开 题目大意:农夫有一群牛,牛排成了一排,现在需要把这些牛都面向正确的方向,农夫买了一个机器,一次可以处理k只牛,现在问你怎么处理这些牛才可以使操作数最小? 这道题很有意思,其实这道题 ...
- linux 用户、用户组不能是全数字
今天封装命令行,需要创建用户.用户组,遇到下面问题,如图: 当时我和迷茫,为什么明明存在‘1111’这个用户组,但是却提示不存在呢??难道是linux的一个bug??? 接着我又试了几个: 发现规律了 ...
- 20145213《Java程序设计》第四周学习总结
20145213<Java程序设计>第四周学习总结 教材学习内容总结 本周任务是学习面向对象的继承.接口以及之后的如何活用多态.(还真是路漫漫其修远兮啊!)教材也是延续上周艰深晦涩的语言风 ...
- UISrollView
1. contentOffset 默认CGPointZero,用来设置scrollView的滚动偏移量. // 设置scrollView的滚动偏移量 scrollView.contentOffset ...
- java中带继承类的加载顺序详解及实战
一.背景: 在面试中,在java基础方面,类的加载顺序经常被问及,很多时候我们是搞不清楚到底类的加载顺序是怎么样的,那么今天我们就来看看带有继承的类的加载顺序到底是怎么一回事?在此记下也方便以后复习巩 ...
- ios创建二维码
#import "LCTwoCodeImage.h" @implementation LCTwoCodeImage +(UIImage *) GotoCreatMyTwoCode ...
- ubuntu 添加环境变量
转自:http://hi.baidu.com/mario05/item/02b6d60ff7371136a2332a48 Ubuntu Linux系统环境变量配置文件:/etc/profile : 在 ...
- Android 菜单定制使用小结
GridView 添加 ArrayAdapter<Bean>, 在 gridView 的 Item 点击之后, 1.list.add(bean)/remove(bean) 2.adapte ...