【POJ】1987 Distance Statistics 点分治

【北大核心-代码复现】基于 VMD-FFCM-iTransformer 光伏功率短期预测 阅读详情

传送门:【POJ】1987 Distance Statistics


题目分析:点分治入门题~

poj 1741 买一送一可不能错过~


代码如下:


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

typedef long long LL ;

#define travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next )
#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define FOR( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )

const int MAXN = 40005 ;
const int MAXE = 80005 ;

struct Edge {
	int v , c ;
	Edge* next ;
} E[MAXE] , *H[MAXN] , *edge ;

bool vis[MAXN] ;
int siz[MAXN] ;
int dep[MAXN] ;
int num[MAXN] ;
int tot_size ;
int S[MAXN] ;
int n , K ;
int root ;
int top ;
int ans ;

void clear () {
	ans = 0 ;
	root = 0 ;
	edge = E ;
	num[0] = n ;
	tot_size = n ;
	clr ( H , 0 ) ;
	clr ( vis , 0 ) ;
}

void addedge ( int u , int v , int c ) {
	edge -> v = v ;
	edge -> c = c ;
	edge -> next = H[u] ;
	H[u] = edge ++ ;
}

void get_root ( int u , int fa = 0 ) {
	siz[u] = 1 ;
	num[u] = 0 ;
	travel ( e , H , u ) {
		int v = e -> v ;
		if ( !vis[v] && v != fa ) {
			get_root ( v , u ) ;
			siz[u] += siz[v] ;
			num[u] = max ( num[u] , siz[v] ) ;
		}
	}
	num[u] = max ( num[u] , tot_size - siz[u] ) ;
	if ( num[u] < num[root] ) root = u ;
}

void get_dep ( int u , int fa = 0 ) {
	if ( dep[u] <= K ) S[top ++] = dep[u] ;
	siz[u] = 1 ;
	travel ( e , H , u ) {
		int v = e -> v ;
		if ( !vis[v] && v != fa ) {
			dep[v] = dep[u] + e -> c ;
			get_dep ( v , u ) ;
			siz[u] += siz[v] ;
		}
	}
}

int get_num ( int u , int len ) {
	top = 0 ;
	dep[u] = len ;
	get_dep ( u ) ;
	sort ( S , S + top ) ;
	int l = 0 , r = top - 1 , res = 0 ;
	while ( l < r ) {
		if ( S[l] + S[r] <= K ) {
			res += r - l ;
			++ l ;
		} else -- r ;
	}
	return res ;
}

void dfs ( int u ) {
	vis[u] = 1 ;
	ans += get_num ( u , 0 ) ;
	travel ( e , H , u ) {
		int v = e -> v ;
		if ( !vis[v] ) {
			ans -= get_num ( v , e -> c ) ;
			root = 0 ;
			get_root ( v ) ;
			tot_size = siz[v] ;
			dfs ( root ) ;
		}
	}
}

void solve () {
	int u , v , c ;
	clear () ;
	rep ( i , 1 , n ) {
		scanf ( "%d%d%d%*s" , &u , &v , &c ) ;
		addedge ( u , v , c ) ;
		addedge ( v , u , c ) ;
	}
	scanf ( "%d" , &K ) ;
	get_root ( 1 ) ;
	dfs ( root ) ;
	printf ( "%d\n" , ans ) ;
}

int main () {
	while ( ~scanf ( "%d%*d" , &n ) ) solve () ;
	return 0 ;
}


实现基于 Python 和 xterm.js 的 Web 交互终端demo 前端 (HTML):使用 xterm.js 作为前端终端模拟器库。创建了一个 WebSocket 连接到 ws://localhost:8765,这与后端服务器的地址和端口匹配。监听 WebSocket 消息,并将数据写入终端。同时监听用户输入并发送到服务器。后端 (Python):使用 websockets 库来创建 WebSocket 服务器,监听端口 8765。使用 paramiko 库来创建 SSH 客户端并连接到指定的远程服务器。 阅读详情

相关推荐

HFSS - 双频微带天线设计与仿真

