QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#129333 | #5250. Combination Locks | Sommohito# | WA | 2ms | 4596kb | C++20 | 4.2kb | 2023-07-22 15:31:50 | 2023-07-22 15:31:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#ifdef APURBA
#include "DEBUG_TEMPLATE.h"
#else
#define HERE
#define debug(args...)
#endif
#define ALL(x) x.begin(),x.end()
const long long flow_inf = 1e18;
const int N=1e4;
struct Blossom {
int vis[N], par[N], orig[N], match[N], aux[N], t;
int n;
bool ad[N];
vector<int> g[N];
queue<int> Q;
Blossom() {}
Blossom(int _n) {
n = _n;
t = 0;
for (int i = 0; i <= _n; ++i) {
g[i].clear();
match[i] = aux[i] = par[i] = vis[i] = aux[i] = ad[i] = orig[i] = 0;
}
}
void add_edge(int u, int v) {
g[u].push_back(v);
g[v].push_back(u);
}
void augment(int u, int v) {
int pv = v, nv;
do {
pv = par[v];
nv = match[pv];
match[v] = pv;
match[pv] = v;
v = nv;
} while (u != pv);
}
int lca(int v, int w) {
++t;
while (true) {
if (v) {
if (aux[v] == t) return v;
aux[v] = t;
v = orig[par[match[v]]];
}
swap(v, w);
}
}
void blossom(int v, int w, int a) {
while (orig[v] != a) {
par[v] = w;
w = match[v];
ad[v] = true;
if (vis[w] == 1) Q.push(w), vis[w] = 0;
orig[v] = orig[w] = a;
v = par[w];
}
}
//it finds an augmented path starting from u
bool bfs(int u) {
fill(vis + 1, vis + n + 1, -1);
iota(orig + 1, orig + n + 1, 1);
Q = queue<int> ();
Q.push(u);
vis[u] = 0;
while (!Q.empty()) {
int v = Q.front();
Q.pop();
ad[v] = true;
for (int x : g[v]) {
if (vis[x] == -1) {
par[x] = v;
vis[x] = 1;
if (!match[x]) return augment(u, x), true;
Q.push(match[x]);
vis[match[x]] = 0;
} else if (vis[x] == 0 && orig[v] != orig[x]) {
int a = lca(orig[v], orig[x]);
blossom(x, v, a);
blossom(v, x, a);
}
}
}
return false;
}
int maximum_matching() {
int ans = 0;
vector<int> p(n - 1);
iota(p.begin(), p.end(), 1);
// shuffle(p.begin(), p.end(), rnd);
// for (int i = 1; i <= n; i++) shuffle(g[i].begin(), g[i].end(), rnd);
for (auto u : p) {
if (!match[u]) {
for(auto v : g[u]) {
if (!match[v]) {
match[u] = v, match[v] = u;
++ans;
break;
}
}
}
}
for(int i = 1; i <= n; ++i) if (!match[i] && bfs(i)) ++ans;
return ans;
}
} dinic1;
int dead[1<<10+5];
int init;
int n,c;
int total;
void ADD(int u,int v)
{
dinic1.add_edge(u+1,v+1);
}
void dfs(int mask)
{
dead[mask]=2;
int cnt=__builtin_popcount(mask);
total++;
for(int i=0;i<n;i++)
{
int notun=mask^(1<<i);
if(dead[notun]==1||dead[notun]==2)
continue;
if(cnt%2==__builtin_popcount(init)%2)
ADD(mask,notun);
else ADD(notun,mask);
dfs(notun);
}
}
void TEST_CASES()
{
cin>>n>>c;
string a,b;
cin>>a>>b;
init=0,total=0;
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])
init|=(1<<i);
}
memset(dead,0,sizeof dead);
dinic1=Blossom((1<<n)+5);
for(int i=0;i<c;i++)
{
string a;
cin>>a;
int here=0;
for(int j=0;j<n;j++)
{
if(a[j]=='.')
here|=(1<<j);
}
dead[here]=1;
}
dfs(init);
// debug(total);
int ans=dinic1.maximum_matching();
if (ans * 2 == total) cout << "Alice" << '\n';
else {
memset(dinic1.ad, 0, sizeof dinic1.ad);
for (int i = 1; i <= (1<<n)+5; i++) if (dinic1.match[i] == 0) dinic1.bfs(i);
int ans = 0;
if(dinic1.ad[init+1])
cout<<"Bob\n";
else cout<<"Alice\n";
}
}
/*
3
2 2
12
89
=.
==
3 1
204
101
.==
3 2
000
000
...
==.
*/
int32_t main()
{
#ifndef APURBA
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
//freopen("input.txt","r",stdin);
//freopen("out1.txt","w",stdout);
int t=1;
cin>>t;
while(t--)
{
TEST_CASES();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 4472kb
input:
2 1 0 0 0 1 1 0 0 .
output:
Alice Bob
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 4596kb
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: 4508kb
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: -100
Wrong Answer
time: 2ms
memory: 4472kb
input:
20 5 30 99942 90170 ..... ....= ...== ..=.. ..=.= ..==. ..=== .=... .=..= .=.=. .=.== .==.. .==.= .===. .==== =...= =..=. =..== =.=.. =.=.= =.==. =.=== ==... ==..= ==.=. ==.== ===.. ===.= ====. ===== 5 14 11760 95480 ...=. ...== ..=.. ..=.= .=... .=..= .==== =.... =...= =.=.. =.==. ==... ==.== =====...
output:
Bob Alice Alice Alice Alice Bob Bob Bob Bob Alice Alice Bob Alice Alice Alice Alice Alice Alice Alice Bob
result:
wrong answer 9th lines differ - expected: 'Alice', found: 'Bob'