QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#245792 | #6623. Perfect Matchings | 0x4F5DA2 | WA | 0ms | 1584kb | C++14 | 3.9kb | 2023-11-10 12:37:27 | 2023-11-10 12:37:28 |
Judging History
answer
#include <cstdio>
#include <cstring>
#define LL long long
const int maxn = 2000;
const int mp = 998244353;
int n;
struct node{
int nxt;
int to;
}edge[maxn * 4 + 10];
int head[maxn * 2 + 10];
int top;
void AddEdge(int u, int v){
++top;
edge[top].nxt = head[u];
edge[top].to = v;
head[u] = top;
return ;
}
long long ans = 0;
long long f[maxn * 2 + 10][maxn + 10][2];
long long temp[maxn + 10][2];
int siz[maxn * 2 + 10];
void dfs(int x, int fa){
siz[x] = 1;
f[x][0][0] = 1;
f[x][0][1] = 0;
for(int i = head[x]; i; i = edge[i].nxt){
int y = edge[i].to;
if(y != fa){
dfs(y, x);
memset(temp, 0, sizeof(temp));
for(int i = 0; i <= siz[x] / 2; i++){
for(int j = 0; j <= siz[y] / 2; j++){
temp[i + j][0]=(temp[i + j][0] + f[x][i][0] * (f[y][j][0] + f[y][j][1]) % mp) % mp;
temp[i + j][1]=(temp[i + j][1] + f[x][i][1] * (f[y][j][0] + f[y][j][1]) % mp) % mp;
temp[i + j + 1][1]=(temp[i + j + 1][1] + f[x][i][0] * f[y][j][0] % mp) % mp;
}
}
for(int i = 0; i <= siz[x] / 2 + siz[y] / 2 + 1; i++){
f[x][i][0] = temp[i][0];
f[x][i][1] = temp[i][1];
}
siz[x] = siz[x] + siz[y];
/*for(int j = siz[x] / 2; j > 0; --j){
//k = 0
//f[x][j][0] = f[x][j][0] * (f[y][0][0] + f[y][0][1]) % mp;
//f[x][j][1] = f[x][j][1] * (f[y][0][0] + f[y][0][1]) % mp;
//if(1 <= j){
f[x][j][1] = (f[x][j][1] + f[x][j - 1][0] * f[y][0][0] % mp) % mp;
//}
//
for(int k = 1; k <= siz[y] / 2; ++k){
if(k <= j){
f[x][j][0] = (f[x][j][0] + f[x][j - k][0] * ((f[y][k][0] + f[y][k][1]) % mp) % mp) % mp;
f[x][j][1] = (f[x][j][1] + f[x][j - k][1] * ((f[y][k][0] + f[y][k][1]) % mp) % mp) % mp;
}
if(k + 1 <= j){
f[x][j][1] = (f[x][j][1] + f[x][j - k - 1][0] * f[y][k][0] % mp) % mp;
}
if(k >= j)
break ;
}
}*/
}
}
return ;
}
/*void dfs(int u,int fa) //dfs求树形dp
{
sz[u]=f[u][0][0]=1;
for(int v:h[u])
{
if(v==fa) continue;
dfs(v,u);
memset(g,0,sizeof g); //定义一个g数组作为中间过度,防止重复计算
for(int i=0;i<=sz[u]/2;i++) //树上的边数为点数/2
for(int j=0;j<=sz[v]/2;j++) //合并v子树到u
{ //三种状态转移
g[i+j][0]=(g[i+j][0]+(LL)f[u][i][0]*(f[v][j][0]+f[v][j][1])%mod)%mod;
g[i+j][1]=(g[i+j][1]+(LL)f[u][i][1]*(f[v][j][0]+f[v][j][1])%mod)%mod;
g[i+j+1][1]=(g[i+j+1][1]+(LL)f[u][i][0]*f[v][j][0]%mod)%mod;
}
for(int i=0;i<=sz[u]/2+sz[v]/2+1;i++) //将过渡数组转移回dp数组
f[u][i][0]=g[i][0],f[u][i][1]=g[i][1];
sz[u]+=sz[v];
}
}*/
long long ny[maxn * 2 + 10];
long long jc[maxn * 2 + 10];
long long jcny[maxn * 2 + 10];
long long pow_2[maxn * 2 + 10];
long long pow_2ny[maxn * 2 + 10];
long long fun(int cnt){
return (jc[cnt] * pow_2ny[cnt / 2] % mp) * jcny[cnt/2] % mp;
}
int main(){
freopen("data.txt", "r", stdin);
scanf("%d", &n);
pow_2[0] = 1;
pow_2[1] = 2;
pow_2ny[0] = 1;
pow_2ny[1] = 499122177;
jc[0] = 1;
jc[1] = 1;
jcny[0] = 1;
jcny[1] = 1;
ny[0] = 0;
ny[1] = 1;
for(int i = 2; i <= 2 * n; ++i){
//inv[i] = (long long)(p - p / i) * inv[p % i] % p;
ny[i] = (mp - mp / i) * ny[mp % i] % mp;
pow_2[i] = pow_2[i - 1] * 2 % mp;
pow_2ny[i] = pow_2ny[i - 1] * ny[2] % mp;
jc[i] = jc[i - 1] * i % mp;
jcny[i] = jcny[i - 1] * ny[i] % mp;
}
//
/*for(int i = 1; i <= 2 * n; ++i){
if(pow_2[i] >
}*/
//pow_2ny[2] = ny[2];
int u, v;
for(int i = 1; i < 2 * n; ++i){
scanf("%d%d", &u, &v);
AddEdge(u, v);
AddEdge(v, u);
}
dfs(1, 0);
/*for(int i = 0; i <= siz[1] / 2; ++i){
printf("%lld %lld\n", f[1][i][0], f[1][i][1]);
}*/
for(int i = 0; i <= n; ++i){
if(i & 1)
ans = (ans - (f[1][i][1] + f[1][i][0]) * fun(2 * n - 2 * i) % mp + mp) % mp;
else
ans = (ans + (f[1][i][1] + f[1][i][0]) * fun(2 * n - 2 * i) % mp) % mp;
}
printf("%lld", ans);
return 0;
}
/*
7
1 2
1 3
2 4
2 5
3 6
3 7
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 1412kb
input:
2 1 2 1 3 3 4
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 1584kb
input:
3 1 2 2 3 3 4 4 5 5 6
output:
1
result:
wrong answer 1st numbers differ - expected: '5', found: '1'