【poj1655】【poj3107】【求树的重心】

【树形DP】树的重心详解+多组例题详解 目录定义:性质:算法分析:POJ 1655 Balancing Act(求重心)POJ 3107 GodfatherP1364 医院设置(树形DP) 定义: 树的重心也叫树的质心。对于一棵树n个节点的无根树,找到一个点,使得把树变成以该点为根的有根树时,最大子树的结点数最小。 树的重心定义为树的某个节点,当去掉该节点后,树的各个连通分量中,节点数最多的连通分量其节点数达到最小值。树可能存在多个重... 阅读详情

先来说一下怎样来求树的直径:

假设 s-t这条路径为树的直径,或者称为树上的最长路

现有结论,从任意一点u出发搜到的最远的点一定是s、t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路

证明:

1 设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T则

dis(u,T) >dis(u,s) 且 dis(u,T)>dis(u,t) 则最长路不是s-t了,与假设矛盾

2 设u不为s-t路径上的点

首先明确,假如u走到了s-t路径上的一点,那么接下来的路径肯定都在s-t上了,而且终点为s或t,在1中已经证明过了

所以现在又有两种情况了:

1:u走到了s-t路径上的某点,假设为X,最后肯定走到某个端点,假设是t ,则路径总长度为dis(u,X)+dis(X,t)

2:u走到最远点的路径u-T与s-t无交点,则dis(u-T) >dis(u,X)+dis(X,t);显然,如果这个式子成立,

则dis(u,T)+dis(s,X)+dis(u,X)>dis(s,X)+dis(X,t)=dis(s,t)最长路不是s-t矛盾

然后对于树的重心的求法其实跟上面的情况类似:

我们用son[i]来记录i的子节点的数目,balance为删除节点i时的最大的节点数时多少。
那么我们就可以知道,balance=max(son[j]+1,n-son[x]-1)
j∈i的子节点。
然后我们dfs一遍就可以在n的时间内求出树的重心了。

Balancing Act

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9890 Accepted: 4063
Description

Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1…N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the forest T created by deleting that node from T.
For example, consider the tree:

Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two.

For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number.
Input

The first line of input contains a single integer t (1 <= t <= 20), the number of test cases. The first line of each test case contains an integer N (1 <= N <= 20,000), the number of congruence. The next N-1 lines each contains two space-separated node numbers that are the endpoints of an edge in the tree. No edge will be listed twice, and all edges will be listed.
Output

For each test case, print a line containing two integers, the number of the node with minimum balance and the balance of that node.
Sample Input

1
7
2 6
1 2
1 4
4 5
3 7
3 1
Sample Output

1 2

题意就是要求出树的重心和重心节点的编号,输出时编号在前。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int N=20010;
const int inf=2100000000LL;
vector <int> a[N];
int t,son[N],n,No,ans;
bool f[N];
int in()
{
    int x=0;
    char ch;
    ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x;
}
void dfs(int x)
{
    int i,j,u,balance=0;
    son[x]=0;
    f[x]=false;
    for(i=0;i<a[x].size();++i){
        u=a[x][i];
        if(!f[u]) continue;
        dfs(u);
        son[x]+=son[u]+1;
        balance=max(balance,son[u]+1);
    }
    balance=max(balance,n-son[x]-1);
    if(balance<ans||(balance==ans&&No<x)){
        ans=balance;
        No=x;
    }
}
int main()
{
    t=in();
    while(t--){
        int i,j,x,y;
        n=in();
        ans=inf;
        memset(f,1,sizeof(f));
        for(i=1;i<=n;++i) a[i].clear();
        for(i=1;i<n;++i){
            x=in();y=in();
            a[x].push_back(y);
            a[y].push_back(x);
        }
        dfs(1);
        printf("%d %d\n",No,ans);
    }
}

Godfather

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5061 Accepted: 1767
Description

Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders.

Unfortunately, the structure of Chicago mafia is rather complicated. There are n persons known to be related to mafia. The police have traced their activity for some time, and know that some of them are communicating with each other. Based on the data collected, the chief of the police suggests that the mafia hierarchy can be represented as a tree. The head of the mafia, Godfather, is the root of the tree, and if some person is represented by a node in the tree, its direct subordinates are represented by the children of that node. For the purpose of conspiracy the gangsters only communicate with their direct subordinates and their direct master.

Unfortunately, though the police know gangsters’ communications, they do not know who is a master in any pair of communicating persons. Thus they only have an undirected tree of communications, and do not know who Godfather is.

