[HNOI2014]米特运输

题面在这里

description

一个(n)个节点的树,修改一些节点的权值(可以为小数但不能为负),
使得这棵树中,父节点的权值等于字节点权值之和,且父节点相同的节点的权值相等
求修改节点数的最小值

data range

[n≤500000, v_i≤10^8 ]

solution

如果我们确定了一个节点的值,那么我们就可以得到这棵树所有节点的权值
因为树中节点权值都存在倍数关系

于是我们把根节点的取值作为状态,最后取与根节点取值相符的点数最多的一类即可
但是这个根节点的取值可能会爆(long long)...
于是(Hash)一下就可以了
直接模zsy(998244353)

code

#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<complex>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
//#define TEST
#define FILE "a"
#define pb push_back
#define RG register
#define il inline
using namespace std;
typedef unsigned long long ull;
typedef vector<int>VI;
typedef long long ll;
typedef double dd;
const int inf=1e9+7;
const int rev2=332748118;
const int mod1=1e9+7;
const int zsy=998244353;
const int N=1000001;
const int K=1000010;
const dd eps=1e-10;
const ll INF=1e18;
il ll read(){
  RG ll data=0,w=1;RG char ch=getchar();
  while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
  if(ch=='-')w=-1,ch=getchar();
  while(ch<='9'&&ch>='0')data=data*10+ch-48,ch=getchar();
  return data*w;
}

il void file(){
  freopen(FILE".in","r",stdin);
  freopen(FILE".out","w",stdout);
}

map<ull,int>M;
map<ull,int>::iterator tmp;
int n,a[N],ans;
int head[N],nxt[N<<1],to[N<<1],cnt;
il void add(int u,int v){
	to[++cnt]=v;
	nxt[cnt]=head[u];
	head[u]=cnt;
}

int son[N];
void dfs(int u,int fa,ull data){
	M[1ll*data*a[u]%zsy]++;
	for(RG int i=head[u];i;i=nxt[i])if(to[i]!=fa)son[u]++;
	for(RG int i=head[u];i;i=nxt[i])if(to[i]!=fa)dfs(to[i],u,data*son[u]%zsy);
}

int main()
{
	n=read();
	for(RG int i=1;i<=n;i++)a[i]=read();
	for(RG int i=1,u,v;i<n;i++){
		u=read();v=read();add(u,v);add(v,u);
	}
	
	dfs(1,0,1);
	
	for(tmp=M.begin();tmp!=M.end();tmp++)
		ans=max(ans,tmp->second);
	printf("%d
",n-ans);
	return 0;
}

原文地址:https://www.cnblogs.com/cjfdf/p/9101724.html