POJ 1741 Tree【树分治】
第一次接触树分治,看了论文又照挑战上抄的代码,也就理解到这个层次了。。
以后做题中再慢慢体会学习。
题目链接:
http://poj.org/problem?id=1741
题意:
给定树和树边的权重,求有多少对顶点之间的边的权重之和小于等于K。
分析:
树分治。
直接枚举不可,我们将树划分成若干子树。
那么两个顶点有两种情况:
- u,v属于同一子树的顶点对
- u,v属于不同子树的顶点对
第一种情况,对子树递归即可求得。
第二种情况,从u到v的路径必然经过了顶点s,只要先求出每个顶点到s的距离再做统计即可。(注意在第二种情况中减去第一种重复计算的部分)
当树退化成链的形式时,递归的深度则退化为O(n),所以选择每次都找到树的重心作为分隔顶点。重心就是删掉此结点后得到的最大子树的顶点数最少的顶点,删除重心后得到的所有子树顶点数必然不超过n/2。
查找重心的时候,假设根为v,先在v的子树中找到一个顶点,使删除该顶点后的最大子树的顶点数最少,然后考虑删除v的情况,获得最大子树的顶点数。
两者选择最小的一个,此时选中的顶点即为重心。
递归的每一层都做了排序O(nlogn),递归深度O(logn),总体时间复杂度O(nlog2n)。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#define sa(a) scanf("%d", &a)
#define mem(a,b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 1e4 + 5, oo = 0x3f3f3f3f;
int cnt[maxn], vis[maxn];
int K, ans;
struct EDGE{int to; int length;int next;};
int head[maxn];
EDGE edge[maxn * 2];
typedef pair<int, int>pii;
int tot = 0;
void addedge(int u, int v, int l)
{
edge[tot].to = v;
edge[tot].length = l;
edge[tot].next = head[u];
head[u] = tot++;
edge[tot].to = u;
edge[tot].length = l;
edge[tot].next = head[v];
head[v] = tot++;
}
int countsubtree(int v, int p)
{
int ans = 1;
for(int i = head[v]; i != -1; i = edge[i].next){
int w = edge[i].to;
if(w == p||vis[w]) continue;
ans += countsubtree(w, v);
}
return cnt[v] = ans;
}
pii findc(int v, int p, int t)
{
pii res = pii(oo, 0);
int s = 1, m = 1;
for(int i = head[v]; i != -1; i = edge[i].next){
int w = edge[i].to;
if(w == p || vis[w]) continue;
res = min(res, findc(w, v, t));
m = max(m, cnt[w]);
s += cnt[w];
}
m = max(m, t - s);
res = min(res, pii(m, v));
return res;
}
void findpath(int v, int p, int d, vector<int>&ds)
{
ds.push_back(d);
for(int i = head[v]; i != -1; i = edge[i].next){
int w = edge[i].to;
if(w == p || vis[w]) continue;
findpath(w, v, d +edge[i].length, ds);
}
}
int count_pair(vector<int>&ds)
{
int res = 0;
sort(ds.begin(), ds.end());
int j = ds.size() - 1;
int i = 0;
while(i < j){
while(j > i &&ds[i] + ds[j] > K) j--;
res += j - i;
i++;
}
return res;
/*
int j = ds.size();
for(int i = 0; i < ds.size(); i++){
while(j > 0 && ds[i] + ds[j - 1] > K) j--;
res += j - (j > i?1:0);
}
return res / 2;*/
}
void solve(int v)
{
vector<int>ds;
countsubtree(v, -1);
int s = findc(v, -1, cnt[v]).second;
vis[s] =true;
//(1)
for(int i = head[s]; i != -1; i = edge[i].next){
int w = edge[i].to;
if(vis[w]) continue;
solve(w);
}
//(2)
ds.push_back(0);
for(int i = head[s]; i != -1; i = edge[i].next){
int w = edge[i].to;
if(vis[w]) continue;
vector<int>ts;
findpath(w, s, edge[i].length, ts);
ans -= count_pair(ts);
ds.insert(ds.end(), ts.begin(), ts.end());
}
vis[s] = false;
ans += count_pair(ds);
}
void init()
{
tot = 0;
ans = 0;
mem(head, -1);
mem(vis, 0);
mem(cnt, 0);
}
int main (void)
{
int n;
while(scanf("%d%d", &n, &K)== 2 && n + K){
int u, v, l;
init();
for(int i = 0; i < n - 1; i++){
sa(u),sa(v),sa(l);
addedge(u, v, l);
}
solve(1);
printf("%d\n", ans);
}
return 0;
}
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 ...
随机推荐
- 在Android上使用酷狗歌词API
参考自http://blog.csdn.net/u010752082/article/details/50810190 代码先贴出来: public void searchLyric(){ final ...
- 关于Android发送短信获取送达报告的问题
最近公司开发一个项目,要求app能够发送短信并获取送达报告.这本不是一个什么难题,实现这一功能的代码一搜一大把,那么这么简单的一个问题,为什么我要在这里提出来呢?那是因为我在写代码的时候掉入了一个坑, ...
- 不能说的hidden
不能说的hidden 时光跑跑...路在脚下...晨光依在...----Vashon 1.所谓"时尚",本质上就是自己看不见自己的样子.好比我们在地球上,却感觉不到地球在动. 2. ...
- HDU 5410 CRB and His Birthday (01背包,完全背包,混合)
题意:有n种商品,每种商品中有a个糖果,如果买这种商品就送多b个糖果,只有第一次买的时候才送.现在有m元,最多能买多少糖果? 思路:第一次买一种商品时有送糖果,对这一次进行一次01背包,也就是只能买一 ...
- web测试需要注意点
- ecpg - 嵌入的 SQL C 预处理器
SYNOPSIS ecpg [ option...] file... DESCRIPTION 描述 ecpg 是一个嵌入的用于C 语言的 SQL 预编译器. 它把嵌有 SQL 语句的 C 程序通过将 ...
- DROP AGGREGATE - 删除一个用户定义的聚集函数
SYNOPSIS DROP AGGREGATE name ( type ) [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP AGGREGATE 将删除一个现存聚集 ...
- uva1615 Highway
画图,每个给出点都有对应区间:先sort,再尽量靠右选:很常见的套路了..//注意不要越界(0,L) struct Q //复习结构{ double l,r; Q(double _l,double _ ...
- Linux-RedHat7.2 使用CentOS源
-- 查看yum rpm -qa |grep yum --卸载yum rpm -qa | grep yum | xargs rpm -e --nodeps --拷贝centos系统yum工具安装程序到 ...
- vue $parent 的上一级 有可能不是父组件,需要好几层$parent 如果这样 还不如用 this.$emit
vue $parent 的上一级 有可能不是父组件,需要好几层$parent 如果这样 还不如用 this.$emit