HDU 5293(Tree chain problem-树链剖分)

Tree chain problem

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 940    Accepted Submission(s): 248


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
 

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
 

Output
For each tests:
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
 
   
6
Hint
Stack expansion program: #pragma comment(linker, "/STACK:1024000000,1024000000")
 

Author
FZUACM
 

Source
2015 Multi-University Training Contest 1
 

Recommend
We have carefully selected several similar problems for you:   5421  5420  5419  5418  5417 
 


考虑dp,dp[i]表示以i为根的子树的最优值,则
sum[i]=json[i]  d p[j]
容易想到有两种转移

  • (1) dp[i]=sum[i]
  • (2) dp[i]=value[p]+sum[k]dp[k] (  plcai,k)
    链上求和很容易想到树链剖分,复杂度O(Nlog2N)




#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
#define MAXM (200000+10)
#define MAXV (1000+10)
#define pb push_back
#define mp make_pair
#pragma comment(linker, "/STACK:1024000000,1024000000") 
typedef int ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
struct Chain{
	int u,v,w;
	Chain(){}
	Chain(int _u,int _v,int _w):u(_u),v(_v),w(_w){}
};
vector a[MAXN];
int n,m;
int edge[MAXM],Next[MAXM],Pre[MAXN],siz=1;
void addedge(int u,int v)
{
	edge[++siz]=v;
	Next[siz]=Pre[u];
	Pre[u]=siz;
}
void addedge2(int u,int v){addedge(u,v);addedge(v,u);}

bool vis[MAXN]; 
int cnt,id[MAXN];
int son[MAXN],dep[MAXN],sz[MAXN],top[MAXN],pre[MAXN],q[MAXN];
void build()
{
	MEM(vis) cnt=0; MEM(id)
	MEM(son) MEM(dep) MEM(sz) MEM(top) MEM(pre) MEM(q) 
	int r=1;
	vis[dep[1]=q[1]=1]=1;
	For(i,r)
	{
		int u=q[i];
		Forp(u)
		{
			int v=edge[p];
			if (vis[v]) continue; else vis[v]=1;
			dep[ q[++r]=v ]=dep[u]+1;
			pre[v]=u;
		}
	}
	ForD(i,r) {
		sz[pre[q[i]]] += ++sz[q[i]];
		if (sz[son[pre[q[i]]]]=0) sumv[o]=setv[o]*(R-L+1),minv[o]=maxv[o]=setv[o];
		
		minv[o]+=addv[o];maxv[o]+=addv[o];sumv[o]+=addv[o]*(R-L+1);
    }

	int y1,y2,v;
	void update(int o,int L,int R) //y1,y2,v
	{
		if (y1<=L&&R<=y2) {
			addv[o]+=v;
		}
		else{
			pushdown(o);
			int M=(R+L)>>1;
			if (y1<=M) update(Lson,L,M); else maintain(Lson,L,M); 
			if (M< y2) update(Rson,M+1,R); else maintain(Rson,M+1,R);
		}
		
		maintain(o,L,R); 
		
	}
	void update2(int o,int L,int R) 
	{
		if (y1<=L&&R<=y2) {
			setv[o]=v;addv[o]=0;
		}
		else{
			pushdown(o);
			int M=(R+L)>>1;
			if (y1<=M) update2(Lson,L,M); else maintain(Lson,L,M); //维护pushodown,再次maintain 
			if (M< y2) update2(Rson,M+1,R); else maintain(Rson,M+1,R);
		}
		
		maintain(o,L,R); 
	}
	
	void pushdown(int o) 
	{
		if (setv[o]>=0)
		{
			setv[Lson]=setv[Rson]=setv[o]; 
			addv[Lson]=addv[Rson]=0;
			setv[o]=-1;
		}
		if (addv[o])
		{
			addv[Lson]+=addv[o];
			addv[Rson]+=addv[o];
			addv[o]=0;
		} 
	}
	ll _min,_max,_sum; 
	
	void query2(int o,int L,int R,ll add)
	{
		if (setv[o]>=0)
		{
			_sum+=(setv[o]+addv[o]+add)*(min(R,y2)-max(L,y1)+1);
			_min=min(_min,setv[o]+addv[o]+add);
			_max=max(_max,setv[o]+addv[o]+add); 
		} else if (y1<=L&&R<=y2)
		{
			_sum+=sumv[o]+add*(R-L+1);
			_min=min(_min,minv[o]+add);
			_max=max(_max,maxv[o]+add); 
		} else {
		//	pushdown(o);
			int M=(L+R)>>1;
			if (y1<=M) query2(Lson,L,M,add+addv[o]);// else maintain(Lson,L,M); 
			if (M< y2) query2(Rson,M+1,R,add+addv[o]);// else maintain(Rson,M+1,R);
		}
		//maintain(o,L,R);
	}
	
	void query(int o,int L,int R,ll add) //y1,y2
	{
		if (y1<=L&&R<=y2)
		{
			_sum+=sumv[o]+add*(R-L+1);
			_min=min(_min,minv[o]+add);
			_max=max(_max,maxv[o]+add); 
		} 
		else{
			int M=(R+L)>>1;
			if (y1<=M) query(Lson,L,M,add+addv[o]);
			if (M< y2) query(Rson,M+1,R,add+addv[o]);
		}		
	}

	void add(int l,int r,ll v)
	{
		if (l>r) swap(l,r);
		y1=l,y2=r;this->v=v;
		update(1,1,n);
	}
	void set(int l,int r,ll v)
	{
		y1=l,y2=r;this->v=v;
		update2(1,1,n);
	}
	ll ask(int l,int r,int b=1)
	{
		if (l>r) swap(l,r);
		_sum=0,_min=INF,_max=-1;
		y1=l,y2=r;
		query2(1,1,n,0);
		switch(b)
		{
			case 1:return _sum;
			case 2:return _min;
			case 3:return _max;
			default:break;
		}		
	}
	void print()
	{
		For(i,n)
			cout<dep[b]) swap(a,b);
	ans+=S[f].ask(id[a],id[b],1);
	return ans;
}


void dfs(int u,int fa)
{
	Forp(u)
	{
		int v=edge[p];
		if (v==fa) continue;
		dfs(v,u);
		s[u]+=d[v];
	}
	d[u]=s[u];
	S[0].add(id[u],id[u],s[u]);  
	int tot=a[u].size();
	Rep(j,tot)
	{
		Chain t = a[u][j];
		int fee=t.w;
		//
		d[u]=max(d[u],(int)(fee+Ask(t.u,t.v,0)-Ask(t.u,t.v,1)));
	} 
	S[1].add(id[u],id[u],d[u]);  
	
}

int main()
{
//	freopen("hdu5293.in","r",stdin);
	
	int T;cin>>T;
	while(T--) {
		MEM(edge) MEM(Next) MEM(Pre) siz=1;
		MEM(d) MEM(s)
		For(i,n) a[i].clear();
		
		cin>>n>>m;
		S[0].mem(n);S[1].mem(n);
		For(i,n-1)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			addedge2(u,v);
		}
		build();

		For(i,m) { 
			int u,v ,w;
			scanf("%d%d%d",&u,&v,&w);
			a[lca(u,v)].pb(Chain(u,v,w));
		}
		dfs(1,0);
		printf("%d\n",d[1]);
	}	
	
	return 0;
}


你可能感兴趣的:(树链剖分)