UVALive - 6039 Let's Go Green

There are a lot of cities in Tree Land kingdom and each city is connected to other cities with roads
such that there is exists exactly one path to go from one city to any other city. Each road in Tree Land
connects exactly two different cities.
The king of this kingdom wants to encourage his citizen to have a good sense of natural environmen-
tal responsibility. One of his agenda is to promote bicycle, a wheeled human-powered transportation,
as the main vehicle for his citizen to travel between places, rather than motor-cycle or auto-mobile
which might produce a bad pollution to the environment.
In order to determine the progress of his campaign, the king asked the chancellor to monitor the
number of bicycles which pass each road in the kingdom. The chancellor executed the king’s order
immediately and reported the result to his king, i.e., for each road in the kingdom he reported the
number of bicycles passing those roads in one full day. The king was happy, but it didn’t take a long
time for him to realize that this report could be misleading. The report only says the number of bicycles
passing each road and doesn’t say anything about the total number of bicycles being used in that day.
It also doesn’t contain any information on the direction of reported bicycles.
For example, consider a city structure shown in the gure (left) below. There are 2 bicycles passing
road connecting city A and B, 1 on road connecting B and C, 2 on C - D and 1 on C - E. The total
number of bicycles recorded is 2 + 1 + 2 + 1 = 6, but it doesn’t mean the actual number of bicycles
used is 6. There could be cases where a bicycle is used to travel between city
X
and
Z
passing some
other cities, such that this one bicycle is recorded several times, once on each road between
X
and
Z
.
The gure on the right describes one possibility where there are only 3 bicycles, i.e., one is used to
travel between A and B, one is used to travel between A and D passing B and C, and one is used to
travel between D and E passing C.
Assume that no bicycles are used to pass any road more than once. If a road detects P passing
bicycles, then those are P different bicycles.
Now your task is to help the king to determine the minimum number of bicycles used on the report.
Input
The rst line of input contains an integer
T
(
T

10) denoting the number of cases. Each case begins
with an integer
N
(2

N

100
;
000) denoting the number of cities in the kingdom numbered from
1
:::N
. The next
N
大体思路挨个查找每一个点的最大边Max和出最大边意外的其它边加和sum,若Max >= sum, 则结果ans = ans + (Max - sum), 否则ans = (sum + Max) % 2;

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#define N 112345
using namespace std;
int n;
vector <int> Map[N];
int solve();
int main()
{
    int t;
    int kase = 0;
    scanf("%d", &t);
    while(t--)
    {
        cin >> n;
        int u, v, w;
        for(int i = 1; i < n; i++)
        {
            scanf("%d%d%d", &u, &v, &w);
            Map[u].push_back(w);
            Map[v].push_back(w);
        }
        printf("Case #%d: %d\n", ++kase, solve());
        for(int i = 0; i <= n; i++)
        {
            Map[i].clear();
        }
    }
    return 0;
}
int solve()
{
    int pos = 1;
    int ans = 0;
    while(pos <= n)
    {
        int End = Map[pos].size();
        int sum = 0;
        int Max = -1;
        for(int i = 0; i < End; i++)
        {
            Max = max(Map[pos][i], Max);
            sum += Map[pos][i];
        }
        sum -= Max;
        if(sum <= Max)
        {
            ans += (Max - sum);
        }
        else
        {
            ans += ((sum  + Max)% 2);
        }
        pos++;
    }
    return ans / 2;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值