QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#334963#7925. ChayasSTnofarjo#WA 75ms1969788kbC++172.4kb2024-02-22 15:38:032024-02-22 15:38:03

Judging History

你现在查看的是最新测评结果

  • [2024-02-22 15:38:03]
  • 评测
  • 测评结果:WA
  • 用时:75ms
  • 内存:1969788kb
  • [2024-02-22 15:38:03]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int maxn = 24, maxm = maxn * maxn * maxn, mod = 998244353;
int n, m;
int a[maxm], b[maxm], c[maxm];
set<vector<int>> st;
// bitset<1<<maxn> invalid[maxn][maxn]; // invalid[a][b][mask] = boleh ga dari a ke b kalo masknya mask?
// set<int> invalid[maxn][maxn];
bool invalid[1<<maxn][maxn];
int dp[1<<maxn][maxn];
int ans;

int cut(int mask, int l, int r) {
	return (mask >> l) & ~(1 << (r-l+1));
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n >> m;
	for (int i=0; i<m; i++) {
		cin >> a[i] >> b[i] >> c[i];
		a[i]--, b[i]--, c[i]--;
	}

	for (int i=0; i<m; i++) {
		vector<int> abc = {a[i], b[i], c[i]};
		sort(abc.begin(), abc.end());
		if (st.count(abc)) {
			cout << "0\n";
			return 0;
		}
		st.insert(abc);
	}

	memset(invalid, false, sizeof invalid);
	for (int i=0; i<m; i++) {

		// gaboleh ke b kalo a dan c sama
		vector<int> srt = {a[i], b[i], c[i]};
		sort(srt.begin(), srt.end());
		for (int mask=0; mask<(1<<(n-3)); mask++) {
			int true_mask = 0;

			if (srt[0] > 0) true_mask |= cut(mask, 0, srt[0] - 1);
			if (srt[0] + 1 <= srt[1] - 1) true_mask |= (cut(mask, srt[0], srt[1] - 2) << (srt[0] + 1));
			if (srt[1] + 1 <= srt[2] - 1) true_mask |= (cut(mask, srt[1] - 1, srt[2] - 3) << (srt[1] + 1));
			if (srt[2] + 1 <= n-1) true_mask |= (cut(mask, srt[2] - 2, n - 4) << (srt[2] + 1));
			invalid[true_mask][b[i]] = true;

			true_mask |= (1 << a[i]);
			true_mask |= (1 << c[i]);
			invalid[true_mask][b[i]] = true;
		}
	}

	memset(dp, 0, sizeof dp);
	for (int mask=1; mask<(1<<n); mask++) {
		if ((mask&(mask-1)) == 0) {
			int i = 31 - __builtin_clz(mask);
			dp[mask][i] = (!invalid[0][i]); // cek dulu i pernah jadi b atau nggak
		} else {
			vector<int> ons;
			for (int i=0; i<n; i++) {
				if ((mask >> i) & 1) ons.push_back(i);
			}
			for (int i : ons) {
				for (int j : ons) {
					if (i == j) continue;
					int pre_mask = mask & ~(1 << j);
					// if (!invalid[i][j][pre_mask]) dp[mask][j] = (dp[mask][j] + dp[pre_mask][i]) % mod;
					// if (!invalid[i][j].count(pre_mask)) dp[mask][j] = (dp[mask][j] + dp[pre_mask][i]) % mod;
					if (!invalid[pre_mask][j]) dp[mask][j] = (dp[mask][j] + dp[pre_mask][i]) % mod;;
				}
			}
		}
	}

	ans = 0;
	for (int i=0; i<n; i++) ans = (ans + dp[(1 << n) - 1][i]) % mod;
	cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 71ms
memory: 1969788kb

input:

5 4
1 2 4
2 3 5
3 2 4
1 3 2

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3548kb

input:

4 2
3 1 4
1 4 3

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 64ms
memory: 1969696kb

input:

5 5
3 1 2
2 5 4
5 4 3
3 1 5
1 4 5

output:

2

result:

ok single line: '2'

Test #4:

score: -100
Wrong Answer
time: 75ms
memory: 1969628kb

input:

6 6
1 6 3
2 3 4
5 6 4
3 5 1
1 3 4
1 2 4

output:

9

result:

wrong answer 1st lines differ - expected: '6', found: '9'