POJ 1741 Tree 树的分治
原题链接:http://poj.org/problem?id=1741
题意:
给你棵树,询问有多少点对,使得这条路径上的权值和小于K
题解:
就。。大约就是树的分治
代码:
#include<iostream>
#include<climits>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdio>
#define MAX_N 10004
using namespace std; struct edge{
public:
int to,length;
edge(int t,int l):to(t),length(l){}
edge(){}
}; int N,K; vector<edge> G[MAX_N]; bool centroid[MAX_N];
int subtree_size[MAX_N]; int ans; int compute_subtree_size(int v,int p){
int c=;
for(int i=;i<G[v].size();i++){
int w=G[v][i].to;
if(w==p||centroid[w])continue;
c+=compute_subtree_size(G[v][i].to,v);
}
subtree_size[v]=c;
return c;
} pair<int,int> search_centroid(int v,int p,int t){
pair<int,int> res=make_pair(INT_MAX,-);
int s=,m=;
for(int i=;i<G[v].size();i++){
int w=G[v][i].to;
if(w==p||centroid[w])continue; res=min(res,search_centroid(w,v,t)); m=max(m,subtree_size[w]);
s+=subtree_size[w];
}
m=max(m,t-s);
res=min(res,make_pair(m,v));
return res;
} void enumeratr_paths(int v,int p,int d,vector<int> &ds){
ds.push_back(d);
for(int i=;i<G[v].size();i++){
int w=G[v][i].to;
if(w==p||centroid[w])continue;
enumeratr_paths(w,v,d+G[v][i].length,ds);
}
} int count_pairs(vector<int> &ds){
int res=;
sort(ds.begin(),ds.end());
int j=ds.size();
for(int i=;i<ds.size();i++){
while(j>&&ds[i]+ds[j-]>K)--j;
res+=j-(j>i?:);
}
return res/;
} void solve_subproblem(int v){
compute_subtree_size(v,-);
int s=search_centroid(v,-,subtree_size[v]).second;
centroid[s]=; for(int i=;i<G[s].size();i++){
if(centroid[G[s][i].to])continue;
solve_subproblem(G[s][i].to);
} vector<int> ds;
ds.push_back();
for(int i=;i<G[s].size();i++){
if(centroid[G[s][i].to])continue; vector<int> tds;
enumeratr_paths(G[s][i].to,s,G[s][i].length,tds); ans-=count_pairs(tds);
ds.insert(ds.end(),tds.begin(),tds.end());
}
ans+=count_pairs(ds);
centroid[s]=false;
} void solve(){
ans=;
solve_subproblem();
printf("%d\n",ans);
} bool input(){
if(scanf("%d%d",&N,&K)!=||N+K==)return false;
for(int i=;i<=N;i++)G[i].clear();
for(int i=;i<N-;i++){
int u,v;
int c;
scanf("%d%d",&u,&v);
scanf("%d",&c);
u--,v--;
G[u].push_back(edge(v,c));
G[v].push_back(edge(u,c));
}
return true;
} int main(){
while(input()){
ans=;
memset(subtree_size,,sizeof(subtree_size));
memset(centroid,,sizeof(centroid));
solve();
}
return ;
}
POJ 1741 Tree 树的分治的更多相关文章
- poj 1741 Tree (树的分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30928 Accepted: 10351 Descriptio ...
- 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(树的点分治,入门题)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21357 Accepted: 7006 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 树上点分治
题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...
- POJ 1741 Tree (点分治)
Tree Time Limit: 1000MS Memory ...
- poj 1741 Tree(点分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15548 Accepted: 5054 Description ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
随机推荐
- MySQL高可用之MHA安装
Preface MasterHA is a tool which can be used in MySQL HA architecture.I'm gonna implement it ...
- Canvas 剪切图片
/** * 剪切图像 */ function initDemo8(){ var canvas = document.getElementById("demo8"); if (!ca ...
- Mysql与Oracle之间的数据类型转换
MySQL Data Type Oracle Data Type BIGINT NUMBER(19, 0) BIT RAW BLOB BLOB, RAW CHAR CHAR DATE DATE DAT ...
- 抽象类和虚方法、base关键字
微软官方文档:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/abstract ...
- NodeJs02 美女爬虫
note: demo代码要编号 导出模块 一个js文件就是一个模块,模块内部的所有变量,对象,方法对外界都不可见.如果想暴漏出去让别人用,就需要导出模块.语法如下: module.exports = ...
- 三、vue依赖收集
Vue 会把普通对象变成响应式对象,响应式对象 getter 相关的逻辑就是做依赖收集,这一节我们来详细分析这个过程 Dep Dep 是整个 getter 依赖收集的核心,它的定义在 src/core ...
- Spring2集成iBatis2
从数据库中查询一条记录,演示Spring与iBatis的集成 1 编写sqlmaps与Domain对象 <?xml version="1.0" encoding=" ...
- Android应用如何打包?
android app开发结束后,就需要对app进行打包.部署与发布了,那对于android初学者而言,如何对apk进行打包呢?今天小编就为大家分享一二,一起来看看吧~~ aapt package - ...
- DP石子合并问题
转自:http://www.hnyzsz.net/Article/ShowArticle.asp?ArticleID=735 [石子合并] 在一个圆形操场的四周摆放着n 堆石子.现要将石子有次序 ...
- vue实现多个元素或多个组件之间动画效果
多个元素的过渡 <style> .v-enter,.v-leave-to{ opacity: 0; } .v-enter-acitve,.v-leave-active{ opacity: ...