SPOJFTOUR2-Free tour II

FTOUR2 - Free tour II

no tags 

After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, Travel Agent SPOJ goes on with another discount tour.

The tour will be held on ICPC island, a miraculous one on the Pacific Ocean. We list N places (indexed from 1 to N) where the visitors can have a trip. Each road connecting them has an interest value, and this value can be negative (if there is nothing interesting to view there). Simply, these N places along with the roads connecting them form a tree structure. We will choose two places as the departure and destination of the tour.

Since September is the festival season of local inhabitants, some places are extremely crowded (we call them crowded places). Therefore, the organizer of the excursion hopes the tour will visit at most K crowded places (too tiring to visit many of them) and of course, the total number of interesting value should be maximum.

Briefly, you are given a map of N places, an integer K, and M id numbers of crowded place. Please help us to find the optimal tour. Note that we can visit each place only once (or our customers easily feel bored), also the departure and destination places don't need to be different.

Input

There is exactly one case. First one line, containing 3 integers N K M, with 1 <= N <= 200000, 0 <= K <= M, 0 <= M <= N.

Next M lines, each line includes an id number of a crowded place.

The last (N - 1) lines describe (N - 1) two-way roads connected N places, form a b i, with a, b is the id of 2 places, and i is its interest value (-10000 <= i <= 10000).

Output

Only one number, the maximum total interest value we can obtain.

Example

Input:
8 2 3
3
5
7
1 3 1
2 3 10
3 4 -2
4 5 -1
5 7 6
5 6 5
4 8 3


Output:
12

Explanation

We choose 2 and 6 as the departure and destination place, so the tour will be 2 -> 3 -> 4 -> 5 -> 6, total interest value = 10 + (-2) + (-1) + 5 = 12 
* Added some unofficial cases


题意:给出一棵树,其中每一节点都有一个颜色(黑色或白色),每条边都有一个权值,现要求在树中找出一条链,使得链上的黑点数不超过k,求权值和最大

解题思路:树分治,树状数组维护前缀最大值,注意树状数组不能每次全部清空,那样就是n^2的效率了


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

const int maxn = 2e5 + 10;
int n, m, K, x, y, w;
int s[maxn], nt[maxn * 2], e[maxn * 2], val[maxn * 2], cnt;
int sum[maxn], mx[maxn], dis[maxn], vis[maxn];
int c[maxn], ma[maxn], res;
int use[20 * maxn], r;

int lowbit(int k) { return k&-k; }

int dfs(int k, int fa, int p)
{
	int ans = 0;
	sum[k] = (mx[k] = 0) + 1;
	for (int i = s[k]; ~i; i = nt[i])
	{
		if (e[i] == fa || vis[e[i]]) continue;
		int temp = dfs(e[i], k, p);
		sum[k] += sum[e[i]];
		mx[k] = max(mx[k], sum[e[i]]);
		if (mx[temp] < mx[ans]) ans = temp;
	}
	mx[k] = max(mx[k], p - sum[k]);
	return mx[k] < mx[ans] ? k : ans;
}

void update(int k, int val)
{
	for (int i = k; i <= K; i += lowbit(i))
	{
		use[r++] = i;
		ma[i] = max(ma[i], val);
	}
}

int query(int k)
{
	int ans = 0;
	for (int i = k; i; i -= lowbit(i)) ans = max(ma[i], ans);
	return ans;
}

void get(int k, int fa, int p, int len)
{
	if (p > K) return;
	int pp = K - p;
	if (pp < 0) return;
	res = max(res, query(pp + 1) + len);
	for (int i = s[k]; ~i; i = nt[i])
	{
		if (vis[e[i]] || fa == e[i]) continue;
		get(e[i], k, p + c[e[i]], len + val[i]);
	}
}

void put(int k, int fa, int p, int len)
{
	if (p > K) return;
	if (len > dis[p])
	{
		dis[p] = len;
		update(p + 1, dis[p]);
	}
	for (int i = s[k]; ~i; i = nt[i])
	{
		if (vis[e[i]] || e[i] == fa) continue;
		put(e[i], k, p + c[e[i]], len + val[i]);
	}
}

void Find(int k)
{
	for (int i = s[k]; ~i; i = nt[i])
	{
		if (vis[e[i]]) continue;
		get(e[i], k, c[k] + c[e[i]], val[i]);
		put(e[i], k, c[e[i]], val[i]);
	}
	for (int i = 0; i<r; i++) ma[use[i]] = dis[use[i]] = 0;
	r = 0;
}

void solve(int k, int p)
{
	int y = dfs(k, k, p);
	vis[y] = 1;
	Find(y);
	for (int i = s[y]; ~i; i = nt[i])
	{
		if (vis[e[i]]) continue;
		if (sum[e[i]] < sum[y]) solve(e[i], sum[e[i]]);
		else solve(e[i], p - sum[y]);
	}
}

int main()
{
	scanf("%d%d%d", &n, &K, &m);
	memset(s, -1, sizeof s);
	mx[cnt = 0] = INF;
	res = 0;
	for (int i = 1; i <= m; i++) scanf("%d", &x), c[x] = 1;
	for (int i = 1; i < n; i++)
	{
		scanf("%d%d%d", &x, &y, &w);
		nt[cnt] = s[x], s[x] = cnt, e[cnt] = y, val[cnt++] = w;
		nt[cnt] = s[y], s[y] = cnt, e[cnt] = x, val[cnt++] = w;
	}
	solve(1, n);
	printf("%d\n", res);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值