POJ 1741/1987 树的点分治
树的点分治,主要思想是每次找子树的重心,计算经过根节点的情况数,再减去点对属于同一子树的情况。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <cassert>
#include <sstream>
using namespace std; const int N=;
struct Edge {
int to,next;
int w;
Edge(){}
Edge (int _t,int _n,int _w=) {
to=_t;
next=_n;
w=_w;
}
}edge[N<<];
int idx,head[N];
void addEdge (int u,int v,int w) {
++idx;
edge[idx]=Edge(v,head[u],w);
head[u]=idx;
}
bool vis[N]; int K;// 输入中的k int dis[N],disCnt;
void getDis(int u,int f,int d) {
dis[disCnt++]=d;
for (int k=head[u];k;k=edge[k].next) {
int v=edge[k].to;
if (vis[v]||v==f) continue;
getDis(v,u,d+edge[k].w);
}
} int bal,cmp;
int getBal(int u,int f) {
int son=;
int dp=;
for (int k=head[u];k;k=edge[k].next) {
int v=edge[k].to;
if (vis[v]||v==f) continue;
int subSon=getBal(v,u);
son+=subSon;
dp=max(dp,subSon);
}
dp=max(dp,disCnt-son);
if (dp<cmp){
cmp=dp;
bal=u;
}
return son;
} int calc(int u,int initDis) {
disCnt=;
getDis(u,-,initDis);
sort(dis,dis+disCnt);
int ret=;
int l=,r=disCnt-;
while (l<r) {
if (dis[l]+dis[r]>K) r--;
else ret+=r-l++;
}
return ret;
} int ans=;
void solve(int u) {
cmp=~0U>>;
getBal(u,-);
ans+=calc(bal,);
vis[bal]=true;
for (int k=head[bal];k;k=edge[k].next) {
int v=edge[k].to;
if (vis[v]) continue;
ans-=calc(v,edge[k].w);// 减去子树内的重复计算的情况
solve(v);
}
} void init(int n) {
idx=;memset(head,,sizeof head);
memset(vis,false,sizeof vis);
disCnt=n;// 由于在计算重心时,可以直接用之前算dis时的disCnt,所以这里初始化为n
ans=;
}
int main () {
int n;
while (scanf("%d %d",&n,&K)!=EOF) {
if (n==&&K==) break;
init(n);
for (int i=;i<n;i++) {
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
addEdge(u,v,w);
addEdge(v,u,w);
}
solve();
printf("%d\n",ans);
}
return ;
}
POJ 1741/1987 树的点分治的更多相关文章
- poj 1741(树的点分治)
Tree Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...
- POJ 1741 Tree(树的点分治,入门题)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21357 Accepted: 7006 Description ...
- POJ 1741 Tree 树的分治(点分治)
题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- POJ 1741 Tree 树的分治
原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- Tree POJ - 1741【树分治】【一句话说清思路】
因为该博客的两位作者瞎几把乱吹(" ̄︶ ̄)人( ̄︶ ̄")用彼此的智慧总结出了两条全新的定理(高度复杂度定理.特异根特异树定理),转载请务必说明出处.(逃 Pass:anuonei, ...
- poj 1741 Tree (树的分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30928 Accepted: 10351 Descriptio ...
- POJ 1741 Tree 树形DP(分治)
链接:id=1741">http://poj.org/problem?id=1741 题意:给出一棵树,节点数为N(N<=10000),给出N-1条边的两点和权值,给出数值k,问 ...
随机推荐
- Lambda&Java多核编程-5-函数式接口与function包
从前面的总结中我们知道Lambda的使用场景是实现一个函数式接口,那么本篇就将阐述一下何为函数式接口以及Java的function包中提供的几种函数原型. 函数式接口 早期也叫作SAM(Single ...
- 2017-03-10 T-sql 语句 高级查询
T-SQL语句: 创建数据库: 1,点击新建查询,在弹出的页面上进行代码编写.点击可用数据库,编写前确定当前操作的页面是自己想要进行操作的界面. 2,数据库创建语句 Create datebase ...
- Binary Search Tree Iterator leetcode
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- Single Number leetcode
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- Spring-boot 最小demo
POM 文件,注意红色部分: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...
- 玩转Node.js单元测试
代码部署之前,进行一定的单元测试是十分必要的,这样能够有效并且持续保证代码质量.而实践表明,高质量的单元测试还可以帮助我们完善自己的代码.这篇博客将通过一些简单的测试案例,介绍几款Node.js测试模 ...
- 【转】深入理解RunLoop
RunLoop 是 iOS 和 OS X 开发中非常基础的一个概念,这篇文章将从 CFRunLoop 的源码入手,介绍 RunLoop 的概念以及底层实现原理.之后会介绍一下在 iOS 中,苹果是如何 ...
- db2 sequence 查询
1. 查询名字 select * from sysibm.sysequences where seqname='wx_Id' 2.nextVal select wx_seq_id.currval fr ...
- 头文件limits—各个类型的数据的范围
要想知道各个类型的数据如int.float.double.long等所能表示的范围,可以加上头文件<limits>,这些类型的范围都在类numeric_limits中定义了的. 类模板:t ...
- Android开发之AsyncTask示例Demo
今天做了一个AsyncTask的小Demo,内含注释,通过此Demo,可以对AsyncTask有一个详细的了解 已经将项目上传到了GitHub上(程序有一个小bug,在第一次提交有说明,有解决方法请留 ...