• [BJOI2017]树的难题


    Description
    给你一棵 n 个点的无根树。树上的每条边具有颜色。 一共有 m 种颜色,编号为 1 到 m。第 i 种颜色的权值为 ci。对于一条树上的简单路径,路径上经过的所有边按顺序组成一个颜色序列,序列可以划分成若干个相同颜色段。 定义路径权值为颜色序列上每个同颜色段的颜色权值之和。请你计算,经过边数在 l 到 r 之间的所有简单路径中, 路径权值的最大值。

    Input
    第一行, 四个整数 n, m, l, r。
    第二行, n 个整数 c1, c2, ……, cm,由空格隔开。依次表示每个颜色的权值。
    接下来 n-1 行,每行三个整数 u, v, c,表示点 u 和点 v 之间有一条颜色为 c 的边。
    (nleqslant 2*10^5)
    (mleqslant n)

    Output
    输出一行, 一个整数, 表示答案。

    Sample Input
    5 3 1 4
    -1 -5 -2
    1 2 1
    1 3 1
    2 4 2
    2 5 3

    Sample Output
    -1

    HINT
    颜色权值均为负,最优路径为 (1, 2) 或 (1, 3)。


    这种树上balabala的题肯定是要用点分治的,找到分治重心后,由于颜色段的存在,我们肯定是把出边颜色相同的放在一起考虑(考虑的同时显然还要按子树最深深度排序,这都是细节啦)

    然后我们维护两个桶,一个记录异色桶信息((f[i])表示能与(i)匹配为合法路径的最大值),一个记录当前同色桶信息((g[i])表示深度为(i)的最大权值)

    然后我们枚举子树,得到信息先去查询答案,然后将其扔到对应同色桶中合并信息,当我们枚举到另一种颜色的时候,将同色桶信息合并入异色桶,并且清空同色桶

    查询的时候使用单调队列,清空的时候用for循环

    时间复杂度为(O(nlog n))

    /*program from Wolfycz*/
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    typedef unsigned int ui;
    typedef unsigned long long ull;
    inline char gc(){
    	static char buf[1000000],*p1=buf,*p2=buf;
    	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
    }
    inline int frd(){
    	int x=0,f=1; char ch=gc();
    	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<3)+(x<<1)+ch-'0';
    	return x*f;
    }
    inline int read(){
    	int x=0,f=1; char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<3)+(x<<1)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x<0)	putchar('-'),x=-x;
    	if (x>9)	print(x/10);
    	putchar(x%10+'0');
    }
    const int N=2e5;
    int pre[(N<<1)+10],now[N+10],child[(N<<1)+10],val[(N<<1)+10];
    int size[N+10],VC[N+10];//value of color
    bool vis[N+10];
    int tot,root,Max,Ans;
    int n,m,L,R;
    inline void join(int x,int y,int z){pre[++tot]=now[x],now[x]=tot,child[tot]=y,val[tot]=z;}
    inline void insert(int x,int y,int z){join(x,y,z),join(y,x,z);}
    inline void Get_root(int x,int fa,int sz){
    	int res=0; size[x]=1;
    	for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
    		if (son==fa||vis[son])	continue;
    		Get_root(son,x,sz);
    		size[x]+=size[son];
    		res=max(res,size[son]);
    	}
    	res=max(res,sz-size[x]);
    	if (res<Max)	Max=res,root=x;
    }
    int Df[N+10],C_D[N+10];//Df:deepest; C_D:color of deepest
    inline void Get_Df(int x,int fa,int D){
    	Df[x]=D;
    	for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
    		if (son==fa||vis[son])	continue;
    		Get_Df(son,x,D+1);
    		Df[x]=max(Df[x],Df[son]);
    	}
    }
    int Dv[N+10];//deep value
    inline void get_Dv(int x,int fa,int v,int Line,int Deep){
    	if (Deep>R)	return;
    	Dv[Deep]=max(Dv[Deep],v);
    	for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
    		if (son==fa||vis[son])	continue;
    		get_Dv(son,x,v+(val[p]!=Line)*VC[val[p]],val[p],Deep+1);
    	}
    }
    inline void merge(int *a,int &la,int *b,int &lb){for (int i=1;i<=max(la,lb);++i)	a[i]=max(a[i],b[i]),b[i]=-inf;	la=lb;}
    int T[N+10];//T[i] means the max{f[j]} and i+j in [L,R]
    int solve(int *a,int la,int *b,int lb,int v,bool flag){
    	la=min(la,R),lb=min(lb,R);
    	static int h[N+10]; int head=1,tail=0,res=-inf;
    	for (int i=lb,j=0;i;i--){
    		while (j<=la&&j+i<=R){
    			while (head<=tail&&a[h[tail]]<=a[j])	tail--;
    			h[++tail]=j++;
    		}
    		while (head<=tail&&h[head]+i<L)	head++;
    		if (flag)	res=max(res,T[i]+b[i]);
    		if (head<=tail&&h[head]+i<=R&&h[head]+i>=L)
    			flag?res=max(res,a[h[head]]-v+b[i]):b[i]=a[h[head]];
    	}
    	return res;
    }
    struct S1{
    	int Col,Deep,x;
    	inline void insert(int _Col,int _Deep,int _x){Col=_Col,Deep=_Deep,x=_x;}
    	bool operator <(const S1 &tis)const{return Col!=tis.Col?C_D[Col]<C_D[tis.Col]:Deep<tis.Deep;}
    }A[N+10];
    int f[N+10],g[N+10];//f:different color value; g:same color value
    inline void divide(int x){
    	vis[x]=1; int son_sz=0;//num of son
    	for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
    		if (vis[son])	continue;
    		Get_Df(son,0,1),C_D[val[p]]=max(C_D[val[p]],Df[son]);
    		A[++son_sz].insert(val[p],Df[son],son);
    	}
    	sort(A+1,A+1+son_sz);
    	if (A[son_sz].Deep<<1<L)	return;
    	int sz_f=0,sz_g=0;//size of f & g
    	for (int i=1;i<=son_sz;++i){
    		if (i!=1&&A[i].Col!=A[i-1].Col){
    			merge(f,sz_f,g,sz_g),sz_g=0;
    			solve(f,sz_f,T,C_D[A[i].Col],0,false);
    		}
    		get_Dv(A[i].x,0,VC[A[i].Col],A[i].Col,1);
    		Ans=max(Ans,solve(g,sz_g,Dv,A[i].Deep,VC[A[i].Col],true));
    		merge(g,sz_g,Dv,A[i].Deep);
    	}
    	for (int i=1;i<=son_sz;++i)	C_D[A[i].Col]=0;
    	for (int i=1;i<=A[son_sz].Deep;++i){
    		f[i]=g[i]=-inf;
    		T[i]=(L<=i&&i<=R)?0:-inf;
    	}
    	for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
    		if (vis[son])	continue;
    		Max=inf,root=0;
    		Get_root(son,0,size[son]);
    		divide(root);
    	}
    }
    int main(){
    	n=frd(),m=frd(),L=frd(),R=frd();
    	for (int i=1;i<=m;++i)	VC[i]=frd();
    	for (int i=1;i<n;++i){
    		int x=frd(),y=frd(),z=frd();
    		insert(x,y,z);
    		f[i]=g[i]=T[i]=Dv[i]=-inf;
    	}
    	f[n]=g[n]=T[n]=Dv[n]=Ans=-inf;
    	Max=inf,root=0;
    	Get_root(1,0,n);
    	divide(root);
    	printf("%d
    ",Ans);
    	return 0;
    }
    
  • 相关阅读:
    Centos7安装Docker
    [LeetCode] 651. 四键键盘 ☆☆☆(动态规划)
    一行代码就能解决的算法题
    博弈问题--石头游戏(动态规划)
    [LeetCode] 322. 零钱兑换 ☆☆☆(动态规划)
    java趣题
    [LeetCode] 516. 最长回文子序列 ☆☆☆(动态规划)
    [LeetCode] 337. 打家劫舍III ☆☆☆(动态规划)
    算法基础--贪心算法
    [LeetCode] 42. 接雨水 ☆☆☆☆☆(按列、动态规划、双指针)
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/10214369.html
Copyright © 2020-2023  润新知