This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int dp[32768];
int cnt[32768];
map<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;
}
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 init = 1; init < (1 << n); init++) {
if (check_valid_layer(edges, init))
cnt[init] = 1;
for (int nxt = 1; nxt < (1 << n); nxt++) {
if (init & nxt)
continue;
if (!check_valid_layer(edges, nxt))
continue;
int cost = 0;
for (int i = 0; i < n; i++) {
if ((init & (1 << i)) == 0)
continue;
for (int j = 0; j < n; j++) {
if ((nxt & (1 << j)) == 0)
continue;
if (find(edges.begin(), edges.end(), make_pair(j, i)) !=
edges.end())
cost++;
}
}
mem[init][nxt] = cost;
// cout << "MEM " << init << " " << nxt << " " << cost << endl;
}
}
for (int acc = 0; acc < (1 << n); acc++) {
// cout << "DP " << acc << ": " << dp[acc] << endl;
for (auto nxt : mem[acc]) {
if (nxt.first & acc)
continue;
// cout << "acc " << acc << " nxt " << nxt.first << " " <<
// nxt.second
// << " giving " << dp[acc] + nxt.second * cnt[acc] << endl;
dp[acc | nxt.first] +=
(dp[acc] + 1ll * nxt.second * cnt[acc]) % MOD;
dp[acc | nxt.first] %= MOD;
cnt[acc | nxt.first] += cnt[acc];
cnt[acc | nxt.first] %= MOD;
}
}
cout << dp[(1 << n) - 1] << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |