QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#101389#5250. Combination LocksDestroyXuanQuang#WA 2ms3464kbC++233.1kb2023-04-29 13:00:092023-04-29 13:00:10

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-29 13:00:10]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3464kb
  • [2023-04-29 13:00:09]
  • 提交

answer

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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

struct UF {
	vi e;
	UF(int n) : e(n, -1) {}
	bool sameSet(int a, int b) { return find(a) == find(b); }
	int size(int x) { return -e[find(x)]; }
	int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
	bool join(int a, int b) {
		a = find(a), b = find(b);
		if (a == b) return false;
		if (e[a] > e[b]) swap(a, b);
		e[a] += e[b]; e[b] = a;
		return true;
	}
};

bool dfs(int a, int L, vector<vi>& g, vi& btoa, vi& A, vi& B) {
	if (A[a] != L) return 0;
	A[a] = -1;
	for (int b : g[a]) if (B[b] == L + 1) {
		B[b] = 0;
		if (btoa[b] == -1 || dfs(btoa[b], L + 1, g, btoa, A, B))
			return btoa[b] = a, 1;
	}
	return 0;
}

int hopcroftKarp(vector<vi>& g, vi& btoa) {
	int res = 0;
	vi A(g.size()), B(btoa.size()), cur, next;
	for (;;) {
		fill(all(A), 0);
		fill(all(B), 0);
		/// Find the starting nodes for BFS (i.e. layer 0).
		cur.clear();
		for (int a : btoa) if(a != -1) A[a] = -1;
		rep(a,0,sz(g)) if(A[a] == 0) cur.push_back(a);
		/// Find all layers using bfs.
		for (int lay = 1;; lay++) {
			bool islast = 0;
			next.clear();
			for (int a : cur) for (int b : g[a]) {
				if (btoa[b] == -1) {
					B[b] = lay;
					islast = 1;
				}
				else if (btoa[b] != a && !B[b]) {
					B[b] = lay;
					next.push_back(btoa[b]);
				}
			}
			if (islast) break;
			if (next.empty()) return res;
			for (int a : next) A[a] = lay;
			cur.swap(next);
		}
		/// Use DFS to scan for augmenting paths.
		rep(a,0,sz(g))
			res += dfs(a, 0, g, btoa, A, B);
	}
}

void solve() {
	int n, m; cin >> n >> m;
	string p1, p2; cin >> p1 >> p2;

	int st = 0;
	for (int i = 0; i < n; i++) {
		st <<= 1;
		if (p1[i] == p2[i]) {
			st |= 1;
		}
	}

	vector<int> ban(1 << n);
	for (int i = 0; i < m; i++) {
		string s; cin >> s;
		int msk = 0;
		for (char c: s) {
			msk <<= 1;
			if (c == '=') msk |= 1;
		}
		ban[msk] = 1;
	}

	UF d(1 << n);

	vector<vi> g(1 << n);
	for (int i = 0; i < (1 << n); i++) {
		for (int b = 0; b < n; b++) {
			int j = i ^ (1 << b);
			if (ban[i] || ban[j]) continue;
			d.join(i, j);
			if ((__builtin_popcount(i) % 2) == 0) {
				g[i].push_back(j);
			}
		}
	}

	vi btoa((1 << n), -1); 
	hopcroftKarp(g, btoa);

	int alice = __builtin_popcount(st) % 2;
	int bob = alice^1;

	// alice wins if all connected nodes belonging to alice side is matched with some node on the bob side
	vector<int> matched(1 << n);
	for (int i = 0; i < (1 << n); i++) {
		if (btoa[i] != -1) {
			matched[i] = matched[btoa[i]] = 1;
		}
	}

	bool aw = 1;
	for (int i = 0; i < (1 << n); i++) {
		if (d.sameSet(i, st) && __builtin_popcount(i) % 2 == alice) {
			if (!matched[i]) aw = 0;
		}
	}

	if (aw) cout << "Alice\n";
	else cout << "Bob\n";
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);
	
	int t; cin >> t;
	while (t--) {
		solve();
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3444kb

input:

2
1 0
0
0
1 1
0
0
.

output:

Alice
Bob

result:

ok 2 lines

Test #2:

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

input:

8
2 0
00
00
2 1
00
00
..
2 1
00
00
=.
2 2
00
00
..
=.
2 1
00
00
.=
2 2
00
00
..
.=
2 2
00
00
=.
.=
2 3
00
00
..
=.
.=

output:

Alice
Alice
Bob
Alice
Bob
Alice
Bob
Bob

result:

ok 8 lines

Test #3:

score: 0
Accepted
time: 2ms
memory: 3464kb

input:

20
4 4
4714
5245
..=.
..==
.==.
==..
4 1
2697
1438
.=..
4 5
9255
0677
...=
..==
=..=
==.=
====
4 12
3292
7326
...=
..=.
..==
.=..
.=.=
.==.
=...
=..=
=.==
==..
==.=
====
4 9
8455
2536
...=
..==
.=..
.=.=
.==.
.===
=...
==..
===.
4 12
5755
1517
...=
..=.
..==
.=..
.=.=
.===
=..=
=.=.
=.==
==..
==.=
=...

output:

Alice
Bob
Alice
Bob
Bob
Alice
Bob
Bob
Alice
Alice
Bob
Alice
Alice
Bob
Bob
Bob
Bob
Bob
Bob
Bob

result:

ok 20 lines

Test #4:

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

input:

20
5 30
99942
90170
.....
....=
...==
..=..
..=.=
..==.
..===
.=...
.=..=
.=.=.
.=.==
.==..
.==.=
.===.
.====
=...=
=..=.
=..==
=.=..
=.=.=
=.==.
=.===
==...
==..=
==.=.
==.==
===..
===.=
====.
=====
5 14
11760
95480
...=.
...==
..=..
..=.=
.=...
.=..=
.====
=....
=...=
=.=..
=.==.
==...
==.==
=====...

output:

Bob
Alice
Alice
Alice
Alice
Bob
Bob
Bob
Alice
Alice
Alice
Bob
Alice
Alice
Alice
Alice
Alice
Alice
Alice
Bob

result:

ok 20 lines

Test #5:

score: -100
Wrong Answer
time: 2ms
memory: 3412kb

input:

20
6 62
188256
588825
......
.....=
....=.
....==
...=..
...=.=
...==.
...===
..=...
..=..=
..=.=.
..=.==
..==..
..==.=
..===.
..====
.=....
.=...=
.=..=.
.=..==
.=.=..
.=.=.=
.=.==.
.=.===
.==..=
.==.=.
.==.==
.===..
.===.=
.=====
=.....
=....=
=...=.
=...==
=..=..
=..=.=
=..==.
=..===
=.=...
=.=.....

output:

Bob
Bob
Alice
Alice
Alice
Bob
Bob
Bob
Bob
Alice
Bob
Bob
Alice
Alice
Bob
Bob
Alice
Alice
Alice
Bob

result:

wrong answer 15th lines differ - expected: 'Alice', found: 'Bob'