http://poj.org/problem?id=1987 (题目链接)

题意

  给出一棵树,求树上距离不超过K的点对个数。

Solution

  点分治,同poj1741。

代码

// poj1987
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define MOD 100000000
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=40010;
struct edge {int to,next,w;}e[maxn<<1];
int head[maxn],deep[maxn],d[maxn],size[maxn],f[maxn],vis[maxn];
int n,m,K,cnt,rt,sum,ans; void link(int u,int v,int w) {
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].w=w;
e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;e[cnt].w=w;
}
void calroot(int x,int fa) {
size[x]=1;f[x]=0;
for (int i=head[x];i;i=e[i].next)
if (!vis[e[i].to] && e[i].to!=fa) {
calroot(e[i].to,x);
size[x]+=size[e[i].to];
f[x]=max(f[x],size[e[i].to]);
}
f[x]=max(f[x],sum-size[x]);
if (f[x]<f[rt]) rt=x;
}
void caldeep(int x,int fa) {
deep[++deep[0]]=d[x];
for (int i=head[x];i;i=e[i].next)
if (!vis[e[i].to] && e[i].to!=fa) {
d[e[i].to]=d[x]+e[i].w;
caldeep(e[i].to,x);
}
}
int cal(int x,int now) {
d[x]=now;deep[0]=0;
caldeep(x,0);
sort(deep+1,deep+deep[0]+1);
int t=0;
for (int l=1,r=deep[0];l<r;) {
if (deep[l]+deep[r]<=K) t+=r-l,l++;
else r--;
}
return t;
}
void solve(int x) {
vis[x]=1;
ans+=cal(x,0);
for (int i=head[x];i;i=e[i].next) if (!vis[e[i].to]) ans-=cal(e[i].to,e[i].w);
for (int i=head[x];i;i=e[i].next) if (!vis[e[i].to]) {
rt=0;sum=size[e[i].to];
calroot(e[i].to,0);
solve(rt);
}
}
int main() {
scanf("%d%d",&n,&m);
char ch;
for (int u,v,w,i=1;i<=m;i++) {
scanf("%d%d%d %c",&u,&v,&w,&ch);
if (u==v) continue;
link(u,v,w);
}
scanf("%d",&K);
rt=0;sum=n;f[0]=inf;
calroot(1,0);
solve(rt);
printf("%d",ans);
return 0;
}

  

【poj1987】 Distance Statistics的更多相关文章

  1. 【树形dp】【CF161D】distance on a tree + 【P1352】没有上司的舞会

    T1题面: 输入点数为N一棵树 求树上长度恰好为K的路径个数 (n < 1e5, k < 500) 这是今天的考试题,也是一道假的紫题,因为我一个根本不会dp的蒟蒻只知道状态就一遍A掉了- ...

  2. 【leetcode】1093. Statistics from a Large Sample

    题目如下: We sampled integers between 0 and 255, and stored the results in an array count:  count[k] is ...

  3. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  4. 【LeetCode】161. One Edit Distance

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...

  5. 【BZOJ2790】[Poi2012]Distance 筛素数+调和级数

    [BZOJ2790][Poi2012]Distance Description 对于两个正整数a.b,这样定义函数d(a,b):每次操作可以选择一个质数p,将a变成a*p或a/p, 如果选择变成a/p ...

  6. 【题解】UVA10140 [Prime Distance]

    [题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...

  7. 论文阅读笔记(二)【IJCAI2016】:Video-Based Person Re-Identification by Simultaneously Learning Intra-Video and Inter-Video Distance Metrics

    摘要 (1)方法: 面对不同行人视频之间和同一个行人视频内部的变化,提出视频间和视频内距离同时学习方法(SI2DL). (2)模型: 视频内(intra-vedio)距离矩阵:使得同一个视频更紧凑: ...

  8. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  9. 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

    Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. ...

  2. C# 多重overide

    overide 是覆盖的意思,用在且仅用在虚函数上,虚函数可以是virtual或abstract修饰的,或者是overide修饰的. 文档大概是这么说的. 由此知道,由overide修饰的函数都是虚函 ...

  3. 我使用celery以及docker部署遇到的问题

    首先我本机测试时没有问题的,但是在线上docker中,任务一直显示 "Sending due task".超时的任务是 django orm update 操作,本地不会出现这样的 ...

  4. Spring Security授权 AccessDecisionManager

    Spring Security授权 AccessDecisionManager 博客分类: Security Spring   在前面那篇博客有一段配置: <http auto-config=& ...

  5. 如何在 ie6 中使用 "localStorage"

    好吧,我只是个标题党,ie6 下根本无法使用跟 h5 沾边的 localStorage.今天要向大家介绍的是 ie 特有的 userData 的存储方式,并且对它进行封装,使得不支持 localSto ...

  6. [leetcode]算法题目 - Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  7. PowerShell Script to Deploy Multiple VM on Azure in Parallel #azure #powershell

    Since I need to deploy, start, stop and remove many virtual machines created from a common image I c ...

  8. [BZOJ 2819]NIM(dfs序维护树上xor值)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2819 分析: 树上的nim游戏,关键就是要判断树上的一条链的异或值是否为0 这个题目有 ...

  9. input用法,永远等待,直到用户输入值赋值给一个东西。

    input用法,永远等待,直到用户输入值赋值给一个东西. n1 = input('请输入用户名:') n1 = input('请输入密码:') print(n1) print(n1)

  10. php上传sae路径问题

    我们写php时,会有 define('POOT_PATH', dirname(__FILE__)); 但是sea中没有用,所以我们要用 define("ROOT_PATH", $_ ...