Submission #1365767
Source Code Expand
#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define fill(x, y) memset(x, y, sizeof x)
#define copy(x, y) memcpy(x, y, sizeof x)
using namespace std;
typedef long long LL;
typedef pair < int, int > pa;
inline int read()
{
int sc = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') sc = sc * 10 + ch - '0', ch = getchar();
return sc * f;
}
const int MAXN = 1005;
const int mod = 1e9 + 7;
int n, dep[MAXN], fa[MAXN], S, T, dia, cg, cg2, f[MAXN][MAXN], ans;
vector < int > G[MAXN];
inline void inc(int &x, int y) { x += y; if (x >= mod) x -= mod; }
inline void dec(int &x, int y) { x -= y; if (x < 0) x += mod; }
inline void dfs(int x, int d)
{
dep[x] = d;
for (auto y : G[x]) if (y ^ fa[x]) fa[y] = x, dfs(y, d + 1);
}
inline void find_dia()
{
dfs(1, 0);
fill(fa, 0);
for (int i = 1; i <= n; i ++) if (dep[i] > dep[S]) S = i;
dfs(S, 0);
for (int i = 1; i <= n; i ++) if (dep[i] > dep[T]) T = i;
dia = dep[cg = T];
for (int i = 1; i <= dep[T] >> 1; i ++) cg = fa[cg];
if (dep[T] & 1) cg2 = fa[cg];
}
inline void dp(int x, int fa, int dep, int len)
{
for (auto y : G[x]) if (y ^ fa) dp(y, x, dep + 1, len);
for (int i = dep - len; i <= len - dep; i ++)
{
f[x][i + 502] = 1;
for (auto y : G[x]) if (y ^ fa) f[x][i + 502] = 1LL * f[x][i + 502] * (f[y][i + 1 + 502] + f[y][i - 1 + 502]) % mod;
}
}
int main()
{
#ifdef wxh010910
freopen("data.in", "r", stdin);
#endif
n = read();
for (int i = 1, x, y; i < n; i ++) x = read(), y = read(), G[x].pb(y), G[y].pb(x);
find_dia();
if (dia & 1)
{
dp(cg, cg2, 0, dia >> 1); dp(cg2, cg, 0, dia + 1 >> 1);
for (int i = - (dia >> 1); i <= (dia >> 1); i ++) inc(ans, 1LL * f[cg][i + 502] * (f[cg2][i + 1 + 502] + f[cg2][i - 1 + 502]) % mod);
fill(f, 0);
dp(cg, cg2, 0, dia + 1 >> 1); dp(cg2, cg, 0, dia >> 1);
for (int i = - (dia >> 1); i <= (dia >> 1); i ++) inc(ans, 1LL * f[cg2][i + 502] * (f[cg][i + 1 + 502] + f[cg][i - 1 + 502]) % mod);
fill(f, 0);
dp(cg, cg2, 0, dia >> 1); dp(cg2, cg, 0, dia >> 1);
for (int i = - (dia >> 1); i <= (dia >> 1); i ++) dec(ans, 2LL * f[cg][i + 502] * f[cg2][i + 502] % mod);
for (int i = - (dia >> 1); i <= (dia >> 1); i ++) dec(ans, 1LL * f[cg][i + 502] * f[cg2][i + 2 + 502] % mod);
for (int i = - (dia >> 1); i <= (dia >> 1); i ++) dec(ans, 1LL * f[cg][i + 502] * f[cg2][i - 2 + 502] % mod);
return printf("%d\n", ans), 0;
}
else
{
dp(cg, 0, 0, dia >> 1);
for (int i = - (dia >> 1); i <= (dia >> 1); i ++) inc(ans, f[cg][i + 502]);
return printf("%d\n", ans), 0;
}
}
Submission Info
Submission Time |
|
Task |
D - Oriented Tree |
User |
wxh010910 |
Language |
C++14 (GCC 5.4.1) |
Score |
1800 |
Code Size |
2734 Byte |
Status |
AC |
Exec Time |
11 ms |
Memory |
4224 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
1800 / 1800 |
Status |
|
|
Set Name |
Test Cases |
Sample |
0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt |
All |
0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt |
Case Name |
Status |
Exec Time |
Memory |
0_00.txt |
AC |
1 ms |
256 KB |
0_01.txt |
AC |
3 ms |
4224 KB |
0_02.txt |
AC |
3 ms |
4224 KB |
0_03.txt |
AC |
1 ms |
256 KB |
1_00.txt |
AC |
3 ms |
4224 KB |
1_01.txt |
AC |
6 ms |
4224 KB |
1_02.txt |
AC |
11 ms |
4224 KB |
1_03.txt |
AC |
3 ms |
4224 KB |
1_04.txt |
AC |
4 ms |
4224 KB |
1_05.txt |
AC |
4 ms |
4224 KB |
1_06.txt |
AC |
4 ms |
4224 KB |
1_07.txt |
AC |
4 ms |
4224 KB |
1_08.txt |
AC |
4 ms |
4224 KB |
1_09.txt |
AC |
5 ms |
4224 KB |
1_10.txt |
AC |
4 ms |
4224 KB |
1_11.txt |
AC |
5 ms |
4224 KB |
1_12.txt |
AC |
4 ms |
4096 KB |
1_13.txt |
AC |
7 ms |
4224 KB |
1_14.txt |
AC |
5 ms |
4224 KB |
1_15.txt |
AC |
5 ms |
4224 KB |
1_16.txt |
AC |
6 ms |
4224 KB |
1_17.txt |
AC |
3 ms |
4224 KB |
1_18.txt |
AC |
3 ms |
4224 KB |
1_19.txt |
AC |
3 ms |
4224 KB |
1_20.txt |
AC |
4 ms |
4224 KB |
1_21.txt |
AC |
3 ms |
4224 KB |
1_22.txt |
AC |
3 ms |
4224 KB |
1_23.txt |
AC |
3 ms |
4224 KB |
1_24.txt |
AC |
3 ms |
4224 KB |
1_25.txt |
AC |
3 ms |
4224 KB |
1_26.txt |
AC |
3 ms |
4096 KB |
1_27.txt |
AC |
3 ms |
4224 KB |
1_28.txt |
AC |
3 ms |
4224 KB |