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的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
随机推荐
- Selenium+Python自动化之如何绕过登录验证码
一.使用Fiddler抓包 1.一般登陆网站成功后,会生成一个已登录状态的cookie,那么只需要直接把这个值拿到,用selenium进行addCookie操作即可. 2.可以先手动登录一次,然后抓取 ...
- GLIBCXX3.4.21 not find
在执行世界杯的二进制代码和安装keepaway中会遇到GLIBCXX3.4.21 not find的问题,其解决办法就是升级安装GCC. 一.首先查看当前gcc版本 $ strings /usr/li ...
- mac虚拟机上(centos系统)怎样实现共享本机文件
首先加载vboxadditions,可以从https://download.virtualbox.org/virtualbox/下载,记得一定要跟virtualBox版本对应 然后打开virtualb ...
- python基础实践(二)
-*-越简单越快乐-*-# -*- coding:utf-8 -*-# Author:sweeping-monkQuestion_1 = "python中的整数运算"Method_ ...
- 项目中使用ECharts插件实现统计功能
一.前端界面 // 界面中定义一个div,放图表 <div id="box" style="width: 600px;height:400px;padding: 1 ...
- Entity Framework(一)
相关知识点复习: 1.var 类型推断: var p=new Person(); 2.匿名类型: var a=new {Name="wang",Age=12 }; 3.给新创建的 ...
- C# http Post与Get方法控制继电器
---恢复内容开始--- using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...
- 课时46:魔法方法:描述符(property的原理)
目录: 一.描述符(property的原理) 二.课时46课后习题及答案 ********************************** 一.描述符(property的原理) ********* ...
- vue实现数据的增删改查
在管理员的一些后台页面里,个人中心里的数据列表里,都会有对这些数据进行增删改查的操作.比如在管理员后台的用户列表里,我们可以录入新用户的信息,也可以对既有的用户信息进行修改.在vue中,我们更应该专注 ...
- CSS3的笔记总结
css3 就是css2 的一个升级版本.css2 是用来做效果渲染的,而css3 可以使做出来的效果更佳丰富. C3有兼容性问题,移动端支持稍微要好些. 坚持以下原则: ...