POJ 1655 - Balancing Act

Description

Consider a tree T with N \((1 \le N \le 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:
1.1
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 \le t \le 20)\), the number of test cases. The first line of each test case contains an integer N \((1 \le N \le 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

Resume

定义树上节点的balance为删除该点后产生的森林中最大的树的节点数,求给定树的所有节点的balance的最小值。

Analysis

建图,转化成有根树,然后树形dp。
\(size[i]\)为以节点\(i\)为根的子树节点数,\(f[i]\)为节点\(i\)的balance值。则状态转移方程为\[f[i] = max(N-size[i],\max_{j \in s[i]}size[j]) \quad \text{where $s[i]$ is the set of the son nodes of node i.}\]也就是说每一个节点的balance值是 其上方节点数之和 与 所有以子节点为根的子树节点数 中的最大值。在遍历过程中记录最小的balance值即可。

技巧:通过BFS转化为有根树时产生的队列可以作为dp顺序使用。

Code

//////////////////////////////////////////////////////////////////////
//Target: POJ 1655 - Balancing Act
//@Author: Pisceskkk
//Date: 2019-2-21
//////////////////////////////////////////////////////////////////////

#include<cstdio>
#include<cstring>
#define N (int)2e5+10
#define INF 1e9
#define Max(a,b) (a>b?a:b)
using namespace std;

int t,n,s[N],ans[N],min_i,min_b;
int q[N],d[N];

int he[N];
struct edge{
    int to,ne;
}e[N<<1];

void add_2(int x,int y,int i){
    i<<=1;
    e[i].ne = he[x];
    he[x] = i;
    e[i].to = y;
    i++;
    e[i].ne = he[y];
    he[y] = i;
    e[i].to = x;
}

int bfs(int s){
    int h=0,t=0;
    memset(d,0,sizeof(d));
    memset(q,0,sizeof(q));
    q[h++]=s;
    d[s] = 1;
    while(t<h){
        int x=q[t++],y;
        for(int i=he[x];i;i=e[i].ne){
            y = e[i].to;
            if(!d[y]){
                d[y] = 1;
                q[h++]=y;
            }
        }
    }
    return h;
}

int main(){
    scanf("%d",&t);
    while(t--){
        memset(e,0,sizeof(e));
        memset(he,0,sizeof(he));
        memset(s,0,sizeof(s));
        memset(ans,0,sizeof(ans));
        min_i = min_b = INF;
        scanf("%d",&n);
        int x,y;
        for(int i=1;i<n;i++){
            scanf("%d %d",&x,&y);
            add_2(x,y,i);
        }
        bfs(1);
        for(int i=n-1;i>=0;i--){
            int x = q[i];
            s[x] = 1;
            for(int i=he[x];i;i=e[i].ne){
                y = e[i].to;
                s[x] += s[y];
                ans[x] = Max(ans[x],s[y]);
            }
            ans[x] = Max(ans[x],n-s[x]);
            if(ans[x] < min_b){
                min_b =  ans[min_i=x];
            }
            else if(ans[x] == min_b && x < min_i){
                min_i = x;
            }
        }
        printf("%d %d\n",min_i,min_b);
    }

}

转载于:https://www.cnblogs.com/pisceskkk/p/10424031.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值