1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include <iostream> #include <cstdio> #include <cstring> #define ll long long
using namespace std;
const int N = 1010; const ll mod = 1e9 + 7; int T; int n; int head[N], nxt[2 * N], to[2 * N], op[2 * N], e_tot; int siz[N]; ll C[N][N]; ll f[N][N], g[N], s[N][N];
void link(int x, int y, int z) { nxt[++e_tot] = head[x], head[x] = e_tot, to[e_tot] = y, op[e_tot] = z; }
ll add(ll x, ll y) { return x + y >= mod ? x + y - mod : x + y; }
ll suf(ll x, ll y) { return x - y < 0 ? x - y + mod : x - y; }
void dfs(int u, int _fa) { siz[u] = 1, f[u][1] = 1; for (int i = head[u]; i; i = nxt[i]) { int v = to[i]; if (v == _fa) continue; dfs(v, u); memcpy(g, f[u], sizeof(f[u])); memset(f[u], 0, sizeof(f[u])); if (op[i] == 1) { for (int p1 = 1; p1 <= siz[u]; ++p1) { for (int p3 = p1; p3 < p1 + siz[v]; ++p3) { f[u][p3] = add(f[u][p3], C[p3 - 1][p1 - 1] * C[siz[u] + siz[v] - p3][siz[u] - p1] % mod * g[p1] % mod * suf(s[v][siz[v]], s[v][p3 - p1]) % mod); } } } else { for (int p1 = 1; p1 <= siz[u]; ++p1) { for (int p3 = p1 + 1; p3 <= p1 + siz[v]; ++p3) { f[u][p3] = add(f[u][p3], C[p3 - 1][p1 - 1] * C[siz[u] + siz[v] - p3][siz[u] - p1] % mod * g[p1] % mod * s[v][p3 - p1] % mod); } } } siz[u] += siz[v]; } for (int i = 1; i <= siz[u]; ++i) { s[u][i] = add(s[u][i - 1], f[u][i]); } }
int main() { scanf("%d", &T); for (int i = 0; i <= 1005; ++i) C[i][0] = 1; for (int i = 1; i <= 1005; ++i) { for (int j = 1; j <= i; ++j) { C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]); } } while (T--) { scanf("%d", &n); memset(f, 0, sizeof(f)); memset(head, 0, sizeof(head)); memset(nxt, 0, sizeof(nxt)); memset(to, 0, sizeof(to)); memset(op, 0, sizeof(op)); e_tot = 0; for (int i = 1; i < n; ++i) { int x, y; char c; scanf("%d %c %d", &x, &c, &y); ++x, ++y; link(x, y, c == '<'); link(y, x, c == '>'); } dfs(1, 0); cout << s[1][n] << endl; } return 0; }
|