poj1741 (点分治)
Problem Tree
题目大意
给一棵树,有边权。求树上距离小于等于K的点对有多少。
解题分析
点分治。对每一棵子树进行dfs,求出每棵子树的重心,继而转化为子问题。
对于经过根的路径i--j,令dep为到根距离,那么需求出dep[i]+dep[j]<=k,且i,j属于不同子树,可以求其补集,求出属于同一子树的对答案贡献。
参考程序
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std; #define N 100008
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define clr(x,v) memset(x,v,sizeof(x));
#define bitcnt(x) __builtin_popcount(x)
#define rep(x,y,z) for (int x=y;x<=z;x++)
#define repd(x,y,z) for (int x=y;x>=z;x--)
const int mo = ;
const int inf = 0x3f3f3f3f;
const int INF = ;
/**************************************************************************/ int n,k,sum,tot,root,ans;
int lt[N],f[N],vis[N],size[N],a[N];
struct line{int u,v,w,nt;}eg[N*];
vector <int> vec;
void add(int u,int v,int w)
{
eg[++sum]=(line){u,v,w,lt[u]};
lt[u]=sum;
}
void init()
{
clr(lt,); sum=; ans=;
clr(f,); f[]=INF;
clr(vis,);
}
void getRoot(int u,int fa)
{
size[u]=; f[u]=;
for (int i=lt[u];i;i=eg[i].nt)
{
int v=eg[i].v;
if (vis[v] || v==fa) continue;
getRoot(v,u);
size[u]+=size[v];
f[u]=max(f[u],size[v]);
}
f[u]=max(f[u],tot-size[u]);
if (f[u]<f[root]) root=u;
}
void getDeep(int u,int fa)
{
vec.push_back(a[u]);
for (int i=lt[u];i;i=eg[i].nt)
{
int v=eg[i].v;
if (v==fa || vis[v]) continue;
a[v]=a[u]+eg[i].w;
getDeep(v,u);
}
}
int cal(int u,int key)
{
a[u]=key; vec.clear();
getDeep(u,);
sort(vec.begin(),vec.end());
int tmp=,l=,r=vec.size()-;
while (l<r)
{
if (vec[l]+vec[r]<=k)
{
tmp+=r-l;
l++;
}
else r--;
}
return tmp;
}
void work(int u)
{
ans+=cal(u,);
vis[u]=;
for (int i=lt[u];i;i=eg[i].nt)
{
int v=eg[i].v;
if (vis[v]) continue;
ans-=cal(v,eg[i].w);
tot=size[v]; root=;
getRoot(v,u);
work(root);
}
}
int main()
{
while (~scanf("%d%d",&n,&k))
{
if (n==) break;
init();
rep(i,,n-)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w); add(v,u,w);
}
tot=n; root=;
getRoot(,);
work(root);
printf("%d\n",ans);
}
}
poj1741 (点分治)的更多相关文章
- POJ1741 点分治模板
传送门:http://poj.org/problem?id=1741 题意: 求树上两点间路径长度小于k的点对个数 题解: 参考资料 守望的淀粉质略解:https://www.luogu.org/bl ...
- POJ-1741 树上分治--点分治(算法太奇妙了)
给你1e5个节点的树,(⊙﹏⊙) 你能求出又几对节点的距离小于k吗??(分治NB!) 这只是一个板子题,树上分治没有简单题呀!(一个大佬说的) #include<cstdio> #incl ...
- Codeforces 293E 点分治+cdq
Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...
- POJ1741 Tree(树分治——点分治)题解
题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...
- 【POJ1741】Tree(点分治)
[POJ1741]Tree(点分治) 题面 Vjudge 题目大意: 求树中距离小于\(K\)的点对的数量 题解 完全不觉得点分治了.. 简直\(GG\),更别说动态点分治了... 于是来复习一下. ...
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- [bzoj1468][poj1741]Tree_点分治
Tree bzoj-1468 poj-1741 题目大意:给你一颗n个点的树,求树上所有路径边权和不大于m的路径条数. 注释:$1\le n\le 4\cdot 10^4$,$1\le m \le 1 ...
- Cogs 1714. [POJ1741][男人八题]树上的点对(点分治)
[POJ1741][男人八题]树上的点对 ★★★ 输入文件:poj1741_tree.in 输出文件:poj1741_tree.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] ...
- poj1741(入门点分治)
题目链接:https://vjudge.net/problem/POJ-1741 题意:给出一棵树,求出树上距离不超过k的点对数量. 思路:点分治经典题.先找重心作为树根,然后求出子树中所有点到重心的 ...
- POJ1741——Tree(树的点分治)
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...
随机推荐
- 使用VC6.0创建和运行C程序的方法
使用VC6.0可以有快捷的方式创建C程序,对于初学者,建议按照如下方式创建.先创建一个工作区,然后创建工程,最后在工程中创建源文件文件.理解工作区.工程与文件之间的关系.
- hello 漂亮的小靓仔
<form type="text" name="超级" method="post"> <table align=" ...
- css选择符
E>F:子选择符,选择所有作为E元素的子元素F.<style type="text/css">li>a {color: #ccc;}</style&g ...
- sql 多行转换为一行
select 字段1, [val]=( select 字段2 +',' from 表名 as b where b.字段1 = a.字段1 for xml path('')) from 表名 as a ...
- pod
在运行 “sudo gem install cocoapods” 的时候出现问题:ERROR: While executing gem ... (Errno::EPERM)Operation not ...
- iOS语音
<span style="white-space:pre"> </span>语音技术近来可是出遍了风头,从iphone4s的siri,到微信的语音聊天 ...
- 用C#实现的内存映射
当文件过大时,无法一次性载入内存时,就需要分次,分段的载入文件 主要是用了以下的WinAPI LPVOID MapViewOfFile(HANDLE hFileMappingObject, DWORD ...
- Codeforces Round #380 (Div. 1, Rated, Based on Technocup 2017 - Elimination Round 2)
http://codeforces.com/contest/737 A: 题目大意: 有n辆车,每辆车有一个价钱ci和油箱容量vi.在x轴上,起点为0,终点为s,中途有k个加油站,坐标分别是pi,到每 ...
- iOS 给NSString文字上添加横线 中间和下划线
有时候我们需要给文字添加横线,有两种情况: 第一种是贯穿中间的横线: 横线的颜色和文字的颜色保持一致 _oldPriceLabel.text = "; _oldPriceLabel.text ...
- VS 2012 No exports were found that match the constraint 解决办法
VS 2012 No exports were found that match the constraint 解决办法 删除C:\Users\你的用户名\AppData\Local\Microsof ...