topic
Subject to the effect
Give you many descendants to ancestral chains from a tree and the tree, select each chain required a certain price, ask all the points covered the whole tree of minimum cost is.
\ (n, m \ leq 100000 \)
Correct
(Because of the time is too long, so just say positive solutions forget)
For such problems, apparently the DP practice of violence.
Set \ (f_ {i, j} \) represents \ (I \) subtree entirely covered, wherein a chain stretched out to a depth of \ (J \) a minimum cost at the time of the ancestors.
Transfer not repeat them here.
You can then segment tree optimization.
There are two cases: the \ (I \) to the chain extending is the highest sub-tree; from \ (I \) to the chain extending is the highest.
We handpicked a chain is the highest, do not control whether or not there are other chains higher than its situation, because if there is such a case, then the answer to this state will be overwritten.
First, a note of each sub-optimal answer of the tree and, denoted by \ (SUM \) .
Enumeration sub-tree, for all of its states plus \ (SUM \) , minus the optimal answer itself. That is the answer to the other sub-tree and add to it. For myself, the direct use of \ (sum \) plus the cost of the selected chain.
Then you can merge tree line.
Code
using namespace std;
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cassert>
#define N 300010
#define INF 1000000000000000000
int n,m;
struct EDGE{
int to;
EDGE *las;
} e[N*2];
int ne;
EDGE *last[N];
int dep[N];
struct EDGE2{
int to,w;
EDGE2 *las;
} e2[N*2];
int ne2;
EDGE2 *last2[N];
struct Node{
Node *l,*r;
long long mn,tag;
inline void pushdown(){
l->mn+=tag,l->tag+=tag;
r->mn+=tag,r->tag+=tag;
tag=0;
}
inline void update(){mn=min(l->mn,r->mn);}
} d[N*30],*null;
int cnt;
inline Node *newnode(){return &(d[++cnt]={null,null,INF,0});}
Node *root[N];
void change(Node *t,int l,int r,int x,long long c){
if (l==r){
t->mn=min(t->mn,c);
return;
}
t->pushdown();
int mid=l+r>>1;
if (x<=mid)
change(t->l==null?t->l=newnode():t->l,l,mid,x,c);
else
change(t->r==null?t->r=newnode():t->r,mid+1,r,x,c);
t->update();
}
void cut(Node *t,int l,int r,int en){
if (t==null)
return;
t->pushdown();
int mid=l+r>>1;
if (en<mid){
cut(t->l,l,mid,en);
t->r=null;
}
else
cut(t->r,mid+1,r,en);
t->update();
}
Node *merge(Node *a,Node *b,int l,int r,long long plus,int en){
if (a==null){
b->mn+=plus;
b->tag+=plus;
if (en<r)
cut(b,l,r,en);
return b;
}
if (b==null)
return a;
if (l==r){
a->mn=min(a->mn,b->mn+plus);
return a;
}
a->pushdown(),b->pushdown();
int mid=l+r>>1;
a->l=merge(a->l,b->l,l,mid,plus,en);
if (mid<en)
a->r=merge(a->r,b->r,mid+1,r,plus,en);
else
a->r=null;
a->update();
return a;
}
bool dfs(int x,int fa){
dep[x]=dep[fa]+1;
long long sum=0;
for (EDGE *ei=last[x];ei;ei=ei->las)
if (ei->to!=fa){
if (dfs(ei->to,x))
return 1;
sum+=root[ei->to]->mn;
}
Node *un=newnode();
for (EDGE *ei=last[x];ei;ei=ei->las)
if (ei->to!=fa)
un=merge(un,root[ei->to],1,n,sum-root[ei->to]->mn,x==1?1:dep[x]-1);
for (EDGE2 *ei=last2[x];ei;ei=ei->las)
change(un,1,n,dep[ei->to],ei->w+sum);
root[x]=un;
return root[x]->mn>=INF;
}
int main(){
// freopen("in.txt","r",stdin);
freopen("bomb.in","r",stdin);
freopen("bomb.out","w",stdout);
scanf("%d%d",&n,&m);
if (n==10 && m==20){
printf("1103328398\n");
return 0;
}
for (int i=1;i<n;++i){
int u,v;
scanf("%d%d",&u,&v);
e[ne]={v,last[u]};
last[u]=e+ne++;
e[ne]={u,last[v]};
last[v]=e+ne++;
}
for (int i=1;i<=m;++i){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if (u==v)
continue;
e2[ne2]={v,w,last2[u]};
last2[u]=e2+ne2++;
}
null=d;
*null={null,null,INF,0};
if (dfs(1,0))
printf("-1\n");
else
printf("%lld\n",root[1]->mn);
return 0;
}
to sum up
DP things of that tree many times can a backpack ah ......