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
| #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> using namespace std; inline int read() { int f = 1, x = 0; char ch;
do{ ch = getchar(); if (ch == '-') f = -1; }while(ch < '0' || ch > '9'); do{ x = x * 10 + ch - '0'; ch = getchar(); }while(ch >= '0' && ch <= '9'); return f * x; } const int N = 2e5; const int mod = 1e9 + 7;
int n; struct Edge { int to, next; } edge[N * 2 + 1]; int start[N + 1], tot; int pow2[N + 1]; int s, t, dis[N + 1], diss[N + 1], dist[N + 1]; int minn, buc[N + 1], ans;
inline void addedge(int u, int v) { edge[++tot] = { v, start[u] }; start[u] = tot; edge[++tot] = { u, start[v] }; start[v] = tot; return; } inline int dfs(int u, int fa) { int maxx = u;
for (int i = start[u]; i; i = edge[i].next) { int v = edge[i].to; if (v != fa) { dis[v] = dis[u] + 1; int w = dfs(v, u); if (dis[w] > dis[maxx]) maxx = w; } } return maxx; } int main() { n = read(); pow2[0] = 1; for (int i = 1; i <= n; i++) pow2[i] = 2ll * pow2[i - 1] % mod; for (int i = 1; i < n; i++) addedge(read(), read()); s = dfs(1, 0); dis[s] = 0; t = dfs(s, 0); memcpy(diss, dis, 4 * (N + 1)); dis[t] = 0; dfs(t, 0); memcpy(dist, dis, 4 * (N + 1));
for (int i = 1; i <= n; i++) { minn = max(min(diss[i], dist[i]), minn); buc[max(diss[i], dist[i])]++; } for (int i = 1; i <= n; i++) buc[i] += buc[i - 1]; for (int i = diss[t], la = 0; i >= 1; i--) { int w = (pow2[n] - 2ll * pow2[buc[i - 1]] % mod + mod) % mod; if (i <= minn) w = pow2[n]; ans = (ans + 1ll * i * (w - la + mod)) % mod; la = w; }
printf("%d\n", ans); return 0; }
|