UVA 1669 && HDU 4118 Holiday's Accommodatio (思路题目--统计子树结点 )

题意:

给你一棵包括n 个结点的树,每个结点上住着一个人,每个人都要换房子,但不能有两个人 住在同一个房子,求的所有人的最大路程长度?

思路:

成都2011年区域赛的题目:

感觉正解好巧妙:

最优解肯定是让每一个边尽量走更多次数。

那么我们只需要算一下每个边 走的最大次数是多少即可。

比如说a 这个结点的子树里(包括a) 一共有u 个结点, 那么剩下的有 n-u 个结点。令f 是a的父亲。

那么f到a 这个边最多走min(u,n-u)次,因为要么是所有u结点都出去,要么是所有n-u个结点都进来。 取一个最小值即可。

这样统计每个边次数 乘以边权即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define Siz(x) (int)x.size()
#define fi first
#define se second
#define mr make_pair
using namespace std;

typedef long long LL;

const int maxn = 1e5+7;


vector<pair<int,int> >g[maxn];
LL ans;
int n, ks, dp[maxn], T;
void dfs(int cur,int fa,int w){
    if (Siz(g[cur]) == 1 && g[cur][0].fi == fa){
        dp[cur] = 1;
        ans += w;
        return;
    }
    dp[cur] = 1;
    for (int i = 0; i < Siz(g[cur]); ++i){
        int v = g[cur][i].fi;
        if (v != fa){
            dfs(v,cur,g[cur][i].se);
            dp[cur] += dp[v];
        }
    }
    ans += (LL)min(dp[cur], n-dp[cur]) * (LL)w;
}

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for (int i = 1; i <= n; ++i)g[i].clear();
        for (int i = 1; i < n; ++i){
            int u,v,w;
            scanf("%d %d %d",&u, &v, &w);
            g[u].push_back(mr(v,w));
            g[v].push_back(mr(u,w));
        }
        ans = 0LL;
        dfs(1,0,0);
        printf("Case #%d: %lld\n",++ks,ans<<1);
    }
    return 0;
}



Holiday's Accommodation

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 200000/200000 K (Java/Others)
Total Submission(s): 3008    Accepted Submission(s): 938


Problem Description
Nowadays, people have many ways to save money on accommodation when they are on vacation.
One of these ways is exchanging houses with other people.
Here is a group of N people who want to travel around the world. They live in different cities, so they can travel to some other people's city and use someone's house temporary. Now they want to make a plan that choose a destination for each person. There are 2 rules should be satisfied:
1. All the people should go to one of the other people's city.
2. Two of them never go to the same city, because they are not willing to share a house.
They want to maximize the sum of all people's travel distance. The travel distance of a person is the distance between the city he lives in and the city he travels to. These N cities have N - 1 highways connecting them. The travelers always choose the shortest path when traveling.
Given the highways' information, it is your job to find the best plan, that maximum the total travel distance of all people.
 

Input
The first line of input contains one integer T(1 <= T <= 10), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer N(2 <= N <= 10 5), representing the number of cities.
Then the followingN-1 lines each contains three integersX, Y,Z(1 <= X, Y <= N, 1 <= Z <= 10 6), means that there is a highway between city X and city Y , and length of that highway.
You can assume all the cities are connected and the highways are bi-directional.
 

Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y represents the largest total travel distance of all people.
 

Sample Input
  
  
2 4 1 2 3 2 3 2 4 3 2 6 1 2 3 2 3 4 2 4 1 4 5 8 5 6 5
 

Sample Output
  
  
Case #1: 18 Case #2: 62
 

Source
 

Recommend
chenyongfu   |   We have carefully selected several similar problems for you:   4119  4112  4114  4115  4111 
 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值