树--个人笔记和经典相关题目解析
n个点 n-1条边 +联通 等于树
树基础
1.树的深度,度,高度和某个结点的不同,且带树的都是指最大值
2.度指的是结点的子树棵树个数,树的度是整个树的最大结点,二叉树是指最多一个结点最多两个子结点的树
3.满足联通,且边数等于顶点数-1的结构 就是树
4.若以重心为根结点,则所有其每个子树的大小都不超过整棵树的一半。
重心相关题目
[https://ac.nowcoder.com/discuss/833004?type=101&order=0&pos=1&page=0&channel=-1&source_id=1]()
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+100;
int a[N];
signed main(){
int t;
cin>>t;
while(t--){
int n,sum=0,max=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];sum+=a[i];max=(max,a[i]);
}
if(max<=sum/2) cout<<sum;
else {//只跟最大的有关
cout<<max-( max-1-sum/2)*2;
}
cout <<endl;
}
}
完全二叉树https://www.acwing.com/problem/content/1242/
求每个深度(每层)最大的权值 输出的
LL maxs = -1e18;//求最大 设置初始值
int depth = 0;
for (int d = 1, i = 1; i <= n; i *= 2, d ++ )//第一层循环 循环第几层 i是起点 停止条件是i所在那一层
{
LL s = 0;
for (int j = i; j < i + (1 << d - 1) && j <= n; j ++ )//第二层循环 每个结点
s += a[j];
if (s > maxs)
{
maxs = s;
depth = d;
}
}
printf("%d\n", depth);