POJ 1741 Tree (树的点分治入门)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 16172 | Accepted: 5272 |
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

view code#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 10010;
int n, k, pre[N], ans, mi, rt, siz[N], num;
bool vis[N]; struct edge
{
int u, v, w, next;
edge() {}
edge(int u, int v, int w, int next):u(u),v(v),w(w),next(next) {}
}e[N<<1];
int ecnt; void init()
{
ans = ecnt = 0;
memset(pre, -1, sizeof(pre));
memset(vis, 0, sizeof(vis));
} inline void add(int u, int v, int w)
{
e[ecnt] = edge(u, v, w, pre[u]);
pre[u] = ecnt++;
} void getroot(int u, int fa)
{
siz[u] = 1;
int mx = 0;
for(int i=pre[u]; ~i; i=e[i].next)
{
int v = e[i].v;
if(v==fa || vis[v]) continue;
getroot(v, u);
siz[u] += siz[v];
mx = max(mx, siz[v]);
}
mx = max(mx, siz[0]-siz[u]);
if(mx <mi) mi = mx, rt = u;
} int dis[N];
void getdis(int u, int d, int fa)
{
dis[num++] = d;
for(int i=pre[u]; ~i; i=e[i].next)
{
int v = e[i].v;
if(v==fa || vis[v]) continue;
getdis(v, d+e[i].w, u);
}
} int calc(int u, int d)
{
int res = 0;
num = 0;
getdis(u, d, 0);
sort(dis, dis+num);
int i = 0, j = num-1;
while(i<j)// 经典。。
{
while(dis[i]+dis[j]>k && i<j) j--;
res += j-i;
i++;
}
return res;
} void solve(int u, int cnt)
{
mi = n;
siz[0] = cnt;
getroot(u, 0);
ans += calc(rt, 0);
vis[rt] = 1;
for(int i=pre[rt]; ~i; i=e[i].next)
{
int v = e[i].v;
if(vis[v]) continue;
ans -= calc(v, e[i].w);
solve(v, siz[v]);
} } int main()
{
// freopen("in.txt", "r", stdin);
while(scanf("%d%d", &n, &k)>0 && (n|k))
{
int u, v, w;
init();
for(int i=1; i<n; i++)
{
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
solve(1, n);
printf("%d\n", ans);
}
return 0;
}
POJ 1741 Tree (树的点分治入门)的更多相关文章
- 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 树的分治(点分治)
题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...
- poj 1741(树的点分治)
Tree Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...
- POJ 1741/1987 树的点分治
树的点分治,主要思想是每次找子树的重心,计算经过根节点的情况数,再减去点对属于同一子树的情况. #include <iostream> #include <vector> #i ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- POJ 1741 Tree 树的分治
原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...
- poj 1741 Tree (树的分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30928 Accepted: 10351 Descriptio ...
- POJ 1741 Tree 树形DP(分治)
链接:id=1741">http://poj.org/problem?id=1741 题意:给出一棵树,节点数为N(N<=10000),给出N-1条边的两点和权值,给出数值k,问 ...
- poj 1741 树的点分治(入门)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18205 Accepted: 5951 Description ...
随机推荐
- 七个结构模式之享元模式(Flyweight Pattern)
定义: 运用共享技术对大量细粒度对象的复用,这要求这些对象都很相似,状态变化很小.将这些对象的内部状态和外部状态进行区分,对于内部状态相同的只存储一个对象,而对不同的外部状态则采用不同的操作. 结构图 ...
- Guacamole之配置Guacamole(五)
摘要 在网上看到一篇Guacamole官方手册的翻译,但是找不到后续,于是想自己也翻译几篇,有时间的话,会尽量多翻译一些. 原文地址:http://guacamole.incubator.apache ...
- Guacamole之本地安装Guacamole(二)
摘要 在网上看到一篇Guacamole官方手册的翻译,但是找不到后续,于是想自己也翻译几篇,有时间的话,会尽量多翻译一些. 原文地址:http://guacamole.incubator.apache ...
- IOS 非常流畅的滑动tableView
为什么要写这篇文章呢?之前写过一篇,因为手机打字不是很方便,还有之前同事用6splus 定下午茶时候,我滑动列表时候竟然误以为是安卓系统的手机. tableview 流畅度可以用fps来测试,到6 ...
- Ajax关于readyState(状态值)和status(状态码)的研究
var getXmlHttpRequest = function () { try{ //主流浏览器提供了XMLHttpRequest对象 return new XMLHttpRequest(); } ...
- reactjs学习一(环境搭配react+es6+webpack热部署)
reactjs学习一(环境搭配react+es6+webpack热部署) 本文的源码在这里下载 https://github.com/tianxiangbing/webpack-study 或者使 ...
- SharePoint 判断用户是否在字段"人员和组"里面
两个自己平时写的方法,记录下来,方便以后查找使用: 1.判断用户是否在字段人员和组里面: public static bool IsUserInFiled(int UserID, string Lis ...
- Javascript - Arraylike的7种实现
jQuery的崛起让ArrayLike(类数组)在javascript中大放异彩,它的出现为一组数据的行为(函数)扩展提供了基础. 类数组和数组相似,具有数组的某些行为,但是它相比数组可以更加自由的扩 ...
- Autodesk 产品开发培训开始报名-8月26~28-武汉– Revit, Vault, Autodesk Viewer, Navisworks
为了帮助Autodesk中国地区的二次开发人员有机会系统地了解与学习Autodesk 在BIM解决方案中的旗舰产品 Revit以及Navisworks等产品的最新开发技术,并有机会与Autodesk ...
- CSS 遗漏点
link属性:链接的颜色(如果改成白色什么都看不见) alink属性:active点击之后的链接的颜色 vlink属性:visited点击之后的颜色 bgcolor属性:背景颜色 word-break ...