1. 程式人生 > >2018.12.22【NOIP提高組】模擬B組 JZOJ 3519 靈能矩陣

2018.12.22【NOIP提高組】模擬B組 JZOJ 3519 靈能矩陣

大意

給定一棵樹,規定每個點的權值為其所有葉子節點的權值和,現在只能減少葉子節點的權值,問使所有節點的子節點權值都相同的最少總減少值

資料範圍:
對於15%的資料, 1 n 10 1\leq n\leq 10


對於30%的資料,對於邊 S i , T i S
i + 1 T i S_i, T_i,S_i + 1\neq Ti
的邊數不超過3 條。
對於50%的資料, 1
n 1000 1\leq n \leq 1000

對於100%的資料, 1 n 100000 1 < = A i < = 2 30 1 \leq n \leq 100000,1 <= A_i <= 2^{30}


思路

5%貪心
30%搜尋
100%樹形 d p dp
可以發現每次修改結束的值必然是子節點們的 l c m lcm


程式碼

#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;typedef long long LL;
int n;
vector<int>e[1000011];
LL lim[1000011],ans,a[1000011];
inline LL gcd(LL x,LL y){return y?gcd(y,x%y):x;}
inline LL lcm(LL x,LL y){return x<y?x*y/gcd(y,x):x*y/gcd(x,y);}
inline char Getchar()
{
    static char buf[100000],*p1=buf+100000,*pend=buf+100000;
    if(p1==pend)
	{
        p1=buf; pend=buf+fread(buf,1,100000,stdin);
        if (pend==p1) return -1;
    }
    return *p1++;
}
inline LL read()
{
	char c;int d=1;LL f=0;
	while(c=Getchar(),!isdigit(c))if(c==45)d=-1;f=(f<<3)+(f<<1)+c-48;
	while(c=Getchar(),isdigit(c)) f=(f<<3)+(f<<1)+c-48;
	return d*f;
}
inline void write(register LL x)
{
	if(x<0)write(45),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+48);
	return;
}
inline void dfs(register int x)
{
	lim[x]=1;
	if(a[x])return;
	int len=e[x].size();
	LL sum=0,minn=1e18;
	for(register int i=0;i<len;i++)
	{
		int y=e[x][i];
		dfs(y);
		lim[x]=lcm(lim[x],lim[y]);
		minn=min(minn,a[y]);
		sum+=a[y];
	}
	lim[x]*=len;
	a[x]=minn*len-minn*len%lim[x];
	ans+=sum-a[x];
	return;
}
signed main()
{
	freopen("pylon.in","r",stdin);
	freopen("pylon.out","w",stdout);
	n=read();
	for(register int i=1;i<=n;i++) a[i]=read();
	for(register int i=1,x,y;i<n;i++)
	{
		x=read();y=read();
		e[x].push_back(y);
	}
	dfs(1);
	write(ans);
}