求树的重心的模板题,size[u]维护以u为根的子树大小,f[u]表示去掉u后的最大子树。

 1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 using namespace std;
5 const int INF=0x7f7f7f7f;
6 const int N=20005;
7 int head[N],to[N*2],nxt[N*2],f[N],size[N];
8 int tot,n,T,center,num;
9
10 void add(int x,int y){
11 nxt[++tot]=head[x];
12 head[x]=tot;
13 to[tot]=y;
14 }
15
16 void dfs(int u,int fa){
17 size[u]=1,f[u]=0;
18 for(int i=head[u];i;i=nxt[i]){
19 int v=to[i];
20 if(v==fa) continue;
21 dfs(v,u);
22 size[u]+=size[v];
23 f[u]=max(f[u],size[v]);
24 }
25 f[u]=max(f[u],n-size[u]);
26 if(f[u]<f[center]||f[u]==f[center]&&u<center){
27 center=u;
28 num=f[u];
29 }
30 }
31
32 void init(){
33 memset(head,0,sizeof(head));
34 tot=0;
35 center=num=0;
36 }
37
38 int main(){
39 scanf("%d",&T);
40 while(T--){
41 init();
42 scanf("%d",&n);
43 int x,y;
44 for(int i=1;i<n;i++){
45 scanf("%d%d",&x,&y);
46 add(x,y);add(y,x);
47 }
48 f[0]=INF;
49 dfs(1,0);
50 printf("%d %d\n",center,num);
51 }
52 return 0;
53 }

POJ1655 Balancing Act (树的重心)的更多相关文章

  1. POJ1655 Balancing Act(树的重心)

    题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP ...

  2. poj-1655 Balancing Act(树的重心+树形dp)

    题目链接: Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11845   Accepted: 4 ...

  3. 『Balancing Act 树的重心』

    树的重心 我们先来认识一下树的重心. 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 根据树的重心的定义,我们可 ...

  4. POJ 1655 Balancing Act 树的重心

    Balancing Act   Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...

  5. poj1655 Balancing Act 找树的重心

    http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  6. POJ1655 Balancing Act(树的重心)

    树的重心即树上某结点,删除该结点后形成的森林中包含结点最多的树的结点数最少. 一个DFS就OK了.. #include<cstdio> #include<cstring> #i ...

  7. POJ-1655 Balancing Act(树的重心)

    Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...

  8. poj1655 Balancing Act (dp? dfs?)

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14247   Accepted: 6026 De ...

  9. poj3107 求树的重心(&& poj1655 同样求树的重心)

    题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...

随机推荐

  1. c语言中的gets和fgets的使用差别

    gets和fgets的差别 2022年6月30日 #include<stdio.h> #include<string.h> #define STLEN 8 int main(i ...

  2. 【水】关于 __attribute__

    目录 1. weak 2. aligned 3. __packed__ 4. always_inline 1. weak 实现如果 Func1 被定义了,我就调用 Func1, 否则就调用 MyFun ...

  3. javascript引用"bug"带来的"继承"

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. 海豚调度5月Meetup:6个月重构大数据平台,帮你避开调度升级改造/集群迁移踩过的坑

    当今许多企业都有着技术架构的DataOps程度不够.二次开发成本高.迁移成本高.集群部署混乱等情况,团队在技术选型之后发现并不适合自己的需求,但是迁移成本和难度又比较大,甚至前团队还留下了不少坑,企业 ...

  5. FTP 基础 与 使用 Docker 搭建 Vsftpd 的 FTP 服务

    FTP 基础 与 使用 Docker 搭建 Vsftpd 的 FTP 服务 前言 最近的工作中,需要将手机上的文件发送到公司的 FTP 的服务器.按照从前的思路,自然是,先将文件传到电脑,再由电脑上传 ...

  6. 自动化选课(Python + selenium

    ​ 前几天听到朋友说自己选课事情,突发奇想想要搞这样一个东西,但是由于各种原因只做到以下的完成度,具体的情况也会在解释的最后留下.这个只适用于曲师大的教务系统,因为用的这个系统来进行的一个调试,对于其 ...

  7. 简单创建一个SpringCloud2021.0.3项目(二)

    目录 1. 项目说明 1. 版本 2. 用到组件 3. 功能 2. 上一篇教程 3. 创建公共模块Common 4. 网关Gateway 1. 创建Security 2. Security登陆配置 3 ...

  8. React报错之Rendered more hooks than during the previous render

    正文从这开始~ 总览 当我们有条件地调用一个钩子或在所有钩子运行之前提前返回时,会产生"Rendered more hooks than during the previous render ...

  9. Arrays.asList()你真的知道怎么用吗?

    发现问题 前几天在看别人的项目的时候,发现一个问题,简单复现一下这个问题 // 注意这是一个Integer对象的数组哦 Integer[] arr = new Integer[]{9999,88,77 ...

  10. K8S Service_Ingress

    Service 在K8S的世界里,虽然每个Pod都会被分配一个单独的IP地址,但这个IP地址会随着Pod的销毁而消失 Service(服务)就是用来解决这个问题的核心该你啊 一个Service可以看作 ...