POWER OJ 2812 Island(2018四川ACM:树的重心)

题目链接

Description 

Rinne dreads sunlight, so she wants to find a refuge with the weakest sunlight. The place where Rinne stay now can be described as a tree with nn vertices. A tree is an undirected graph where any two vertices are connected by exactly one simple path. Rinne can stand on any one of the vertices. Each vertex in the tree has a value susu indicating the light intensity.

If Rinne stands on vertex ii, the light intensity she will suffer is calculated as followed.

1. We delete vertex ii and its adjacent edges in the tree, and then the tree becomes several connected components.

2. The light intensity of a component is the sum of light intensity of all vertices within it.

3. The maximum light intensity among all of the components is the light intensity that Rinne will suffer.

Rinne wants to ensure the light intensity she will suffer as less as possible. Can you help her?

Input 

The first line contains a integer TT representing the number of test cases. In each test case, the first line contains one positive integer nn. The second line contains nn integers. The i−thi−th integer sisi represents the light intensity of vertex ii. The following n−1n−1 lines each contains two integers u;vu;v representing that there is an undirected edge connecting vertex uu and vv. It is guaranteed that the input graph is a tree. • 1≤T,∑n≤2×105,0≤si≤2×105,1≤u;v≤n1≤T,∑n≤2×105,0≤si≤2×105,1≤u;v≤n

Output 

For each test case, output the minimum light intensity Rinne will suffer.

1 10 4 7 5 8 9 2 6 0 1 3 1 2 1 3 2 4 5 2 3 6 3 7 4 8 4 9 5 10

17

题意:就是一个数,每个节点有自己的权值,现在去掉一点节点,剩下的每个联通块中都有自己的权值的和。现在要求这个和的最大时的最小值是多少。

题解:其实是一个比较裸的树的重心的题。(这个题类似传送门

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
//#include<unordered_map>
//#include<unordered_set>
const int maxn=2e5+10;
const int mod=1e9+7;
const int inf=1e9;
const long long onf=1e18;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define mid l+(r-l)/2
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI 3.14159265358979323846
int dir[4][2]= {0,-1,-1,0,0,1,1,0};
int dx[]= {-2,-2,-1,-1,1,1,2,2};
int dy[]= {-1,1,-2,2,-2,2,-1,1};
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int head[maxn],cnt;
int val[maxn];
ll sum=0,Min,sam[maxn];
struct node
{
    int v,next;
}tree[maxn];
void add_edge(int u,int v)
{
    tree[cnt].v=v;
    tree[cnt].next=head[u];
    head[u]=cnt++;
}
void dfs(int u,int pre)
{
    ll t_Max=0;
    for(int i=head[u];i!=-1;i=tree[i].next)
    {
        int v=tree[i].v;
        if(v!=pre)
        {
            dfs(v,u);
            sam[u]+=sam[v];
            t_Max=max(t_Max,sam[v]);
        }
    }
    t_Max=max(t_Max,sum-sam[u]);
    Min=min(Min,t_Max);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        sum=0,cnt=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&val[i]);
            head[i]=-1;
            sum+=val[i],sam[i]=val[i];
        }
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add_edge(u,v);
            add_edge(v,u);
        }
        Min=onf;
        dfs(1,-1);
        printf("%lld\n",Min);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值