QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#491788 | #6553. Shared Memory Switch | pandapythoner | RE | 0ms | 0kb | C++23 | 2.5kb | 2024-07-25 22:07:11 | 2024-07-25 22:07:12 |
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) for(int i = 0; i < n; i += 1)
const ll inf = 1e18;
mt19937 rnd(234);
const ll mod = 998244353;
const int maxn = 1000;
int n, m;
char g[maxn][maxn];
char self_loop[maxn];
void swap_vertices(int u, int v) {
if (u == v) {
return;
}
swap(self_loop[u], self_loop[v]);
rep(i, n) {
swap(g[i][u], g[i][v]);
}
rep(i, n) {
swap(g[u][i], g[v][i]);
}
}
void make_flex(int u, int v) {
assert(u != v);
if (self_loop[u] and g[v][u]) {
} else if (self_loop[u]) {
self_loop[v] ^= 1;
g[u][v] ^= 1;
g[v][u] ^= 1;
} else if (g[u][v]) {
self_loop[v] ^= 1;
}
rep(i, n) if (i != u and i != v) g[i][v] = g[v][i] = (g[i][u] ^ g[i][v]);
}
int32_t main() {
if (1) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
cin >> n >> m;
rep(i, n) rep(j, n) g[i][j] = 0;
rep(i, n) self_loop[i] = 0;
for (int i = 0; i < m; i += 1) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u][v] = g[v][u] = 1;
}
for (int i = 0; i < n - 1; i += 1) {
int to = -1;
for (int j = i + 1; j < n; j += 1) {
if (g[i][j]) {
to = j;
break;
}
}
if (to == -1) {
continue;
}
for (int j = to + 1; j < n; j += 1) {
if (g[i][j]) {
make_flex(to, j);
assert(g[i][j] == 0);
}
}
swap_vertices(to, i + 1);
}
array<array<ll, 2>, 2> dp;
rep(i, 2) rep(j, 2) dp[i][j] = 0;
dp[0][0] = 1;
for (int i = 0; i < n; i += 1) {
int d = 0;
if (i > 0) {
d = g[i][i - 1];
}
array<array<ll, 2>, 2> ndp;
rep(x, 2) rep(y, 2) ndp[x][y] = 0;
rep(prev, 2) rep(prev_sum, 2) rep(cur, 2) {
auto& new_val = ndp[(prev_sum + cur * self_loop[i] + d * cur * prev) % 2][cur];
new_val += dp[prev_sum][prev];
if (new_val >= mod) {
new_val -= mod;
}
}
dp.swap(ndp);
}
ll result = (dp[0][0] + dp[0][1]) % mod;
cout << result << "\n";
return 0;
}
/*
5 5
1 2
2 3
3 4
4 5
1 5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
14 5 1 1 1 2 2 2 -1 1 -1 1 -1 1 -1 1