一、设计指标 中心频率:1.9GHz、2.45GHz 匹配情况:中心频率处S11S_{11}S11​参数小于-20dB 介质板厚度:1.6mm 介质板介电常数:3.8 馈电方式:50Ω\OmegaΩ同轴线馈电 二、双频微带天线 获得双频工作的一种最简单的方法是辐射贴片的长度对应-一个频率谐振,其宽度对应另一个频率谐振,然后从对角线的一角馈电,就能使同一个辐射贴片工作于两个频率上。其结构如下图所示。 从图中可以得知,x轴上的A点(x,0)为激发TM10_{10}10​模式的50Ω\OmegaΩ馈电点,由

MaxWei 1万+

POJ 1987 Distance Statistics (树上点分治)

题目地址:POJ 1987 点分治模板题,跟POJ 1741几乎一样,。。 代码如下:#include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <

scf0920 1320

C#安装OpenCv及打开笔记本默认摄像头方法

C#安装OpenCv及打开笔记本默认摄像头方法

cg1135217680的博客 2575

POJ1987 Distance Statistics【点分治】

题意:给一棵树求之间距离不超过k的点对数量。。 和poj1741一模一样。。。一样的做法。。每次找经过根的点对的数量。。统计数量时,排个序然后头尾开始找数量就好 #include #include #include using namespace std; const int MAXN=40010; const int INF=1<<30; struct EDGE { int v,next;

eeeaaaaa的专栏 477

【unity】解决unity编译器安装中文汉化包失败

查看是否有Localization目录如果没有就进行创建,然后点击打开Localization把下载的zh-hans.po文件复制剪切进去即可。如果有的同学中文包安装失败,我们找到相应的编译器版本,点击在资源管理器中显示按钮,我们找到简体中文,复制downloadUrl后面的值到浏览器打开进行下载,点击Edtior language选择框,选择简体中文,等待加载。我们点击当前目录的上一级,进入编译器目录。返回在文件管理器中点击Editor目录。编译器启动后点击Edit[编辑]按钮。复制或者剪切下载的文件。

weixin_44959182的博客 8037

EWF写保护功能详解:从安装到实战操作指南

本文详细介绍了EWF写保护功能的安装与实战操作指南。EWF(Enhanced Write Filter)通过重定向写入操作保护硬盘分区,特别适合需要长期稳定运行的系统场景。文章涵盖从安装准备、图形界面操作到命令行高级管理的全流程,并提供了常见问题的解决方案,帮助用户轻松掌握这一实用工具。

weixin_28710999的博客 450

POJ 1987 Distance Statistics 树上点分治

算是入门题了,详见《分治算法在树的路径问题中的应用》(by 漆子超)。 #include #include #include #include #include #include #include #include #include #define LL long long #define INF 0x3f3f3f3f #define Mod 1000000007 #def

自在飞花 793

全网最新首发纯干货:微信数据库表结构完整分析(一)

找不到微信DB表结构说明?找不到具体字段的解释?来这里,全网最完整版!!!

killghosts的博客 4943

POJ 1987 Distance Statistics(树的点分治)

转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove   上场CF的C题是一个树的分治。。。 今天刚好又看到一题,就做了下 题意:一棵树,问两个点的距离 http://poj.org/problem?id=1987   貌似是经典的点分治题。。。。。 看成有根

窝不是爱酱,喵~~~~ 4142

把 ST03 的历史数据留得更久:ABAP Workload 统计保留期设置、原理与落地避坑指南

SAP ST03性能数据保留期配置指南 ST03/ST03N是SAP系统性能分析的核心工具,但默认数据保留期通常较短(3个月左右),难以满足跨季度对比、年度规划等需求。本文详细解析了ST03数据保留期的配置方法及注意事项: 数据存储机制:性能数据存储在监控库中,通过定期重组作业清理过期数据 配置路径:7.0+系统在"Workload Collector Database→Reorganization→Control"中按Profile分别设置 关键限制:保留期最大值建议不超过99,否则可

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 224

选择的 Python 解释器无效,请尝试更改解释器以启用 IntelliSense、Lint 分析和调试等功能。请查看输出,详细了解解释器无效的原因。

摘要:在VS Code中配置Anaconda Python解释器时若出现"解释器无效"错误,可通过修改用户设置解决。具体步骤:1) 按Ctrl+Shift+P打开命令面板;2) 搜索并打开用户设置JSON文件;3) 添加conda路径配置项,示例格式为"python.condaPath": "D:\\Anaconda\\setup\\Scripts\\conda.exe";4) 同时启用环境激活选项"python.terminal.act

