「POJ 1741」Tree
题面:
Tree
Give a tree with n vertices,each edge has a length(positive integer less than 1001).
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 input contains several test cases. The first line of each test case contains two integers n, k. (n<=10000) The following n-1 lines each contains three integers u,v,l, which means there is an edge between node u and v of length l.
The last test case is followed by two zeros.
Output
For each test case output the answer on a single line.
Sample Input
5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0
Sample Output
8
题意:
给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K
输入格式: 每次输入n,k(如果n0&&k0停止),接着n-1行,每行3个数描述边
这道题就是一道淀粉质点分治的模板题,具体不多讲,如果不会看看这个吧
接下来直接上代码
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=200001;
int read() {
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')c=='-'?f=-1,c=getchar():c=getchar();
while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x*f;
}
int n,k;
int dep[N];/*从当前节点i到枚举当前树的根节点父亲的距离*/
int f[N];/*当i为根节点时最大字数大小*/
int vis[N];/*i节点是否被当根使用过*/
int siz[N];/*以i节点为根时,其子树(包括本身)的节点个数*/
int root;/*根节点*/
struct node {
int next,to,v;
} a[N<<1];
int head[N],cnt,sum/*这棵当前递归的这棵树的大小*/;
void add(int x,int y,int c) {
a[++cnt].to=y;
a[cnt].next=head[x];
a[cnt].v=c;
head[x]=cnt;
}
void findroot(int k,int fa) {
f[k]=0,siz[k]=1;
for(int i=head[k]; i; i=a[i].next) {
int v=a[i].to;
if(vis[v]||v==fa)
continue;
findroot(v,k);
siz[k]+=siz[v];
f[k]=max(f[k],siz[v]);
}
f[k]=max(f[k],sum-siz[k]);
if(f[k]<f[root])
root=k;
}
int tot;
void finddep(int k,int fa,int l) {
dep[++tot]=l;
for(int i=head[k]; i; i=a[i].next) {
int v=a[i].to;
if(v==fa||vis[v])
continue;
finddep(v,k,l+a[i].v);
}
}
int K;
int calc(int k,int L) {
tot=0;
finddep(k,0,L);
sort(dep+1,dep+1+tot);
int l=1,r=tot,ans=0;
while(l<r){
if(dep[l]+dep[r]<=K)
l++,ans+=r-l+1;
else
r--;
}
return ans;
}
int js;
void devide(int k) {
vis[k]=1;
js+=calc(k,0);
for(int i=head[k]; i; i=a[i].next) {
int v=a[i].to;
if(vis[v])
continue;
js-=calc(v,a[i].v);
root=0,sum=siz[v];
findroot(v,0);
devide(root);
}
}
int main() {
int n=read(),x,y,z;
K=read();
while(n&&K){
memset(head,0,sizeof(head)),cnt=0;//head
memset(vis,0,sizeof(vis));//标记数组
for (int i=1; i<n; i++)
x=read(),y=read(),z=read(),add(x,y,z),add(y,x,z);
sum=f[0]=n,js=0;//总数
findroot(1,0);
devide(root);
printf("%d\n",js);
n=read(),K=read();
}
return 0;
}
注意初始化!!!注意初始化!!!注意初始化!!!
重要的事情说三遍
「POJ 1741」Tree的更多相关文章
- 「POJ 3666」Making the Grade 题解(两种做法)
0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...
- 【POJ 1741】 Tree (树的点分治)
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...
- 【POJ 1741】Tree
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11570 Accepted: 3626 Description ...
- 【POJ 1741】 Tree
[题目链接] http://poj.org/problem?id=1741 [算法] 点分治 要求距离不超过k的点对个数,不妨将路径分成两类 : 1. 经过根节点 2. 不经过根节点 考虑第1类路径, ...
- 「POJ Challenge」生日礼物
Tag 堆,贪心,链表 Solution 把连续的符号相同的数缩成一个数,去掉两端的非正数,得到一个正负交替的序列,把该序列中所有数的绝对值扔进堆中,用所有正数的和减去一个最小值,这个最小值的求法与「 ...
- 「POJ 3268」Silver Cow Party
更好的阅读体验 Portal Portal1: POJ Portal2: Luogu Description One cow from each of N farms \((1 \le N \le 1 ...
- Solution -「HDU 5498」Tree
\(\mathcal{Description}\) link. 给定一个 \(n\) 个结点 \(m\) 条边的无向图,\(q\) 次操作每次随机选出一条边.问 \(q\) 条边去重后构成生成 ...
- 「POJ 1135」Domino Effect(dfs)
BUPT 2017 Summer Training (for 16) #3G 题意 摆好的多米诺牌中有n个关键牌,两个关键牌之间有边代表它们之间有一排多米诺牌.从1号关键牌开始推倒,问最后倒下的牌在哪 ...
- 「POJ - 1003」Hangover
BUPT 2017 summer training (16) #2C 题意 n个卡片可以支撑住的长度是1/2+1/3+1/4+..+1/(n+1)个卡片长度.现在给出需要达到总长度,求最小的n. 题解 ...
随机推荐
- Java-Maven-Runoob:Maven教程
ylbtech-Java-Maven-Runoob:Maven教程 1.返回顶部 1. Maven 教程 Maven 翻译为"专家"."内行",是 Apache ...
- 字符串转换为字典的函数eval(字符串)
首先把多行的字符串,变成一个字符串,用'''和'''扩起来: 然后把这个字符串,赋值给b 这个时候,b根本调不出来,也用不起来: 用eval(b),来格式化字符串变成字典: 然后b就变成了一个字典:
- mybatis generator 用法
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- Ceph recover的速度控制
前言 磁盘损坏对于一个大集群来说,可以说是必然发生的事情,即使再小的概率,磁盘量上去,总会坏那么几块盘,这个时候就会触发内部的修复过程,修复就是让不满足副本要求的PG,恢复到满足的情况 一般是踢掉坏盘 ...
- haproxy启动时提示失败
haproxy启动时提示失败:[ALERT] 164/110030 (11606) : Starting proxy linuxyw.com: cannot bind socket 这个问题,其实就是 ...
- Deep Learning 学习笔记(2):多参数的线性回归
上次用简单地介绍了线性回归的模型和梯度下降获得参数方程的方法. 用到的一个十分简单的参数方程h(x)=theta0+theta1*x 在现实问题中,参数方程能要复杂许多, 不只有一个未知量x,可能有多 ...
- 监控Mongo慢查询
监控Mongo慢查询 1. 使用mongostat监控MongoDB全局情况 mongostat是mongdb自带的状态检测工具,在命令行下使用.它会间隔固定时间获取MongoDB的当前运行状态,并输 ...
- Django中favicon.ico文件的配置
默认情况下,浏览器访问一个网站的时候,同时还会向服务器请求“/favicon.ico”这个URL,目的是获取网站的图标. 若没有配置的话,Django就会返回一个404错误,并且浏览器接收到这个404 ...
- json转字符串 —— jsonObj.toJSONString()与JSON.stringify(jsonObj)
ar people = { "programmers": [{ "firstName": "Brett", "lastName&q ...
- 《转》我的ARM学习经历
1.基础阶段 话说06年第一份工作从事的是PLC开发,用protel画原理图和PCB,写AVR单片机程序,焊焊板子,还去过华强北买器件,比较杂,但是接触面比较广,为进一步学ARM打下了基础. ...