voiddfs(int x, int fa){ for (int i = head[x]; i; i = edge[i].nxt) { int y = edge[i].to; if (y == fa) continue; dfs(y, x); } for (int c : col[x]) { for (int i = head[x]; i; i = edge[i].nxt) { int y = edge[i].to; if (y == fa) continue; int sum = 0; for (int j : col[y]) if (j ^ c) Add(sum, f[y][j]); f[x][c] = 1LL * f[x][c] * sum % P; } } }
#include<cstdio> #include<iostream> usingnamespace std; char buf[1 << 14], *p1 = buf, *p2 = buf; inlinechargc(){ return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 14, stdin), p1 == p2) ? EOF : *p1++; } inlineintread(){ int x = 0, f = 1; char c = gc(); for (; !isdigit(c); c = gc()) if (c == '-') f = -1; for (; isdigit(c); c = gc()) x = x * 10 + c - '0'; return x * f; } constint N = 5005; constint P = 1e9 + 7; int nedge, head[N]; structEdge { int to, nxt; } edge[N << 1]; inlinevoidadd(int x, int y){ edge[++nedge].to = y; edge[nedge].nxt = head[x]; head[x] = nedge; } inlinevoidAdd(int &x, int y){ x = x + y >= P ? x + y - P : x + y; } inlineintDec(int x, int y){ x -= y; if (x < 0) x += P; return x; } int n, m, f[N][N]; voiddfs(int x, int fa){ for (int i = head[x]; i; i = edge[i].nxt) { int y = edge[i].to; if (y == fa) continue; dfs(y, x); int sum = 0; for (int c = 1; c <= m; c++) if (f[y][c]) Add(sum, f[y][c]); for (int c = 1; c <= m; c++) if (f[x][c]) f[x][c] = 1LL * f[x][c] * Dec(sum, f[y][c]) % P; } } intmain(){ n = read(), m = read(); for (int i = 1; i <= n; i++) for (int num = read(); num; num--) f[i][read()] = 1; for (int i = 1; i < n; i++) { int x = read(), y = read(); add(x, y), add(y, x); } dfs(1, 0); int ans = 0; for (int c = 1; c <= m; c++) Add(ans, f[1][c]); printf("%d", ans); }