【NOIP2014八校联考第1场第1试9.20】统计损失(count)(树形dp)

来源:互联网 发布:阿里云域名解析时间 编辑:程序博客网 时间:2024/06/05 14:22

题意:

一棵树,找出所有路径的乘积的和。

思路:

把最小面的树改成一个点,为存放到哪里的答案,然后再用一个记录他的乘积。
ans:=ans+(f[u]-a[u]+oo)*f[v];
f[u]:=f[u]+f[v]*a[u];
当最后做完时ans加上那个点的值

程序:

 const oo=10086;maxn=200010;var        n,i,u,v,tot,ans:longint;        a,vet,next,head,f:array[0..maxn] of longint;procedure add(u,v:longint);begin         inc(tot);         vet[tot]:=v;         next[tot]:=head[u];         head[u]:=tot;         inc(tot);         vet[tot]:=u;         next[tot]:=head[v];         head[v]:=tot;end;procedure dfs(u,pre:longint);var        e,v:longint;begin        e:=head[u];        while e<>0 do        begin                v:=vet[e];                if v<>pre then                          begin                                dfs(v,u);                                ans:=(ans+(f[u]-a[u]+oo)*f[v]) mod oo;                                f[u]:=(f[u]+f[v]*a[u]) mod oo;                          end;                e:=next[e];        end;        ans:=(ans+f[u]) mod oo;end;begin        read(n);        for i:=1 to n do        begin                read(a[i]);                f[i]:=a[i];        end;        for i:=1 to n-1 do        begin                read(u,v);                add(u,v);        end;        dfs(1,0);        write(ans);end.
0 0
原创粉丝点击