NOIP 2012 提高组第一试模拟赛 魔法树 Solution

题意

Solution

压位+前缀和

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cmath>
 4 #include <algorithm>
 5 #define ll int
 6 using namespace std;
 7 const ll mod=100000007;
 8 inline void read(ll &k)
 9 {
10     ll f=1;char c=getchar();k=0;
11     while (c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
12     while (c>='0'&&c<='9')k=k*10+c-'0',c=getchar();
13     k*=f;
14 }
15 const int maxn=1000010;
16 ll n,q,k,aa,bb,cc;
17 ll last[maxn],next[maxn],tot,c[maxn],to[maxn],sum[maxn];long long cost[233];
18 bool v[maxn];
19 void dfs(ll now,ll fa)
20 {
21     v[now]=1;ll cur=last[now];
22     while (cur)
23     {
24         if (!v[to[cur]])
25         {
26             sum[to[cur]]=sum[now]^(1<<(c[cur]-1));
27             dfs(to[cur],now);
28         }
29         cur=next[cur];
30     }    
31 }
32 int main()
33 {
34     freopen("mahou.in","r",stdin);
35     freopen("mahou.out","w",stdout);
36     read(n);read(q);read(k);
37 //    printf("..............qaq%lld%lld%lld\n",n,q,k);
38     for (int i=1;i<n;i++)
39     {
40         read(aa);read(bb);read(cc);
41         to[++tot]=bb;
42         next[tot]=last[aa];
43         last[aa]=tot;
44         c[tot]=cc;
45         to[++tot]=aa;
46         next[tot]=last[bb];
47         last[bb]=tot;
48         c[tot]=cc;
49     }
50     dfs(1,0);
51     for (int i=1;i<=k;i++)scanf("%lld",&cost[i]);
52     ll pp,qq;
53     for (int i=1;i<=q;i++)
54     {
55         long long ans=1;
56         read(pp);read(qq);
57         ll cur=sum[pp]^sum[qq];
58         for (int j=1;j<=k;j++)
59         if (cur&(1<<(j-1)))ans=(long long)((long long)ans*cost[j])%mod;
60         printf("%lld\n",ans%mod);
61     }/*
62     for (int i=1;i<=n;i++){printf("%d ",i);
63     for (int j=1;j<=k;j++)
64     printf("%lld ",sum[i][j]);
65     printf("\n");
66     }*/
67 }
View Code

 

 

posted @ 2017-09-11 19:23  Michael_Zhuang  阅读(119)  评论(0编辑  收藏  举报