当前位置:网站首页>Gm3234 Yin Yang
Gm3234 Yin Yang
2022-07-18 12:46:00 【STJqwq】
GM3234 yin-yang
The question : Statistics do not repeat point pairs ( x , y ) (x,y) (x,y) The number of , Satisfy x x x To y y y There is at least one on the path z z z bring z → x z\to x z→x and z → y z\to y z→y The sum of the path weights of is 0 0 0 .
Ideas : Encounter such problems , We need to think about what is the number of root count pairs ; Then consider which algorithms can solve . Divide and conquer is a wonderful way to greatly simplify the problem on the tree .
details : Point divide and conquer should take the center of gravity of the tree every time , In this way, the optimal . Pay attention to clearing . Pay attention to repeated statistics , Don't let it slip , And don't repeat .
practice : The title says , Find one z z z Point make z → x z\to x z→x The sum of the weights of is 0 0 0 meanwhile z → y z\to y z→y The sum of the weights of is also 0 0 0 . therefore You may wish to p p p Root subtree Do some divide and conquer : about p p p The answer is Guarantee L C A = p LCA=p LCA=p In this nature , All legal points are right x , y x,y x,y The number of . set up d [ i ] d[i] d[i] by i i i The depth of the .
Observe , because x → y x\to y x→y The highest point of the path is p p p , therefore d [ z ] d[z] d[z] Meeting ≥ d [ p ] \ge d[p] ≥d[p] ( Think about it , Why can it be equal to ) And it's a certain point x x x perhaps y y y The ancestors of the . So consider maintaining each node i i i The precursor of a [ i ] a[i] a[i] , It's defined as Closest to it , And not root One Node number bring d [ a [ i ] ] = d [ i ] d[a[i]]=d[i] d[a[i]]=d[i] . It can be pre processed d d d When , Use the bucket to calculate . Curious students may ask , Why? Not for the root ? This is to facilitate the classification and discussion of different answers later , Please move the small bench and listen carefully .
that , Let's first discuss a special case , namely x x x perhaps y y y Is the answer of the root node .
- Statistics have precursors ( Having a precursor means a a a Not for 0 0 0 ) all x x x , send x x x Satisfy d [ a [ x ] ] d[a[x]] d[a[x]] by 0 0 0 . Then we can turn it into d [ x ] = 0 d[x]=0 d[x]=0 And there are precursors x x x Number .
Discuss more general situations : x x x and y y y be in Different sons of the root node Of subtree In .( Because it's definitely not possible in the subtree of the same son ) therefore , We need to calculate by son , Every son , The first Its contribution to the answer minus —— Prevent duplicate contributions , Then calculate the answer , Again Add back the contribution .
When x x x Have a precursor When , Statistics of all There is no precursor And qualified y y y .
When x x x Have a precursor When , Statistics of all Have a precursor And qualified y y y , But because Will be counted twice , So store separately .
When x x x There is no precursor When , At this time The precursor is the root node , therefore d [ x ] = 0 d[x]=0 d[x]=0 , Statistics of all There is no precursor And meet the conditions y y y , Because it will be counted twice , So store separately .
When x x x There is no precursor When , Statistics of all Have a precursor And qualified y y y , But because (2) Will be Repeated statistics , Therefore, it is not calculated .
The total answer can be calculated . Those answers stored separately need to be divided by 2 2 2 , Add up to the total answer .
So how can we continue to divide and conquer , Guarantee the time complexity ?
When p p p After calculating the answer of , You need to find the center of gravity of your son's neutron tree every time y y y 了 . Next level recursion , It's calculation y y y The answer , The results add up to the final answer . Why should we look for the center of gravity of the tree every time ?
prove : The center of gravity of the tree is defined : Focus p p p , With p p p Root , The maximum subtree size does not exceed 0.5 × s i z e 0.5\times size 0.5×size .
So the p p p After calculation , It will be divided into at least two subtrees .
We know n n n A complete binary tree of points has at most log n \log n logn layer , So divide the tree according to the center of gravity , The number of layers is also O ( log n ) O(\log n) O(logn) Class .
Calculation of each layer , need O ( n ) O(n) O(n) Time for , So the total time complexity O ( n log n ) O(n\log n) O(nlogn) .
therefore , This question is ok O ( n log n ) O(n\log n) O(nlogn) A perfect solution ( The constant is slightly larger ).
边栏推荐
- Uncover the origin of metamask: a tool set for getting started with Web3 culture
- Gson parsing and generating JSON data tool class
- TCP IP ICMP Ethernet frame format
- FreeRTOS个人笔记-列表与列表项
- How to make encrypted PDF files editable
- HDOJ-2057(A + B Again)
- Chapter 1: getting to know database and MySQL -- MySQL installation
- FreeRTOS personal notes - lists and list items
- MySQL one-stop learning, read and learn
- 021.多态详解 续2
猜你喜欢

AIRIOT答疑第4期|如何使用数据分析引擎?

Chinese character style transfer -- unsupervised font style conversion model based on generative confrontation network

Redis的安装(Windows)及常用的使用方法

9. Conception du module Px4: introduction au mode de vol Px4

Is free knowledge not worth paying attention to?

P1765 手机【入门】

揭秘MetaMask的起源:成为入门Web3文化的工具集

Software testing - learning notes 3

PX4模块设计之九:PX4飞行模式简介

Resume of sleeper - is there a boss who needs to recruit? Your success is just as bad as mine
随机推荐
Redis_ Linux Installation
0x22, 0x2e services of UDS
ThreadX kernel source code analysis (SMP) - thread execution core remap
P1567 统计天数【入门】
022.static与final关键字详解
Does Google | map neural network pre training help molecular characterization
Etherscan:熊市中的一些重要图表
判等问题:程序里如何确定你就是你?
Senior driver takes you around - Test Case
Redis 中两个字段排序
Open source data set - flower classification data set
[unity] animator animation replay, and startrecording animation recording
8 o'clock tonight! Lightdb PG distributed database technology innovation and practice "
从应用到底层:36张图带你进入Redis世界(上)
Personal safety protection order
[Vulnhub] Raven-1
容器健康检查解析
expdp导出
人身安全保护令
【后疫情时代的慢直播:镜头之下的美好时光】