QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#715937#2549. King's PalaceMilkcat2009TL 801ms36324kbC++141.9kb2024-11-06 13:50:472024-11-06 13:50:47

Judging History

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

  • [2024-11-06 13:50:47]
  • 评测
  • 测评结果:TL
  • 用时:801ms
  • 内存:36324kb
  • [2024-11-06 13:50:47]
  • 提交

answer

#include <bits/stdc++.h>
#define REP(i, l, r) for (int i = (l); i <= (r); ++ i)
#define DEP(i, r, l) for (int i = (r); i >= (l); -- i)
#define fi first
#define se second
#define pb emplace_back
#define mems(x, v) memset((x), (v), sizeof(x))
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
using namespace std;
namespace Milkcat {
	typedef long long LL;
	typedef pair<LL, LL> pii;
	const int N = 22;
	LL n, m, rs, x, y, c1, c2, c[N], f[1 << N]; char s1, s2;
	vector<pii> g[N][3];
	int main() {
		cin >> n >> m;
		REP(i, 1, m) {
			cin >> x >> s1 >> y >> s2, x --, y --;
			c1 = (s1 == 'R' ? 0 : (s1 == 'G' ? 1 : 2));
			c2 = (s2 == 'R' ? 0 : (s2 == 'G' ? 1 : 2));
			g[x][c1].pb(y, c2), g[y][c2].pb(x, c1);
		}
		f[0] = 1;
		REP(S, 1, (1 << n) - 1) {
			int x = __lg(S & -S);
			for (int v : {0, 1}) {
				int T = 0, chk = 1;
				REP(i, 0, n - 1) c[i] = -1;
				queue<int> q; c[x] = v, q.push(x); 
				while (q.size()) {
					int u = q.front(); q.pop(), T |= 1 << u;
					for (auto [v, w] : g[u][c[u]]) {
						if (w == 2 || !(S >> v & 1)) continue;
						if (c[v] == -1) c[v] = !w, q.push(v);
						else chk &= (c[v] != w);
					}
				}
				if (chk) f[S] += f[S ^ T];
			}
		}
		REP(S, 0, (1 << n) - 1) {
			int chk = 1, T = (1 << n) - 1; queue<int> q;
			REP(i, 0, n - 1) {
				c[i] = -1;
				if (S >> i & 1) c[i] = 2, q.push(i);
			}
			while (q.size()) {
				int u = q.front(); q.pop(), T ^= 1 << u;
				for (auto [v, w] : g[u][c[u]]) {
					if (w == 2) { chk &= (c[v] != 2); continue; }
					if (c[v] == -1) c[v] = !w, q.push(v);
					else chk &= (c[v] != w);
				}
			}
			if (chk) rs += f[T];
		}
		cout << rs << '\n';
		return 0;
	}
}
int main() {
	// freopen("paint.in", "r", stdin);
	// freopen("paint.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T = 1;
	while (T --) Milkcat::main();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3824kb

input:

2 3
1 R 2 R
1 G 2 R
1 B 2 G

output:

6

result:

ok answer is '6'

Test #2:

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

input:

1 0

output:

3

result:

ok answer is '3'

Test #3:

score: 0
Accepted
time: 801ms
memory: 36324kb

input:

22 0

output:

31381059609

result:

ok answer is '31381059609'

Test #4:

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

input:

4 12
2 R 3 R
1 B 2 B
2 R 3 B
3 R 4 R
1 B 4 G
1 R 3 B
3 G 4 B
2 G 3 G
1 B 2 R
1 G 2 R
1 R 3 G
1 G 3 B

output:

13

result:

ok answer is '13'

Test #5:

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

input:

2 4
1 G 2 G
1 B 2 R
1 R 2 G
1 B 2 B

output:

5

result:

ok answer is '5'

Test #6:

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

input:

5 77
3 B 5 B
2 G 5 G
4 R 5 G
1 G 2 B
1 R 4 R
4 B 5 G
2 B 3 G
2 G 5 B
1 R 3 G
2 R 5 R
3 B 4 R
1 R 2 B
3 G 4 G
1 B 5 G
3 R 5 G
3 G 4 B
1 B 4 G
4 B 5 R
2 R 4 G
1 G 4 B
2 G 3 R
2 R 5 B
1 G 2 R
2 B 4 R
2 R 3 R
3 B 5 G
2 G 3 G
1 R 3 R
1 R 5 G
2 G 3 B
3 B 4 B
4 R 5 B
1 R 2 G
3 G 5 R
1 R 2 R
2 B 5 B
3 B 5 R...

output:

0

result:

ok answer is '0'

Test #7:

score: 0
Accepted
time: 1ms
memory: 3864kb

input:

10 141
3 B 9 B
1 R 8 R
4 B 8 R
2 B 4 R
2 R 7 B
6 B 9 R
1 R 9 R
4 R 8 G
3 B 8 R
3 B 5 G
4 B 9 B
4 G 5 R
2 R 3 G
7 B 8 G
5 B 7 R
7 B 8 R
2 B 8 B
7 R 10 B
2 G 10 G
6 G 8 B
1 R 4 B
8 R 10 B
2 G 3 B
2 B 5 B
3 R 4 R
3 B 7 R
3 R 7 R
2 R 10 R
3 G 9 G
5 B 10 G
6 R 8 B
3 R 9 G
1 B 10 G
3 R 8 G
1 B 3 R
4 R 9 R...

output:

0

result:

ok answer is '0'

Test #8:

score: -100
Time Limit Exceeded

input:

22 2079
1 R 2 R
1 R 2 G
1 R 2 B
1 G 2 R
1 G 2 G
1 G 2 B
1 B 2 R
1 B 2 G
1 B 2 B
1 R 3 R
1 R 3 G
1 R 3 B
1 G 3 R
1 G 3 G
1 G 3 B
1 B 3 R
1 B 3 G
1 B 3 B
1 R 4 R
1 R 4 G
1 R 4 B
1 G 4 R
1 G 4 G
1 G 4 B
1 B 4 R
1 B 4 G
1 B 4 B
1 R 5 R
1 R 5 G
1 R 5 B
1 G 5 R
1 G 5 G
1 G 5 B
1 B 5 R
1 B 5 G
1 B 5 B
1 R ...

output:


result: