标签:style blog http color io os for sp div
这样加边比STL快!
不明白为什么要+mod
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<vector> 6 #include<algorithm> 7 #define INF 0x3f3f3f3f 8 #define M(a,b) memset(a,b,sizeof(a)) 9 typedef long long LL; 10 using namespace std; 11 12 const LL mod = 1000000007; 13 int N,M; 14 int dp[5060][5060]; 15 int tot[5060]; 16 17 18 struct node 19 { 20 int t,nxt; 21 }edge[5060*2]; 22 int headline[5060]; 23 int E; 24 void add(int f,int t) 25 { 26 E++; 27 edge[E].t = t; 28 edge[E].nxt = headline[f]; 29 headline[f] = E; 30 } 31 32 void dfs(int u, int fa) 33 { 34 for(int i = headline[u];i != -1;i = edge[i].nxt) 35 { 36 int t = edge[i].t;; 37 if(t!=fa) 38 { 39 dfs(t,u); 40 for(int j = 1;j<=M;j++) 41 { 42 if(tot[t]-dp[t][j]<0) 43 dp[u][j] = ((LL)dp[u][j]*(tot[t]-dp[t][j]+mod)%mod)%mod; 44 else 45 dp[u][j] = ((LL)dp[u][j]*(tot[t]-dp[t][j])%mod)%mod; 46 } 47 } 48 } 49 for(int i = 1;i<=M;i++) 50 { 51 tot[u] = (tot[u]+dp[u][i])%mod; 52 } 53 return; 54 } 55 56 int main() 57 { 58 while(scanf("%d%d",&N,&M)==2) 59 { 60 int t; 61 M(dp,0); 62 M(headline,-1); 63 M(tot,0); 64 E=0; 65 for(int i = 1;i<=N;i++) 66 { 67 scanf("%d",&t); 68 int a; 69 for(int j = 0;j<t;j++) {scanf("%d",&a);dp[i][a]=1;} 70 } 71 int u,v; 72 for(int i = 0;i<N-1;i++) 73 { 74 scanf("%d%d",&u,&v); 75 add(u,v); 76 add(v,u); 77 } 78 dfs(1,0); 79 printf("%d\n",tot[1]%mod); 80 } 81 return 0; 82 }
标签:style blog http color io os for sp div
原文地址:http://www.cnblogs.com/haohaooo/p/4035302.html