Based on the idea that Godfather wants to have the most possible control over mafia, the chief of the police has made a suggestion that Godfather is such a person that after deleting it from the communications tree the size of the largest remaining connected component is as small as possible. Help the police to find all potential Godfathers and they will arrest them.

Input

The first line of the input file contains n — the number of persons suspected to belong to mafia (2 ≤ n ≤ 50 000). Let them be numbered from 1 to n.

The following n − 1 lines contain two integer numbers each. The pair ai, bi means that the gangster ai has communicated with the gangster bi. It is guaranteed that the gangsters’ communications form a tree.

Output

Print the numbers of all persons that are suspected to be Godfather. The numbers must be printed in the increasing order, separated by spaces.

Sample Input

6
1 2
2 3
2 5
3 4
3 6
Sample Output

2 3
这个题是要求求出这个树上的所有重心,并按照节点的编号从小到大输出。
值得注意的是,这道题如果用stl会T,所以我用了next数组

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=50010;
const int inf=210000000;
int point[N],next[N*10],n,tot=1,minn,ans[N],son[N];
bool f[N];
struct s{
    int st,en;
}aa[N*10];
void add(int x,int y)
{
    tot+=1;next[tot]=point[x];point[x]=tot;
    aa[tot].st=x;aa[tot].en=y;
}
void dfs(int x)
{
    int u,balance=0,i;
    f[x]=false;
    son[x]=0;
    for(i=point[x];i;i=next[i]){
        u=aa[i].en;
        if(!f[u]) continue;
        dfs(u);
        son[x]+=son[u]+1;
        balance=max(balance,son[u]+1);
    }
    balance=max(balance,n-son[x]-1);
    if(balance<minn){
        ans[0]=1;
        ans[1]=x;
        minn=balance;
    }
    else if(balance==minn){
        ans[0]+=1;
        ans[ans[0]]=x;
    }
}
int main()
{
    int i,j,x,y;
    scanf("%d",&n);
    memset(f,1,sizeof(f));
    for(i=1;i<n;++i){
        scanf("%d%d",&x,&y);
        add(x,y);add(y,x);
    }
    minn=inf;
    dfs(1);
    sort(ans+1,ans+ans[0]+1);
    for(i=1;i<=ans[0];++i)
      printf("%d ",ans[i]);
    printf("\n");
}
SARScape实战:集成GACOS数据优化InSAR大气校正全流程 本文详细介绍了如何在SARScape软件中集成GACOS数据,以优化InSAR大气校正的全流程。通过实战步骤,从GACOS数据申请、下载、导入,到在单景干涉图和SBAS-InSAR流程中应用,有效剥离大气相位延迟,显著提升形变监测结果的精度与可靠性,实现从“雾里看花”到“拨云见日”的转变。 阅读详情

相关推荐

ESP32锂电池电量检测实战:从引脚选择到低功耗优化(附完整电路图)

本文详细介绍了ESP32锂电池电量检测的实战方案,从ADC引脚选择到低功耗优化,提供完整的电路图和代码实现。通过MOS管智能开关和深度睡眠技术,显著降低静态功耗至0.5μA,提升设备续航能力。适合物联网开发者解决电量检测中的常见问题。

weixin_28731223的博客 465

POJ-1655-树的重心

树的重心模板题 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <queue> #include <functional> #include <vector> #include <stack> #inc

lifeforge的博客 246

从理论到实践:使用Sigrity SystemSI完成DDR4信号完整性仿真全流程解析

本文详细解析了使用Sigrity SystemSI完成DDR4信号完整性仿真的全流程,从核心挑战到实战操作。文章深入探讨了DDR4信号完整性问题,介绍了SystemSI工具链的演进与分工,并提供了从前期准备到高级技巧的详细指南,帮助工程师有效解决DDR4设计中的信号完整性问题。

weixin_30505225的博客 517

poj 1655(3107、2378) 求树的重心

题意:求树的重心。树的重心定义为:

dearmango 650

汽车级智能栅极驱动器深度解析 | 连载01:DRV871x-Q1系列产品架构与核心特性

集成度:单芯片集成多通道驱动器、电源管理、电流检测智能化:自适应控制算法,减少外部元件可靠性:多重保护机制,满足汽车级要求灵活性:SPI和硬件两种接口,适应不同应用需求。

VehSwHwDeveloper的博客 1170

vislib_vex5:面向VEX V5的嵌入式视觉处理库

