QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#447566#7677. Easy Diameter Problemucup-team3564WA 3ms6936kbC++204.4kb2024-06-18 16:33:482024-06-18 16:33:48

Judging History

你现在查看的是最新测评结果

  • [2024-06-18 16:33:48]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:6936kb
  • [2024-06-18 16:33:48]
  • 提交

answer

// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "\033[32m[" << __LINE__ << "]\033[0m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T& x, T y) {return x = max(x, y);}
template <typename T> T& chkmin(T& x, T y) {return x = min(x, y);}
template <typename T> T& read(T &x) {
	x = 0; int f = 1; char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = - f;
	for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	return x *= f;
}
const int N = 5010, MOD = 1e9 + 7;
inline int& add(int &x, ll y) {return x = (x + y) % MOD;}
int n, x[N], y[N], cnt, d[N][N], mx, c, fa[N], e[N][N], f[2][N][N], ans, C[N][N], g[N][N];
vector <int> v[N];
int fac[N], ifac[N], inv[N];
inline void binom(int x) {
	fac[0] = ifac[0] = inv[1] = 1;
	F(i, 2, x) inv[i] = (ll) (MOD - MOD / i) * inv[MOD % i] % MOD;
	F(i, 1, x) fac[i] = (ll) fac[i - 1] * i % MOD, ifac[i] = (ll) ifac[i - 1] * inv[i] % MOD;
	F(i, 0, x) {
		C[i][0] = 1;
		F(j, 1, i) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
	}
}
// inline int C(int x, int y) {return x < y || y < 0 ? 0 : (ll) fac[x] * ifac[y] % MOD * ifac[x - y] % MOD;}
void dfs(int id, int x, int fa, int dep) {
	// debug << x << " " << id << " " << dep << endl;
	::fa[x] = fa;
	if (dep + 1 > mx) {
		mx = dep + 1;
		int w = x;
		F(i, 1, mx >> 1) w = ::fa[w];
		if (mx & 1) c = e[w][::fa[w]];
		else c = w;
	}
	d[id][dep]++;
	for (int i: v[x])
		if (i != fa) dfs(id, i, x, dep + 1);
}
signed main() {
	binom(read(n));
	F(i, 1, n - 1) {
		y[i << 1 | 1] = read(x[i << 1]), x[i << 1 | 1] = read(y[i << 1]);
		e[x[i << 1]][y[i << 1]] = i << 1;
		e[y[i << 1]][x[i << 1]] = i << 1 ^ 1;
		v[x[i << 1]].push_back(y[i << 1]);
		v[y[i << 1]].push_back(x[i << 1]);
	}
	F(i, 2, 2 * n - 1) dfs(i, y[i], x[i], 0);
	// debug << mx << " " << c << endl;
	if (mx & 1) {
		int a = d[c][mx >> 1], b = d[c ^ 1][mx >> 1];
		F(i, 0, a - 1) f[mx & 1 ^ 1][c][i] = (ll) fac[b] * C[i + b - 1][i] % MOD;
		F(i, 0, b - 1) f[mx & 1 ^ 1][c ^ 1][i] = (ll) fac[a] * C[i + a - 1][i] % MOD;
	} else {
		for (int i: v[c]) {
			int a = d[e[c][i]][(mx >> 1) - 1], b = d[e[c][i] ^ 1][mx >> 1];
			// debug << a << " " << b << endl;
			// debug << e[c][i] << endl;
			F(j, 0, a - 1) f[mx & 1 ^ 1][e[c][i]][j] = (ll) fac[b] * C[j + b - 1][j] % MOD;
		}
	}
	DF(i, mx - 1, 1) {
		int cur = i & 1, to = cur ^ 1;
		if (i & 1) {
			F(j, 2, n * 2 - 1) {
				int a = d[j][i >> 1], b = d[j ^ 1][i >> 1];
				if (!a || !b) continue;
				F(k, 0, a - 1) {
					g[k][0] = ((k ? g[k - 1][0] : 0) + f[cur][j][k]) % MOD;
					// if (f[cur][j][k]) debug << i << ' ' << j << " " << k << " " << f[cur][j][k] << endl;
					f[cur][j][k] = 0;
					F(l, 1, b - 1) g[k][l] = (g[k][l - 1] + (k ? g[k - 1][l] : 0)) % MOD;
					add(f[to][j][k], (ll) g[k][b - 1] * fac[b]);
					// F(l, 0, b - 1) add(f[to][j ^ 1][l], (ll) f[cur][j][k] * C[a - k - 1 + l][l] % MOD * fac[b]);
				}
				F(k, 0, b - 1) add(f[to][j ^ 1][k], (ll) g[a - 1][k] * fac[a]);
			}
		} else {
			F(j, 2, n * 2 - 1) {
				int a = d[j][i >> 1], b = d[j ^ 1][(i >> 1) - 1];
				if (!a || !b) continue;
				// debug << i << " " << j << " " << a << " " << b << endl;
				F(k, 0, a - 1) {
					// if (f[cur][j][k] != 1 || k) f[cur][j][k] = 0;
					g[k][0] = ((k ? g[k - 1][0] : 0) + f[cur][j][k]) % MOD;
					// if (f[cur][j][k]) debug << i << ' ' << j << " " << k << " " << f[cur][j][k] << endl;
					f[cur][j][k] = 0;
					F(l, 1, b) g[k][l] = (g[k][l - 1] + (k ? g[k - 1][l] : 0)) % MOD;
				}
				// debug << i << " " << j << endl;
				F(k, 0, b - 1) add(f[to][j ^ 1][k], (ll) g[a - 1][k] * fac[a]);//, debug << "! " << k << " " << g[a - 1][k] << " " << fac[a] << endl;
				// continue;
				// debug << y[j] << endl;
				for (int k: v[y[j]]) {
					int c = d[e[y[j]][k]][(i >> 1) - 1];
					if (k != x[j] && c) {
						F(l, 0, c - 1) {
							add(f[to][e[y[j]][k]][l], (ll) g[a - c + l][b - 1] * C[a - c + l][l] % MOD * fac[b] % MOD * fac[a - c]);
							if (a - c + l) add(f[to][e[y[j]][k]][l], (ll) g[a - c + l - 1][b] * C[a - c + l - 1][l] * fac[b] % MOD * fac[a - c]);
						}
					}
				}
			}
		}
	}
	F(i, 2, n * 2 - 1) add(ans, f[0][i][0]);
	cout << ans;
	return 0;
}
/* why?
*/

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3760kb

input:

3
1 2
3 2

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3744kb

input:

5
4 1
4 5
1 2
1 3

output:

28

result:

ok 1 number(s): "28"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3944kb

input:

7
5 7
2 5
2 1
1 6
3 6
4 1

output:

116

result:

ok 1 number(s): "116"

Test #4:

score: -100
Wrong Answer
time: 3ms
memory: 6936kb

input:

100
89 60
66 37
59 74
63 49
69 37
9 44
94 22
70 30
79 14
25 33
23 4
78 43
63 30
95 7
6 59
56 31
21 56
58 43
95 28
81 19
63 40
58 87
81 38
100 55
78 23
37 78
86 70
33 11
52 17
42 19
19 13
70 100
97 79
66 67
66 27
82 55
15 27
68 33
97 26
46 20
70 93
1 10
69 79
81 45
58 95
24 63
79 51
85 11
12 43
41 64...

output:

476393277

result:

wrong answer 1st numbers differ - expected: '748786623', found: '476393277'