猿小白的博客 849

poj 1987 Distance Statistics 点分治

题目大意: 给你一棵树,问两个点对距离 题目分析:点分治裸题。dfs处理出每个重心(分治下)到当前子树根节点的距离存在dis数组中,o(n)直接找出。 但要注意的是,对于dis所存的距离,只适用于两个不同的子树当中,所以要将位于同一个子树中的方案数减去。 #include #include #include #include #include #include #incl

WKLL_ 841

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

POJ 1987 Distance Statistics【树上点分治】

同 POJ1741 Distance Statistics Time Limit: 2000MS Memory Limit: 64000K Total Submissions: 2678 Accepted: 964 Case Time Limit: 1000MS Description Frustrated at the number of distance quer...

Irish_Moonshine的博客 305

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

[POJ1987]Distance Statistics(点分治)

【题目链接】http://poj.org/problem?id=1987 【题目大意】给定一棵树,每条边有权值,求距离<=k的点对数 【解题思路】树上点分治基础题,更新答案时用容斥原理把子树信息一起处理#include<cstdio> #include<algorithm> #include<cmath> #include<cstdlib> #include<iostream> const in

S_G_G的博客 526

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

POJ 1987 Distance Statistics【Tree,点分治】

这道题目和POJ 1741基本一样了。 树上的分治。 此类算法参考论文:09年国家集训队论文《分治算法在树的路径问题中的应用——漆子超》 #include #include #include #include using namespace std; #define N 40005 struct node { int v, l; node() {};

智障 2047

[树的点分治] [POJ1741/POJ1987] Tree/Distance Statistics

传送门:POJ1741、POJ1987 题目大意是询问树上两点间距离小于等于kk的点对个数。 很容易想到O(n2log2n)O(n^2\log_2n)暴力,即枚举所有路径,然后LCA统计长度。过不去! 考虑一种对于树的分治,找到树的重心,可以将树分成一些子树,那么需要统计的路径就有三种情况(其实是两种): 1、完全在一个子树中; 2、两端在不同子树中,易知这条路径一定过重心; 3、(2的

HeRaNO@HSAHRBNU 683

【POJ 1987】树的点分治 Distance Statistics

Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decides to ask queries from which he can learn more information. Specifically, he supplies an

Starlight的博客 512

POJ 题目1987 Distance Statistics(树的点分治)

Distance Statistics Time Limit: 2000MS   Memory Limit: 64000K Total Submissions: 1973   Accepted: 658 Case Time Limit: 1000MS Description Frustrated at the number of d

Jogging_Clown 463

POJ - 1987 Distance Statistics (点分治 距离小于等于k的点对)

Distance Statistics Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decides to ask queries from which he can learn more information. Speci...

YOONGI 455

POJ 1741 Tree + POJ 1987 Distance Statistics【树的点分治】

Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13558 Accepted: 4367 DescriptionGive a tree with n vertices,each edge has a length(positive integer less than 1001). Def

我是一颗小星星~ 882

POJ 1987 BZOJ 3365 Distance Statistics 树的分治(点分治)

题目大意:(同poj1741,刷一赠一系列) CODE: #include #include #include #include #define MAX 20010 #define INF 0x3f3f3f3f using namespace std; int points,edges,k; int head[MAX],total; int next[MAX << 1

(╯°口°)╯(┴—┴ 1176

POJ 1987 BZOJ 3365 USACO 2004 Feb Distance Statistics 路程统计 点分治

和POJ 1714一样。。 http://blog.csdn.net/huanghongxun/article/details/50967578#include <cstdio> #include <algorithm> #include <cstring> const int N = 40005, M = N * 2; #define adj(i,j) for(int i=h[j];i;i=p[

とある黄鱼のOIブログ 663

【点分治】poj1741 Tree / poj2114 Boatherds / poj1987 Distance Statistics

三道题都很类似。给出1741的代码 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define MAXN 10001 typedef pair<int,int> Point; int n,K,ans; int v[MAXN&...

weixin_34102807的博客 132

poj1987 Distance Statistics 树的分治

Description Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decides to ask queries from which he can learn more information. Specifically,

thunderMrbird的博客 845
上一篇: 【POJ】1741 Tree 点分治
下一篇: 【POJ】2114 Boatherds 点分治
poursoul
博客等级 码龄13年 470粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值