#include <bits/stdc++.h>
using namespace std;
map<int, int> dp[32768];
int cnt[32768];
map<pair<int, int>, int> mem[32768];
int n, m;
bool check_valid_layer(const vector<pair<int, int>> &edges, int mask) {
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) == 0)
continue;
for (int j = 0; j < i; j++) {
if ((mask & (1 << j)) == 0)
continue;
if (find(edges.begin(), edges.end(), make_pair(i, j)) !=
edges.end() ||
find(edges.begin(), edges.end(), make_pair(j, i)) !=
edges.end()) {
return false;
}
}
}
return true;
}
bool check_condition(const vector<pair<int, int>> &edges, int mask_old,
int mask_new) {
if (mask_old == 0)
return true;
for (int i = 0; i < n; i++) {
if ((mask_new & (1 << i)) == 0)
continue;
bool ok = false;
for (int j = 0; j < n; j++) {
if ((mask_old & (1 << j)) == 0)
continue;
if (find(edges.begin(), edges.end(), make_pair(j, i)) !=
edges.end() ||
find(edges.begin(), edges.end(), make_pair(i, j)) !=
edges.end())
ok = true;
}
if (!ok)
return false;
}
return true;
}
const int MOD = 998244353;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
vector<pair<int, int>> edges;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
edges.push_back({u, v});
}
for (int front = 1; front < (1 << n); front++) {
if (!check_valid_layer(edges, front))
continue;
for (int incoming = 1; incoming < (1 << n); incoming++) {
if (!check_valid_layer(edges, incoming))
continue;
if (front & incoming)
continue;
if (!check_condition(edges, front, incoming))
continue;
for (int scanned = 0; scanned < (1 << n); scanned++) {
if (scanned & (front | incoming))
continue;
if (!check_condition(edges, scanned, front))
continue;
int cost = 0;
for (int i = 0; i < n; i++) {
if (((scanned | front) & (1 << i)) == 0)
continue;
for (int j = 0; j < n; j++) {
if ((incoming & (1 << j)) == 0)
continue;
if (find(edges.begin(), edges.end(), make_pair(j, i)) !=
edges.end())
cost++;
}
}
mem[scanned | front][make_pair(front, incoming)] = cost;
// cout << "MEM " << scanned << " " << front << " " << incoming
// << ": " << cost << endl;
}
}
}
int sum = 0;
for (int acc = 0; acc < (1 << n); acc++) {
// cout << "DP " << acc << ": " << dp[acc] << endl;
if (acc == (1 << n) - 1)
for (auto front : dp[acc]) {
sum += front.second;
sum %= MOD;
}
for (auto mp : mem[acc]) {
int front = mp.first.first;
int incoming = mp.first.second;
int cost = mp.second;
dp[acc | incoming][incoming] += (dp[acc][front] + cost) % MOD;
dp[acc | incoming][incoming] %= MOD;
}
}
cout << sum << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
2 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Incorrect |
2 ms |
3420 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
2 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Incorrect |
2 ms |
3420 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
2 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Incorrect |
2 ms |
3420 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
2 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Incorrect |
2 ms |
3420 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |