QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#276230 | #7196. Perfect Matching | PetroTarnavskyi# | WA | 386ms | 66076kb | C++20 | 2.3kb | 2023-12-05 18:26:45 | 2023-12-05 18:26:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int mod = 1'000'000'007;
const int N = 30;
void updAdd(int& x, int val)
{
x += val;
if (x >= mod)
x -= mod;
}
int mult(int a, int b)
{
return (LL)a * b % mod;
}
int g[N];
bool containsBit(int mask, int i)
{
return (mask >> i) & 1;
}
VI calcDPOneHalf(int l, int r)
{
int k = r - l;
VI dp(1 << k);
dp[0] = 1;
FOR(i, 0, k)
{
FOR(mask, 0, 1 << k)
{
if (containsBit(mask, i))
continue;
FOR(j, i + 1, k)
{
if (containsBit(g[i + l], j + l) && !containsBit(mask, j))
{
updAdd(dp[mask | (1 << i) | (1 << j)], dp[mask]);
}
}
}
}
return dp;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
FOR(i, 0, m)
{
int u, v;
cin >> u >> v;
u--;
v--;
g[u] |= 1 << v;
g[v] |= 1 << u;
}
int l = min(19, n), r = n - l;
VI dpL = calcDPOneHalf(0, l), dpR = calcDPOneHalf(l, n);
reverse(ALL(dpL));
reverse(ALL(dpR));
vector<VI> masks(r + 1);
VI maskIdx(1 << l, -1);
FOR(mask, 0, 1 << l)
{
int pc = __builtin_popcount(mask);
if (pc <= r)
{
maskIdx[mask] = SZ(masks[pc]);
masks[pc].PB(mask);
}
}
vector<VI> dp = {{1}};
int ans = mult(dpL[0], dpR[0]);
FOR(i, 1, r + 1)
{
int cntR = 0;
for (int mask : masks[i])
if (mask < (1 << r))
cntR++;
vector ndp(cntR, VI(SZ(masks[i])));
FOR(j, 0, cntR)
{
int maskR = masks[i][j];
FOR(k, 0, SZ(masks[i]))
{
int maskL = masks[i][k];
int bitL = 31 - __builtin_clz(maskL);
int nmaskLIdx = maskIdx[maskL ^ (1 << bitL)];
FOR(bitR, 0, r)
{
if (containsBit(maskR, bitR))
{
updAdd(ndp[j][k],
dp[maskIdx[maskR ^ (1 << bitR)]][nmaskLIdx]);
}
}
updAdd(ans, mult(ndp[j][k], mult(dpL[maskL], dpR[maskR])));
}
}
dp = ndp;
}
cout << ans << "\n";
cerr << (db)clock() / CLOCKS_PER_SEC << endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 4072kb
input:
4 4 1 3 1 4 2 3 2 4
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3872kb
input:
4 6 1 2 1 3 1 4 2 3 2 4 3 4
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: 0
Accepted
time: 0ms
memory: 4072kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 0ms
memory: 4004kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3932kb
input:
2 1 2 1
output:
1
result:
ok 1 number(s): "1"
Test #6:
score: 0
Accepted
time: 0ms
memory: 4004kb
input:
4 5 4 2 2 3 3 1 1 4 3 4
output:
2
result:
ok 1 number(s): "2"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3916kb
input:
8 18 8 7 4 3 4 8 5 3 3 6 2 5 6 2 8 6 7 3 4 7 1 2 3 8 2 3 8 5 1 7 7 6 1 6 8 1
output:
15
result:
ok 1 number(s): "15"
Test #8:
score: 0
Accepted
time: 5ms
memory: 3876kb
input:
16 91 9 12 8 14 5 2 4 12 11 16 7 9 8 16 4 10 6 15 9 14 10 16 15 7 15 16 11 12 11 10 10 2 11 13 8 1 5 14 16 3 13 3 5 10 7 10 15 11 11 2 6 9 1 7 7 3 2 8 6 7 9 16 11 9 4 3 16 1 15 2 6 5 16 12 3 15 13 14 7 16 9 1 9 2 1 14 15 14 10 1 10 9 16 14 16 4 2 14 6 12 5 8 11 1 1 4 1 5 6 14 12 7 10 15 12 2 12 5 1 ...
output:
222058
result:
ok 1 number(s): "222058"
Test #9:
score: 0
Accepted
time: 386ms
memory: 66076kb
input:
29 303 13 22 17 7 21 13 29 26 3 23 24 29 11 9 17 3 25 28 19 8 25 9 22 2 4 11 23 10 24 27 23 20 8 9 6 9 20 15 22 4 1 19 15 16 25 5 17 16 1 5 22 18 10 7 8 26 29 17 27 15 29 10 13 28 17 11 8 13 29 2 9 15 1 10 8 23 6 26 28 14 4 8 1 20 2 21 14 15 24 11 11 22 27 19 7 22 25 12 26 3 7 12 15 4 22 26 4 29 16 ...
output:
0
result:
ok 1 number(s): "0"
Test #10:
score: -100
Wrong Answer
time: 54ms
memory: 7264kb
input:
20 150 20 3 17 8 5 11 20 11 12 9 15 1 3 18 19 11 4 10 8 7 1 13 13 9 14 9 1 20 3 16 16 4 14 15 1 8 5 14 16 12 8 9 2 19 3 15 13 7 19 13 20 14 10 9 1 6 6 15 20 2 13 20 18 2 8 11 5 16 19 8 1 14 13 6 1 19 15 18 20 5 14 8 1 12 10 18 3 4 4 20 13 10 8 5 12 7 5 13 1 7 16 7 15 4 14 6 10 12 11 1 11 13 15 7 9 1...
output:
80375996
result:
wrong answer 1st numbers differ - expected: '55571531', found: '80375996'