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 ...
随机推荐
- VBA 从sql存储过程-记录集-导入
cnn.Open cnnstr cmd.ActiveConnection = cnn cmd.CommandTimeout = 120 cmd.CommandText = "dbo.t_bi ...
- COGS 495. 窗口
★☆ 输入文件:window.in 输出文件:window.out 简单对比时间限制:2 s 内存限制:256 MB [问题描述] 给你一个长度为N的数组,一个长为K的滑动的窗体从最左 ...
- 使用ABAP正则表达式解析HTML标签
需求就是我用ABAP的某个函数从数据库读取一个字符串出来,该字符串的内容是一个网页. 网页的form里包含了很多隐藏的input field.我的任务是解析出name为svyValueGuid的inp ...
- 原创 :单刷深渊 在Linux中系统安装mysql实战直播
[root@web108 tools]# ###开始装mysql 1添加用户 [root@web108 tools]# useradd -s /sbin/nologin -M mysql 2解压 [r ...
- PHP一句话后门过狗姿势万千之传输层加工
既然木马已就绪,那么想要利用木马,必然有一个数据传输的过程,数据提交是必须的,数据返回一般也会有的,除非执行特殊命令. 当我们用普通菜刀连接后门时,数据时如何提交的,狗狗又是如何识别的,下面结合一个实 ...
- 云原生技术图谱 (CNCF Landscape)
转自:https://raw.githubusercontent.com/cncf/landscape/master/landscape/CloudNativeLandscape_latest.jpg
- Android(java)学习笔记183:多媒体之图形颜色的变化
1.相信大家都用过美图秀秀中如下的功能,调整颜色: 2. 下面通过案例说明Android中如何调色: 颜色矩阵 ColorMatrix cm = new ColorMatrix(); paint.se ...
- Ubuntu 16.04 LTS: apt-get update 失败处理 Aborted (core dumped)
在Ubuntu 16.04运行sudo apt-get update出现如下错误: rogn@ubuntu:~$ sudo apt-get update Get:1 http://us.archive ...
- G7或变G6+1?特朗普七国峰会箱友军开炮
今日导读 G7 峰会刚召开完毕,德国总理默克尔发的一张照片就迅速火遍全球.照片中她身体前倾,像是在质问特朗普,而后者则双手交叉胸前,我自岿然不动,这张火药味十足的照片不禁让人脑补当时剑拔弩张的气氛.到 ...
- fossil 使用
~$ fossil updateCannot figure out who you are! Consider using the --usercommand line option, setting ...