【bzoj3573】 Hnoi2014—米特运输
http://www.lydsy.com/JudgeOnline/problem.php?id=3573 (题目链接)
题意
题意是这道题最大的难点→_→
Solution
沙茶树形dp,考虑一定会存在一个节点的权值没有改变,枚举这个点,然后算算根节点的权值要变成多少,对于两个不同的节点,如果它们所对应的根节点的权值相同,那么这两个节点可以不改。可能根节点的权值会很大,我们取对数,hash统计答案。
代码
// bzoj3573 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #define LL long long #define HAS 1000007 #define inf (1ll<<60) #define eps 1e-7 #define Pi acos(-1.0) #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout); using namespace std; const int maxn=500010; LL r[maxn],a[maxn]; int head[maxn],h[HAS],n,ans,cnt; struct edge {int to,next;}e[maxn]; struct hash {double w;int cnt,next;}has[maxn]; bool equal(double a,double b) { return fabs(a-b)<eps; } int push(LL id,double w) { for (int i=h[id];i;i=has[i].next) if (equal(has[i].w,w)) return ++has[i].cnt; has[++cnt]=(hash){w,1,h[id]};h[id]=cnt; return 1; } void link(int u,int v) { e[++cnt]=(edge){v,head[u]};head[u]=cnt; } void dfs(int x,double w,LL c) { ans=max(ans,push(c*a[x]%HAS,w+log(a[x]))); for (int i=head[x];i;i=e[i].next) dfs(e[i].to,w+log(r[x]),c*r[x]%HAS); } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%lld",&a[i]); for (int u,v,i=1;i<n;i++) { scanf("%d%d",&u,&v); link(u,v);r[u]++; } cnt=0;dfs(1,0,1); printf("%d",n-ans); return 0; }
This passage is made by MashiroSky.