2018年 ICPC四川省省赛 I题 2812: Island (水题)

 2812: Island

Time Limit: 1000 MS Memory Limit: 1048576 KB
Total Submit: 91 Accepted: 17 Page View: 161
Submit Status Discuss

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.

  • Sample Input
  • Raw

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

  • Sample Output
  • Raw

17

Source 

The 2018 Sichuan Provincial Collegiate Programming Contest

Tags 

Submit Status Discuss

给你一颗树,然后可以选择任意一个点,删掉与他相连的边 可以得到一些连通分量,每个连通分量的阳光贡献是这个连通分量上的和,问你删除每个点上连通分量阳光贡献的最大值的最小值是多少

 

直接dfs一下 ans_i,代表以i为子树的所有节点上的和为多少,跑的时候重新建一颗单边树,那一个点可以得到的贡献就只有与他相连的子树和他父亲节点的子树,直接减去就可以得到

#include 
#include 
#define fi first
#define se second
#define endll "\n"
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
///vector(len,val);
using namespace std;
 
typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair  P;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
inline int cmp(db a,db b){ return sign(a - b);}
void debug(int x){  cout << x << endl; }
ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
 
int fa[maxn];
int Find(int x) {   if(x != fa[x]) return fa[x] = Find(fa[x]);  return fa[x];}
ll c,n,k;
ll ans[maxn];
ll a[maxn];
vector >v;
vector >v1;
ll anss;
void dfs(int x,int fa){
    ll Max = 0;
    for(auto d:v[x]){
        if(d == fa) continue;
        v1[x].push_back(d);
        dfs(d,x);
        ans[x] += ans[d];
    }
}
int main() { int m,t; 
            scanf("%d",&t);
    while(t--){
            scanf("%lld",&n);
        v.clear();
        v1.clear();
        v.resize(n + 50);
        v1.resize(n + 50);
        ll sum = 0;
        for(int i = 1;i <= n;i++) scanf("%lld",&a[i]),sum += a[i],ans[i] = a[i];
        for(int i = 1;i < n;i++){
            int x,y,z;
            scanf("%d%d",&x,&y);
            v[x].push_back(y);
            v[y].push_back(x);
        }
        anss = 1e18;
        dfs(1,0); 
        for(int i = 1;i <= n;i++){
            ll Max = 0;
            for(auto d:v1[i]){
                Max = max(Max,ans[d]);
            }
            Max = max(Max,sum - ans[i]);
            anss = min(Max,anss);
        }
        printf("%lld\n",anss);
    }
//    cerr << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}
 

 

你可能感兴趣的:(树形dp)