当前位置:网站首页>(dfs+ tree DP) acwing 846 Center of gravity of tree
(dfs+ tree DP) acwing 846 Center of gravity of tree
2022-06-13 09:24:00 【Age worry】
846. The center of gravity of the tree
Topic link https://www.acwing.com/problem/content/848/
subject :

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int N=1e5+10;
int n;
int res=N;
bool vis[N];
vector<int > q[N];
int dfs(int u){
vis[u]=1;
int rs=0;
int sum=0;
for(int i=0;i<q[u].size();i++){
if(!vis[q[u][i]]){
int t=dfs(q[u][i]);
sum+=t;
rs=max(rs,t);
}
}
rs=max(rs,n-sum-1);
res=min(rs,res);
return sum+1;
}
int main(){
cin>>n;
int a,b;
for(int i=1;i<n;i++){
scanf("%d%d",&a,&b);
q[a].push_back(b);
q[b].push_back(a);
}
dfs(1);
cout<<res;
return 0;
}
边栏推荐
- LeetCode 343. integer partition
- Batch read all voice files under the folder
- Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing
- C language time difference calculation
- How Simulink adds modules to the library browser
- JUC atomic integer
- C language: preprocessing in program environment
- 20220606 Young's inequality for Matrices
- Musk's "meta universe" dream
- Zigzag transformation
猜你喜欢
随机推荐
C language: deep understanding of character functions and string functions (2)
Heap
LeetCode 6096. 咒语和药水的成功对数(二分查找)
turtle库的使用数字时钟模拟时钟动态显示
Opencv face recognition of ros2 foxy~galactic~humble
【最全面详细解释】背包问题详解
Resolve importerror:lib*** so--cannot open shared object file: No such... (pycharm/clion reports an error but the shell does not)
Final principle
Leetcode points to offer 30 Stack containing min function
阿里高级专家剖析 | Protocol的标准设计
SQL ROW_ The number() function uses
LeetCode 5270. Minimum path cost in grid (dynamic programming)
LeetCode 6098. 统计得分小于 K 的子数组数目(前缀和+二分查找)
C/s model and P2P model
Jenkins接入Openldap用户认证
20211108 differential tracker
20211115 any n-order square matrix is similar to triangular matrix (upper triangle or lower triangle)
C language: sanziqi
C/S模型与P2P模型
C language: five custom types









