QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#578991 | #6623. Perfect Matchings | wdw | WA | 48ms | 33688kb | C++20 | 2.5kb | 2024-09-21 00:32:47 | 2024-09-21 00:32:47 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define ll long long
#define db double
const int mod = 998244353;
const int N =4e3+ 10;
const double eps = 1e-10;
inline void read(int &a) {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
a = s * w;
}
void qw(int out) {
if (out > 9)qw(out / 10);
putchar(out % 10 | 48);
}
vector<vector<int>>q;
int dp[N][N/2][2];
int dp2[N/2][2];
int siz[N];
void dfs(int u,int fa){
dp[u][0][0]=1;
siz[u]=1;
for(int i=0;i<q[u].size();i++){
int y=q[u][i];
if(y==fa)continue;
dfs(y,u);
siz[u]+=siz[y];
}
}
void dfs2(int u,int fa){
for(int i=0;i<q[u].size();i++){
int y=q[u][i];
if(y==fa)continue;
dfs2(y,u);
memset(dp2,0,sizeof dp2);
for(int j=0;j<=siz[u]/2;j++){
for(int k=0;k<=siz[y]/2;k++){
dp2[j+k][0]+=((dp[y][k][0]+dp[y][k][1])%mod*dp[u][j][0])%mod;
dp2[j+k][0]%=mod;
dp2[j+k][1]+=((dp[y][k][0]+dp[y][k][1])%mod*dp[u][j][1])%mod;
dp2[j+k][1]%=mod;
dp2[j+k+1][1]+=((dp[y][k][0])%mod*dp[u][j][0])%mod;
dp2[j+k+1][1]%=mod;
}
}
for(int j=0;j<=siz[u]/2+siz[y]/2+1;j++){
dp[u][j][0]=dp2[j][0];
dp[u][j][1]=dp2[j][1];
}
}
}
void solve() {
int n;
cin>>n;
q=vector<vector<int>>(2*n+5);
for(int i=1,a,b;i<=2*n-1;i++){
cin>>a>>b;
q[a].push_back(b);
q[b].push_back(a);
}
dfs(1,0);
dfs2(1,0);
vector<int>fac(2*n+5);
fac[0]=1;
fac[2]=1;
for(int i=4;i<=2*n;i+=2){
fac[i]=(fac[i-2]*(i-1))%mod;
}
int ans=fac[2*n];
for(int i=1;i<=n;i++){
if(i%2==1){
ans-=((dp[1][i][0]+dp[1][i][1])%mod*fac[2*n-2*i])%mod;
ans%=mod;
ans+=mod;
ans%=mod;
}else{
ans+=((dp[1][i][0]+dp[1][i][1])%mod*fac[2*n-2*i])%mod;
ans%=mod;
ans+=mod;
ans%=mod;
}
}
cout<<ans<<'\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
// cout<<fixed<<setprecision(10);
// cin >> T;
while (T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3780kb
input:
2 1 2 1 3 3 4
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3792kb
input:
3 1 2 2 3 3 4 4 5 5 6
output:
5
result:
ok 1 number(s): "5"
Test #3:
score: 0
Accepted
time: 1ms
memory: 5876kb
input:
10 2 1 3 2 4 2 5 3 6 3 7 5 8 4 9 3 10 5 11 3 12 9 13 11 14 8 15 5 16 1 17 4 18 1 19 11 20 19
output:
223263378
result:
ok 1 number(s): "223263378"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3848kb
input:
10 2 1 3 1 4 1 5 1 6 5 7 3 8 7 9 3 10 2 11 3 12 5 13 12 14 10 15 11 16 10 17 4 18 14 19 4 20 4
output:
225215244
result:
ok 1 number(s): "225215244"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3920kb
input:
10 2 1 3 1 4 3 5 3 6 5 7 3 8 5 9 3 10 8 11 2 12 1 13 11 14 2 15 3 16 3 17 2 18 11 19 10 20 3
output:
210104685
result:
ok 1 number(s): "210104685"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3580kb
input:
10 2 1 3 2 4 3 5 1 6 2 7 5 8 2 9 3 10 2 11 10 12 7 13 12 14 2 15 2 16 15 17 2 18 6 19 15 20 8
output:
211263144
result:
ok 1 number(s): "211263144"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3724kb
input:
10 2 1 3 2 4 3 5 2 6 2 7 1 8 7 9 3 10 8 11 5 12 6 13 11 14 8 15 1 16 13 17 2 18 14 19 11 20 12
output:
226024809
result:
ok 1 number(s): "226024809"
Test #8:
score: -100
Wrong Answer
time: 48ms
memory: 33688kb
input:
1977 2 1 3 1 4 1 5 4 6 4 7 1 8 3 9 5 10 2 11 3 12 2 13 3 14 2 15 9 16 9 17 2 18 17 19 5 20 16 21 2 22 2 23 15 24 16 25 22 26 14 27 6 28 4 29 24 30 25 31 28 32 15 33 27 34 32 35 24 36 10 37 18 38 15 39 33 40 3 41 27 42 3 43 35 44 15 45 11 46 19 47 21 48 4 49 28 50 6 51 3 52 14 53 14 54 14 55 25 56 18...
output:
949366909
result:
wrong answer 1st numbers differ - expected: '337494603', found: '949366909'