Day8 - F - Tree POJ - 1741
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 思路:点分治板子题,提供两个blog
https://blog.csdn.net/qq_39553725/article/details/77542223https://www.cnblogs.com/bztMinamoto/p/9489473.html
typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e4+; struct Node {
int v, next, val;
} Nodes[maxm*]; int head[maxm], cnt, siz[maxm], mxson[maxm], dis[maxm], root, mxsum, rootsum, points, n, k;
bool vis[maxm];
LL ans; void init() {
ans = ; cnt = ;
memset(vis, false, sizeof(vis)), memset(head, , sizeof(head));
} void addedge(int u, int v, int val) {
Nodes[++cnt].v = v;
Nodes[cnt].val = val;
Nodes[cnt].next = head[u];
head[u] = cnt;
} void getroot(int u, int fa) {
mxson[u] = , siz[u] = ;
for(int i = head[u]; i; i = Nodes[i].next) {
int v = Nodes[i].v;
if(v == fa || vis[v]) continue;
getroot(v, u);
siz[u] += siz[v];
mxson[u] = max(mxson[u], siz[v]);
}
mxson[u] = max(mxson[u], rootsum - siz[u]);
if(mxson[u] < mxsum) {
root = u, mxsum = mxson[u];
}
} void getdist(int u, int fa, int dist) {
dis[++points] = dist;
for(int i = head[u]; i; i = Nodes[i].next) {
int v = Nodes[i].v;
if(v == fa || vis[v]) continue;
getdist(v, u, dist+Nodes[i].val);
}
} int solve(int rt, int val) {
points = ;
getdist(rt, , val);
int l = , r = points, t = ;
sort(dis+, dis++points);
while(l <= r) {
if(dis[l] + dis[r] <= k) {
t += r-l;
l++;
} else
r--;
}
return t;
} void Divide(int rt) {
ans += solve(rt, );
vis[rt] = true;
for(int i = head[rt]; i; i = Nodes[i].next) {
int v = Nodes[i].v;
if(vis[v]) continue;
ans -= solve(v, Nodes[i].val);
rootsum = siz[v];
root = ; mxsum = 0x3f3f3f3f;
getroot(v, );
Divide(root);
}
} int main() {
ios::sync_with_stdio(false), cin.tie();
while(cin >> n >> k && n+k) {
init();
int u, v, val;
for(int i = ; i < n-; ++i) {
cin >> u >> v >> val;
addedge(u, v, val), addedge(v, u, val);
}
mxsum = 0x3f3f3f3f; rootsum = n;
getroot(,);
Divide(root);
cout << ans << "\n";
}
return ;
}
Day8 - F - Tree POJ - 1741的更多相关文章
- Tree POJ - 1741【树分治】【一句话说清思路】
因为该博客的两位作者瞎几把乱吹(" ̄︶ ̄)人( ̄︶ ̄")用彼此的智慧总结出了两条全新的定理(高度复杂度定理.特异根特异树定理),转载请务必说明出处.(逃 Pass:anuonei, ...
- 【POJ 1741】 Tree (树的点分治)
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量
POJ 1741. Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34141 Accepted: 11420 ...
- poj 1741 树的点分治(入门)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18205 Accepted: 5951 Description ...
- 点分治——POJ 1741
写的第一道点分治的题目,权当认识点分治了. 点分治,就是对每条过某个点的路径进行考虑,若路径不经过此点,则可以对其子树进行考虑. 具体可以看menci的blog:点分治 来看一道例题:POJ 1741 ...
- poj 1741 楼教主男人八题之中的一个:树分治
http://poj.org/problem? id=1741 Description Give a tree with n vertices,each edge has a length(posit ...
- [atcoder contest 010] F - Tree Game
[atcoder contest 010] F - Tree Game Time limit : 2sec / Memory limit : 256MB Score : 1600 points Pro ...
随机推荐
- 01初步启动Hadoop服务
1.rz命令将hadoop压缩包上传至Linux服务器中 2.tar -zxvf hadoop-2.7.7.tar.gz(解压即可用) 3.将解压出来的hadoop移到想要放的位置 mv hadoop ...
- Windows驱动开发-IRP超时处理
IRP被送到底层驱动程序以后,由于硬件设备的问题,IRP不能得到及时处理,甚至有可能永远不会被处理,这时候需要对IRP超时情况进行处理,一旦在规定时间内,IRP没有被处理,操作系统就会进入到IRP的处 ...
- Duilib热键
转载:https://www.zhaokeli.com/article/8288.html 在initwindow中注册热键 //生成热键标识,需要保存起来退出时销毁使用 int m_HotKeyId ...
- jdk动态代理和cglib动态代理底层实现原理超详细解析(jdk动态代理篇)
代理模式是一种很常见的模式,本文主要分析jdk动态代理的过程 1.举例 public class ProxyFactory implements InvocationHandler { private ...
- 单例设计模式和main方法
设计模式就是在大量的实践中总结和理论之后优选的代码结构.编程风格.以及解决问题的思考方式. 说白了设计模式就是在实际编程中逐渐总结出的解决问题的套路,类似于数学公式. 类的单例设计模式:在开发过程中有 ...
- Write-up-CH4INRULZ_v1.0.1
关于 下载地址:点我 哔哩哔哩:哔哩哔哩 信息收集 网卡:vboxnet0,192.168.56.1/24,Nmap扫存活主机发现IP为192.168.56.101 ➜ ~ nmap -sn 192. ...
- div背景图片自适应
对背景图片设置属性:background-size:cover;可以实现背景图片适应div的大小. background-size有3个属性: auto:当使用该属性的时候,背景图片将保持100% 的 ...
- Melodic 使用URDF创建简单的机器人模型
本人Linux版本:Ubuntu 18.04LTS ROS版本:Melodic URDF代码 <?xml version="1.0" ?> <robot name ...
- CF1209C Paint the Digits
CF1209C Paint the Digits 题意:给定T组数据,每组数据第一行输入数字串长度,第二行输入数字串,用数字1和2对数字串进行涂色,被1涂色的数字子串和被2涂色的数字子串拼接成新的数字 ...
- ionic3记录之APP运行时网络判断
判断设备网路是否正常: 安装插件: ionic cordova plugin add cordova-plugin-network-information npm install --save@nat ...