嵌入式视觉处理是机器人实时感知的核心能力,其本质是在资源受限环境下实现图像采集、色彩转换、阈值分割与目标定位的确定性流水线。原理上依赖硬件加速(如DMA2D)、定点运算优化与中断协同机制,以规避浮点开销和动态内存分配。技术价值在于提供亚毫秒级、无RTOS依赖、低延迟可预测的视觉基础能力,显著提升教育及竞赛机器人在动态场景下的响应鲁棒性。典型应用场景包括色块跟踪、目标居中控制、自动导航中的简易视觉伺服等。本文聚焦于专为VEX V5主控(STM32H743+图像协处理器)深度优化的轻量库vislib_vex5,

weixin_31185473的博客 610

Poj 1655 【树的重心】

题目链接:http://poj.org/problem?id=1655 题解: 一开始建树时是取小的点指向大的点,然后一直WA后来发现这样建树是有问题的,可能会有(1,3)(2,3)这种出现1->3,2->3导致1、2不联通的情况出现。 #include #include #include #include #include #include #include #inc

t51645的博客 413

【系统分析师】2025年上半年真题:案例分析-答案及详解(回忆版)

摘要: 试题一考查面向对象分析,涉及用例模型构建的四个阶段(识别参与者、合并需求、细化描述、调整模型),分析CRM系统的参与者与用例,并说明用例间的包含、扩展和泛化关系。 试题二探讨嵌入式Docker,包括容器的轻量性、可移植性及隔离性含义,10项措施与7个规范方面的对应关系,以及优化容器切换时间的三种方法(如无状态连接优化)。 试题三聚焦数据库设计,分析订单表的函数依赖与范式分解(BCNF),讨论分解后是否保持函数依赖及无损性,并针对触发器的性能缺陷提出优化方案。 试题四解析Web开发中的MVC架构与SS

数据知道的博客 6631

poj - 1655 Balancing Act

给出一个树,若去掉其中一个结点,剩下的子树中规模最大的树的规模(估且称为D值),就是该点的D值。求所有结点中D值最小的点,输出点及其D值。 这题用两次DFS,第一次求出所有结点的子结点数,第二次求出所有结点的D值。 1 #include <stdio.h> 2 #include <string.h> 3 #include <vector> ...

weixin_34061482的博客 158

RK3568适配MIPI屏幕触摸,触摸延时很大

我用的是系统,接了一个,设备树已经正确配置,evtest测试坐标值和触摸位置都是准确的。但是出现了一个:我在屏幕上点一下,要等大约 2 秒才能在evtest里看到坐标事件打印。这种现象在 Goodix 系列触摸驱动里比较少见,一般意味着。

小灰灰的博客 1372

第244篇 视觉SLAM后端之BA优化——Bundle Adjustment的原理

本文介绍了SLAM后端优化中的Bundle Adjustment(BA)技术。BA通过同时优化相机位姿和3D路标点位置,最小化重投影误差,解决前端估计的累积误差问题。文章详细讲解了BA的数学原理,包括目标函数构建、Gauss-Newton法和Levenberg-Marquardt法求解过程,以及利用稀疏性和Schur补加速计算的关键技术。对比了全量BA和增量BA的优缺点,并介绍了Ceres、g2o等工程实现工具。BA作为SLAM后端的核心算法,其理解深度直接影响系统性能优化能力。

郑伟强dev的专栏 366

poj3107:Godfather

Problem Description Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders. Unfortu

某某某的博客 314

9、眼底图像智能分析:糖尿病视网膜病变的自动化识别与分级系统

本博客介绍了一种基于深度学习的糖尿病视网膜病变(DR)自动化识别与分级系统。该系统通过视网膜图像的预处理、数据增强、集成模型的构建与优化,实现了对DR的高效识别与分级。识别系统采用二分类任务,准确率达到97.67%,而分级系统通过四分类策略取得了95.2%的准确率。系统在基层社区和医院中具有广泛的应用前景,为DR的早期筛查和精准诊断提供了有效工具。

pea55的博客 148

POJ 1655 +POJ 3107【求树的重心】

树的重心:树的重心也叫树的质心。找到一个点,其所有的子树中最大的子树节点数最少, 那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡。 换句话说,删除这个点后最大连通块(一定是树)的结点数最小。 eg: POJ 1655 http://poj.org/problem?id=1655 题目大意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,

Jane_JXR的博客 689

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

