Tree chain problem
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1779 Accepted Submission(s): 577
Problem Description
Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.
There are m chain on the tree, Each chain has a certain weight. Coco would like to pick out some chains any two of which do not share common vertices.
Find out the maximum sum of the weight Coco can pick
There are m chain on the tree, Each chain has a certain weight. Coco would like to pick out some chains any two of which do not share common vertices.
Find out the maximum sum of the weight Coco can pick
Input
The input consists of several test cases. The first line of input gives the number of test cases T (T<=10).
For each tests:
First line two positive integers n, m.(1<=n,m<=100000)
The following (n - 1) lines contain 2 integers ai bi denoting an edge between vertices ai and bi (1≤ai,bi≤n),
Next m lines each three numbers u, v and val(1≤u,v≤n,0<val<1000), represent the two end points and the weight of a tree chain.
For each tests:
First line two positive integers n, m.(1<=n,m<=100000)
The following (n - 1) lines contain 2 integers ai bi denoting an edge between vertices ai and bi (1≤ai,bi≤n),
Next m lines each three numbers u, v and val(1≤u,v≤n,0<val<1000), represent the two end points and the weight of a tree chain.
Output
For each tests:
A single integer, the maximum number of paths.
A single integer, the maximum number of paths.
Sample Input
1 7 3 1 2 1 3 2 4 2 5 3 6 3 7 2 3 4 4 5 3 6 7 3
Sample Output
6HintStack expansion program: #pragma comment(linker, "/STACK:1024000000,1024000000")
Author
FZUACM
Source
题意:一棵树,M条有权值的链,问选择哪些链使得权值总和最大,要求选择的链不能有相交节点。
思路:树形dp,在处理每条链的lca时才进行转移,不选择当前点i的链:dp[i] = sum(dp[j]),j为i所有儿子;选择当前i的链:dp[i] = sum(dp[k])-sum(dp[j])+w,k为与链相连但不在链上的儿子,j为在链上的儿子,w为权值,优化这条式子:dp[i] = sum(sum(dp[p]))-sum(dp[j]) = sum(sum(dp[p])-dp[j]),p为j的儿子,j为链上的点,意思是链上的所有儿子的sum()求和减去它自己,就是链外的儿子了。这样做的话,sum(dp[p])-dp[j]可以用树状数组或线段树快速计算。
失误处:想不到应该处理链的LCA,树状数组容量开小了,注意跟dfs序的大小对应。
#pragma comment(linker,"/STACK:1024000000,1024000000")
# include <iostream>
# include <cstdio>
# include <cstring>
# include <vector>
# define pb push_back
using namespace std;
typedef long long LL;
const int maxn = 1e5+3;
vector<int>v[maxn];
int n, m, in[maxn], out[maxn], id=0;
int h[maxn], fa[maxn][20];
LL s[maxn<<1]={0}, sum[maxn], dp[maxn];
struct node {int u, v, w;};
vector<node>c[maxn];
void update(int x, int num)
{
while(x <= id) s[x] += num, x += x&-x;
}
LL cal(int x)
{
LL ans = 0;
while(x) ans += s[x], x -= x&-x;
return ans;
}
void dfs(int cur, int pre)
{
fa[cur][0] = pre;
for(int i=1; i<20; ++i)
fa[cur][i] = fa[fa[cur][i-1]][i-1];
in[cur] = ++id;
for(auto to : v[cur])
{
if(to == pre) continue;
h[to] = h[cur]+1;
dfs(to, cur);
}
out[cur] = ++id;
}
int lca(int v, int u)
{
if(h[v] > h[u]) swap(u, v);
for(int i=0; i<20; ++i)
if(h[u]-h[v]>>i&1) u = fa[u][i];
for(int i=19; i>=0; --i)
if(fa[v][i] != fa[u][i])
v = fa[v][i], u=fa[u][i];
return v==u?v:fa[v][0];
}
void dfs2(int cur, int pre)
{
for(auto to : v[cur])
{
if(to == pre) continue;
dfs2(to, cur);
sum[cur] += dp[to];
}
dp[cur] = sum[cur];
for(auto j : c[cur])
{
int x=j.u, y=j.v, w=j.w;
dp[cur] = max(dp[cur], sum[cur] + cal(in[x]) + cal(in[y]) + w);
}
update(in[cur], sum[cur]-dp[cur]);
update(out[cur], dp[cur]-sum[cur]);
}
void init()
{
for(int i=1; i<=n; ++i) v[i].clear(), c[i].clear();
id = 0;
memset(s, 0, sizeof(s));
memset(dp, 0, sizeof(dp));
memset(sum, 0, sizeof(sum));
memset(fa, 0, sizeof(fa));
}
int main()
{
int t, w, a, b;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
for(int i=1; i<n; ++i)
{
scanf("%d%d",&a,&b);
v[a].pb(b), v[b].pb(a);
}
dfs(1, 0);
while(m--)
{
scanf("%d%d%d",&a,&b,&w);
c[lca(a,b)].pb({a, b, w});
}
dfs2(1, 0);
printf("%lld\n",dp[1]);
}
return 0;
}