当前位置:网站首页>(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;
}
边栏推荐
猜你喜欢

20220606 Young's inequality for Matrices

turtle库的使用数字时钟模拟时钟动态显示

C/S模型与P2P模型

20211020 academician all drive system

SQL ROW_ The number() function uses

Storage mode of drawings

Exporting MySQL data table documents using Navicat

(state compression dp+ binary) acwing 91 Shortest Hamilton path

Class loading overview

Figure introduction to database neo4j
随机推荐
JUC原子引用与ABA问题
Simple implementation of database link pool
LeetCode 5259. 计算应缴税款总额
C language: file operation
JUC atomic reference and ABA problem
20211018 some special matrices
Storage mode of drawings
@Value不生效及extend/implement其他类无法注入bean的手动注入
Delete soft link
C language: recursive function to realize Hanoi Tower
Collection of articles on virtualization and cloud computing
Cisco, Huawei network equipment
20211108 is transpose multiply a a a positive definite matrix? What are the necessary and sufficient conditions for a to be a positive definite matrix?
LeetCode 202. Happy number
Heap
Attack and defense world PWN shell
阿里高级专家剖析 | Protocol的标准设计
Alibaba senior experts analyze the standard design of protocol
虚拟化和云计算文章大合集
20211020 academician all drive system