POJ 1741 Tree ——(树分治)
思路参考于:http://blog.csdn.net/yang_7_46/article/details/9966455,不再赘述。
复杂度:找树的重心然后分治复杂度为logn,每次对距离数组dep排序复杂度为nlogn,而找重心的复杂度为dfs的复杂度——O(n),因此总的复杂度为O(nlognlogn)。
因为第一次写树分治,留个代码:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
const int N = + ; struct edge
{
int v,w;
};
int n,k,root,sz,ans;
bool vis[N];
int son[N],f[N],d[N];
vector<edge> G[N];
vector<int> dep;
void init()
{
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++) G[i].clear();
memset(son,,sizeof son);
memset(f,,sizeof f);
}
void addEdge(int u,int v,int w)
{
G[u].push_back((edge){v,w});
G[v].push_back((edge){u,w});
}
void getRoot(int u,int fa)
{
son[u] = ; f[u] = ;
for(int i=;i<G[u].size();i++)
{
edge e = G[u][i];
int v = e.v;
if(v == fa || vis[v]) continue;
getRoot(v,u);
son[u] += son[v];
f[u] = max(f[u], son[v]);
}
f[u] = max(f[u], sz-son[u]);
if(f[u] < f[root]) root = u;
}
void getDep(int u,int fa)
{
dep.push_back(d[u]);
for(int i=;i<G[u].size();i++)
{
edge e = G[u][i];
int v = e.v, w= e.w;
if(v == fa || vis[v]) continue;
d[v] = d[u] + w;
getDep(v,u);
}
}
int cal(int u,int len)
{
dep.clear(); d[u] = len;
getDep(u, );
sort(dep.begin(), dep.end());
int ret = ;
for(int l=,r=dep.size()-;l<r;)
{
if(dep[l] + dep[r] <= k) ret += r - (l++);
else r--;
}
return ret;
}
void solve(int u)
{
ans += cal(u, );
vis[u] = ;
for(int i=;i<G[u].size();i++)
{
edge e = G[u][i];
int v = e.v, w = e.w;
if(vis[v]) continue;
ans -= cal(v, w);
f[] = sz = son[v];
getRoot(v, root=);
solve(root);
}
} int main()
{
while(scanf("%d%d",&n,&k) == )
{
if(n == && k == ) break;
init();
for(int i=;i<=n-;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w);
}
f[] = sz = n;
getRoot(,root=);
ans = ;
solve(root);
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 ...
- poj 1741 Tree (树的分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30928 Accepted: 10351 Descriptio ...
- poj 1744 tree 树分治
Tree Time Limit: 1000MS Memory Limit: 30000K Description Give a tree with n vertices,each ed ...
- POJ 1741 Tree ——点分治
[题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...
- POJ 1741 Tree(树的点分治,入门题)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21357 Accepted: 7006 Description ...
- POJ 1741 Tree 树的分治
原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...
- Tree POJ - 1741【树分治】【一句话说清思路】
因为该博客的两位作者瞎几把乱吹(" ̄︶ ̄)人( ̄︶ ̄")用彼此的智慧总结出了两条全新的定理(高度复杂度定理.特异根特异树定理),转载请务必说明出处.(逃 Pass:anuonei, ...
- POJ 1741 Tree 树的分治(点分治)
题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...
- POJ 1741 Tree(点分治点对<=k)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
随机推荐
- .Net C# RSA签名和验签重写
namespace com._80community.unittest.CUP { /// <summary> /// CUP Client /// </summary> pu ...
- Path.Combine(
// 获取程序的基目录. var dir1 = System.AppDomain.CurrentDomain.BaseDirectory; // 获取模块的完整路径. var dir2 = Syste ...
- 逆向知识第九讲,switch case语句在汇编中表达的方式
一丶Switch Case语句在汇编中的第一种表达方式 (引导性跳转表) 第一种表达方式生成条件: case 个数偏少,那么汇编中将会生成引导性的跳转表,会做出 if else的情况(类似,但还是能分 ...
- 事件处理程序EventUtil
/**********事件处理程序***********EventUtil.js*浏览器兼容,<高三>13章 P354*2014-12-8************************* ...
- javaIO——BufferedWriter
[环境] jdk1.8 前面学习过 BufferedReader,是缓冲字符输入流.那么今天来学习对应的缓冲字符输出流类:BufferedWriter.跟 BufferedReader 同理,它也是一 ...
- DX使用随笔--NavBarControl
1. Item图标大小显示 需要先设置此Item所在Group的属性GroupStyle的值为LargeImageText.
- 使用openSSL构造一个支持https的nodejs服务器
首先通过下面的链接下载openSSL https://slproweb.com/products/Win32OpenSSL.html 下载完毕后,执行openssl进入交互式界面: 使用命令生成pri ...
- python监控CPU/内存/磁盘,超过指定百分比,发送邮件
#!/usr/bin/python #coding:utf-8 #导入psutil模块 import psutil import yagmail def mail(subject,contents): ...
- python实现IP地址转换为32位二进制
python实现IP地址转换为32位二进制 #!/usr/bin/env python # -*- coding:utf-8 -*- class IpAddrConverter(object): de ...
- 018.查询练习50题(sql实例)
CREATE TABLE EMP(EMPNO numeric(5,0) NOT NULL primary key,--雇员的编号ENAME nvarchar(10) not null,--雇员的名字J ...