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
| #include <cstdio> using namespace std; typedef long long LL; const int N = 1e5 + 10, M = 2e5 + 10, mod = 1e9 + 7; int n; LL f[N]; int idx, hd[N], nxt[M], edg[M]; void dfs(int x, int fa) { f[x] = 1; for (int i = hd[x]; ~i; i = nxt[i]) if (edg[i] != fa) { dfs(edg[i], x); f[x] = f[x] * (f[edg[i]] + 1) % mod; } } void add(int a, int b) { nxt[++idx] = hd[a]; hd[a] = idx; edg[idx] = b; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) hd[i] = -1; for (int i = 1, a, b; i < n; i++) { scanf("%d%d", &a, &b); add(a, b); add(b, a); } dfs(1, 0); LL res = 0; for (int i = 1; i <= n; i++) res = (res + f[i]) % mod; printf("%lld", res); return 0; }
|