求树的重心 POJ 1655、POJ 3107 树形DP

树的重心:在树中删去重心后,剩下的子树的最大子树大小最小,则这个节点为重心POJ 1655:直接求树的重心 思路:递归每个节点的时候,记录一下这个节点所有子树的大小和,然后记录最大子树的大小,维护最小的最大子树大小和相应的节点即可 代码:#include <iostream> #include <cstdio> #include <cstring> #define sf scanf #defin

SolarDomo的博客 445

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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

题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心。 剩下的数的 最大结点dp[i]=max(max(s[j]),n-s[i]) 其中的s[i]为以i为根节点的子树的结点总数,j为i的孩子。 //思路和代码都是比较好理解的 代码: #include&...

weixin_30830327的博客 79

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

树的重心的定义是:一个点的所有子树中节点数最大的子树节点数最小。 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点。 POJ 1655 题目大意:直接给你一棵树,让你求树的重心,如果有多个,找出编号最小的那个,并输出他的子树当中最大的节点数。 思路:利用dfs求出每个点的所有孩子数目,然后在dfs一下求出树的重心。 用途:树的重心在树分治的点分治中...

dizhuo0219的博客 139

树的重心

定义及性质 定义1:找到一个点,删除它得到的森林中最大的子树节点数最少(也就是最大的连通块的点数最小),那么这个点就是这棵树的重心。 定义2:删除重心后得到的所有子树,其顶点数必然不超过n/2 性质1:树中所有点到某个点的距离和中,到重心的距离和是最小的;如果有两个重心,那么他们的距离和一样。 性质2:把两个树通过一条边相连得到一个新的树,那么新的树的重心在连接原来两个树的重心的路径上。 性质3:...

青烟绕指柔的博客 377

树的重心及其一些性质。poj3107 && poj 1655

定义 树的重心:删去重心后,生成的多棵树尽可能平衡。即以这个点为根,那么所有的子树(不算整个树自身)的大小都不超过整个树大小的一半。(所有子树的最大块的节点最小) 性质 性质 1 :树中所有点到某个点的距离和中,到重心的距离和是最小的,如果有两个距离和,他们的距离和一样。 性质 2 :把两棵树通过某一点相连得到一颗新的树,新的树的重心必然在连接原来两棵树重心的路径上。 性

小菜鸟。。 470

树的重心 poj - 1655 和 poj - 3107

首先知道 树的重心定义为: 找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 实际上树的重心在树的点分治中有重要的作用, 可以避免N^2的极端复杂度(从退化链的一端出发),保证nlogn的复杂度, 利用树型dp可以很好地求树的重心. 但是贪心的写法更好写 也更好理解, 正确性也可保证, 所以用贪心好乐.. poj - 1...

Anxdada -- 我等风来也等你 619

如何求树的重心

树的重心:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 打这个blog主要是为了等一下写的点分治做铺垫。 例题T1:poj1655 题意:求一棵树上的重心编号和去掉这个重心以后最大子树的节点数。 在dfs上做文章,每次对于一个点,记录下他子树中的最大节点,然后更新一下编号和子树节点大小就可以了。#include <iostr

老臣 1119

poj3107 树的重心 非stl实现

poj3107 求树的重心,全部输出 (poj1655 输出序号最小的节点,数据规模小一点,使用stl) #include #include #include #include using namespace std; int N; int v[50010*2],next[50010*2],head[50010],E; int vis[50010]; int num[50010],dp[5

Time flies 1817

树的重心及其一些性质

树的重心及其一些性质

追梦赤子心 4461

poj 1655 求树的重心

题目:点击打开链接 题意:有n-1条边链接n个点形成树形结构,问删除哪个点使得,形成的最大连通分量节点数最少,如有多解,输出字典序最小的 分析:和poj3107那题一样,求树的重心,dfs一次,从叶子到根转移,求出每个点的子树的最大连通分量,再和父节点所在一侧的连通分量比较,取最大的一个,再看是否比其他点形成的连通分量节点数大,就这样,把重心点放到一个数组中,排序后,取第一个。 #inc

01的世界 510

poj3107 树的重心

http://poj.org/problem?id=3107 Description Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arre

ACM,再见! 1023

POJ 1655 Balancing Act 树的重心

Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be

swust_fang 458
上一篇: 【bzoj1044】【HAOI2008】【木棍分割】
下一篇: 【poj2152】【Fire】【树形dp】
FZHvampire
博客等级 码龄12年 24粉丝 155原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值