POJ1741 Tree (点分治)
Tree
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 25772 | Accepted: 8566 |
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
code
#include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; const int N = ; struct Edge{
int to,nxt,w;
}e[N<<];
int head[N],son[N],f[N],deth[N],d[N];
bool vis[N];
int ans,tot,Root,sum,n,k; void init() {
memset(head,,sizeof(head));
memset(vis,false,sizeof(vis));
ans = tot = Root = ;
}
void add_edge(int u,int v,int w) {
e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
}
void getroot(int u,int pa) {
son[u] = ;f[u] = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v==pa||vis[v]) continue;
getroot(v,u);
son[u] += son[v];
f[u] = max(f[u],son[v]);
}
f[u] = max(f[u],sum-son[u]);
if (f[u] < f[Root]) Root = u;
}
void getdeth(int u,int pa) {
deth[++deth[]] = d[u];
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;
if (v==pa||vis[v]) continue;
d[v] = d[u] + w;
getdeth(v,u);
}
}
int calcc(int u,int w) {
deth[] = ;
d[u] = w;
getdeth(u,);
sort(deth+,deth+deth[]+);
int l = ,r = deth[],ret = ;
while (l < r) {
if (deth[l] + deth[r] <= k) ret += r-l,l++;
else r--;
}
return ret;
}
void work(int u) {
ans += calcc(u,);
vis[u] = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;
if (vis[v]) continue;
ans -= calcc(v,w);
sum = son[v];
Root = ;
getroot(v,);
work(Root);
}
}
int main() {
while (scanf("%d%d",&n,&k)!=EOF && !(n==&&k==)) {
init();
for (int a,b,c,i=; i<n; ++i) {
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);add_edge(b,a,c);
}
f[] = 1e9;
sum = n;
getroot(,);
work(Root);
printf("%d\n",ans);
}
return ;
}
POJ1741 Tree (点分治)的更多相关文章
- [POJ1741]Tree(点分治)
树分治之点分治入门 所谓点分治,就是对于树针对点的分治处理 首先找出重心以保证时间复杂度 然后递归处理所有子树 对于这道题,对于点对(u,v)满足dis(u,v)<=k,分2种情况 路径过当前根 ...
- [poj1741]Tree(点分治+容斥原理)
题意:求树中点对距离<=k的无序点对个数. 解题关键:树上点分治,这个分治并没有传统分治的合并过程,只是分成各个小问题,并将各个小问题的答案相加即可,也就是每层的复杂度并不在合并的过程,是在每层 ...
- [bzoj1468][poj1741]Tree[点分治]
可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树 ...
- POJ1741 Tree 树分治模板
http://poj.org/problem?id=1741 题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数. dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- POJ1741 Tree(树分治——点分治)题解
题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...
- [poj1741][tree] (树/点分治)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ1741 tree 【点分治】
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25286 Accepted: 8421 Description ...
- POJ1741 Tree(树的点分治基础题)
Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v) ...
- POJ1741——Tree(树的点分治)
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...
随机推荐
- 数据库查询SQL语句的时候如何写会效率更高?
引言 以前刚开始做项目的时候,开发经验尚浅,遇到问题需求只要把结果查询出来就行,至于查询的效率可能就没有太多考虑,数据少的时候还好,数据一多,效率问题就显现出来了.每次遇到查询比较慢时,项目经理就会问 ...
- 分享到qq空间等代码
<!-- Baidu Button BEGIN --><div id="bdshare" class="bdshare_t bds_tools get- ...
- vim常用命令大全
在命令状态下对当前行用== (连按=两次), 或对多行用n==(n是自然数)表示自动缩进从当前行起的下面n行.你可以试试把代码缩进任意打乱再用n==排版,相当于一般IDE里的code format.使 ...
- mysql配置jdbc
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/database?useUnicode=t ...
- 零基础逆向工程25_C++_02_类的成员权限_虚函数_模板
1 类的成员权限 1.1 小结: 1.对外提供的函数或者变量,发布成public的 但不能随意改动. 2.可能会变动的函数或者变量,定义成private的 这样编译器会在使用的时候做检测. 3.只有结 ...
- Android 环信 调用相机崩掉 mikdir()
protected void selectPicFromCamera() { if (!EaseCommonUtils.isSdcardExist()) { Toast.makeText(getAct ...
- 定时器new Timer().schedule()的使用
Timer是一种工具,线程用其安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行.实际上是个线程,定时调度所拥有的TimerTasks. TimerTask是一个抽象类,它的子类由 ...
- UVA 340 Master-Mind Hints 猜密码游戏(水)
题意: 给一串密码(第一行),接着再给你很多行猜测,针对每行猜测,输出两个数字,分表代表:同一列上匹配的个数,不同列上匹配的个数.注:匹配指的是一次,一旦配对,不能再与其他配对. 思路: 每接受一行猜 ...
- Shuffle Cards
C: Shuffle Cards 时间限制: 1 Sec 内存限制: 128 MB提交: 3 解决: 3[提交] [状态] [讨论版] [命题人:admin] 题目描述 Eddy likes to ...
- 51nod——1640 天气晴朗的魔法 有边权限制的最大生成树
好好读题嗷:“所以我们要求阵中的魔法链的魔力值最大值尽可能的小,与此同时,魔力值之和要尽可能的大.” 第一条件是生成树的最大边权更小,第二条件是在最大边权的限制下搞一个最大生成树. 至于最大生成树,如 ...