当前位置:网站首页>(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;
}
边栏推荐
- Subspace of 20211004 matrix
- JUC 字段更新器
- 1-2 24:00 (20 points) [CSP certification true question]
- [implementation of depth first search]
- I have summarized the knowledge points of JS [intermediate and advanced] for you
- Calculate the number of days between two times (supports cross month and cross year)
- 20211006 linear transformation
- C language time difference calculation
- C # introductory series (XIII) -- getting to know the structure for the first time
- CAS无锁
猜你喜欢

Exporting MySQL data table documents using Navicat

an error occurred while trying to rename a file in the destination directory code 5

C language: file operation

阿里高级专家剖析 | Protocol的标准设计

Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing

Figure introduction to database neo4j

线上调试工具Arthas高级

Use typescript to complete simple snake eating function

20220606 Young's inequality for Matrices

攻防世界PWN play 条件竞争漏洞的利用
随机推荐
Haproxy + keepalived for high availability load balancing of MySQL
Delete soft link
Zigzag transformation
LeetCode 5270. 网格中的最小路径代价(动态规划)
CAS无锁
Neo4j Environment Building
20220524 how to install coppeliasim to disk D
Necessary and sufficient conditions for diagonalization of 20211115 matrix; The full rank matrix does not necessarily have n linearly independent eigenvectors; Symmetric matrices must be diagonalized
20211005 Hermite matrix and some properties
Cisco, Huawei network equipment
Yolov5 face video stream
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 583. Deleting two strings
Neo4j environment construction
Sort() sort function
Jenkins接入Openldap用户认证
JUC 字段更新器
Simulink variant model and variant subsystem usage
Library management system based on wechat applet Rar (thesis + source code)
How to build an aby framework and run an instance