POJ 1741.Tree 树分治 树形dp 树上点对
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 24258 | Accepted: 8062 |
Description
Define dist(u,v)=The min distance between node u and v.
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k.
Write a program that will count how many pairs which are valid for a given tree.
Input
The last test case is followed by two zeros.
Output
Sample Input
5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0
Sample Output
8
Source
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<bitset>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define bug(x) cout<<"bug"<<x<<endl;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int> P;
const int N=1e5+,M=1e5+;
const int inf=0x3f3f3f3f;
const ll INF=1e18+,mod=1e9+;
struct edge
{
int from,to;
int w;
int next;
};
edge es[M];
int cut,head[N];
int si[N],maxx[N];
bool vis[N];
int deep[N];
int k;
int root,ans;
void init()
{
cut=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int w)
{
cut++;
es[cut].from=u,es[cut].to=v;
es[cut].w=w;
es[cut].next=head[u];
head[u]=cut;
}
int getroot(int u,int fa,int n)
{
si[u]=,maxx[u]=;
for(int i=head[u]; i!=-; i=es[i].next)
{
int v=es[i].to;
if(v==fa||vis[v]) continue;
si[u]+=getroot(v,u,n);
maxx[u]=max(maxx[u],si[v]);
}
maxx[u]=max(maxx[u],n-si[u]);
if(maxx[u]<maxx[root]) root=u;
return si[u];
}
void getdeep(int u,int fa,int d)
{
deep[++deep[]]=d;
for(int i=head[u]; i!=-; i=es[i].next)
{
edge e=es[i];
if(e.to==fa||vis[e.to]) continue;
getdeep(e.to,u,d+e.w);
}
}
int cal(int u,int fa,int d)
{
deep[]=;
getdeep(u,fa,d);
sort(deep+,deep+deep[]+);
int l=,r=deep[];
int res=;
while(l<r)
{
if(deep[l]+deep[r]<=k) res+=r-l,l++;
else r--;
}
return res;
}
void solve(int u)
{
vis[u]=true;
ans+=cal(u,,);///统计符合情况的点对数
for(int i=head[u]; i!=-; i=es[i].next)
{
edge e=es[i];
if(vis[e.to]) continue;
ans-=cal(e.to,,e.w);///删除同一子树的点对数
root=;
getroot(e.to,,si[e.to]);
solve(root);///递归同一子树
}
}
int main()
{
int n;
while(scanf("%d%d",&n,&k)!=EOF)
{
if(n==&&k==) break;
init();
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);
}
memset(vis,false,sizeof(vis));
root=,maxx[]=inf;
ans=;
getroot(,,n);
solve(root);
printf("%d\n",ans);
}
return ;
}
树分治
POJ 1741.Tree 树分治 树形dp 树上点对的更多相关文章
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- 树的点分治 (poj 1741, 1655(树形dp))
poj 1655:http://poj.org/problem?id=1655 题意: 给无根树, 找出以一节点为根, 使节点最多的树,节点最少. 题解:一道树形dp,先dfs 标记 所有节点的子 ...
- POJ 1741 Tree(树的点分治,入门题)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21357 Accepted: 7006 Description ...
- 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 ...
- [codeforces161D]Distance in Tree(点分治/树形dp)
题意:求树上距离为k的点对个数: 解题关键:练习一下点分治不用容斥 而直接做的做法.注意先查询,后更新. 不过这个方法有个缺陷,每次以一个新节点为根,必须memset mp数组,或许使用map会好些, ...
- POJ 1741 Tree ——点分治
[题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...
- 『You Are Given a Tree 整体分治 树形dp』
You Are Given a Tree Description A tree is an undirected graph with exactly one simple path between ...
- Tree POJ - 1741【树分治】【一句话说清思路】
因为该博客的两位作者瞎几把乱吹(" ̄︶ ̄)人( ̄︶ ̄")用彼此的智慧总结出了两条全新的定理(高度复杂度定理.特异根特异树定理),转载请务必说明出处.(逃 Pass:anuonei, ...
随机推荐
- Wireshark win7 没有找到接口;找不到接口
下载安装winpcap: https://www.winpcap.org/install/default.htm
- LeetCode - 刷题经验
1.加快代码速度 刷了前面几道题发现速度总是处于尾部10%,刚开始非常不服,后来仔细一看那些排名靠前的提交,发现了猫腻.几乎每一个提交都有这样的一段代码: static const auto io_s ...
- 位置式PID与增量式PID
//位置式PID float Kp; float Ki; float Kd; float eSum,e0,e1; float pid_control(float now,float target) { ...
- 使用IDE之webstorm
最近打算试试用webstorm,今天从vscode换成了webstorm. 官方下载webstorm 1.下载之后安装,我全部选择默认,因为webstorm是付费ide,到启动面板时,选择激活选项. ...
- Entity Framework Code first 可能会导致循环或多个级联路径.
用code first映射数据库报错 Introducing FOREIGN KEY constraint 'FK_dbo.Roles_dbo.SubSystems_SubSystemID' on t ...
- Python【每日一问】06
问:简述Python文件打开模式 r. w. a. r+.w+.a+之间的区别 答: 1.只读模式 r 文件存在:只读打开,只能执行读操作 文件不存在:报错 # ######## 只读模式r #### ...
- Linux find 命令参数大全及示例
Linux中find常见用法示例 命令格式:find path -option [-print] [ -exec -ok command] {} \; 参数说明: path:find命令所查找的目录路 ...
- thinkphp5 Exception类重定义
重点定义自己的错误信息和错误码: 在TP5的配置文件中有下面一段 // 异常处理handle类 留空使用 \think\exception\Handle 'exception_handle' => ...
- asp.net 微信JsSDK
有时间再整理吧 using System; using System.Collections.Generic; using System.Linq; using System.Web; using S ...
- leetcode15
class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); Li ...