QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#129320 | #5250. Combination Locks | Sommohito# | WA | 1ms | 3728kb | C++20 | 4.6kb | 2023-07-22 15:07:07 | 2023-07-22 15:07:10 |
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;
struct FlowEdge {
int v,u,id; long long cap, flow = 0;
FlowEdge(int v, int u, long long cap,int id=-1) : v(v), u(u), cap(cap),id(id) {}
};
struct Dinic
{
vector<FlowEdge> edges; vector<vector<int> > adj;
int n, m = 0; int s, t;
vector<int> level, ptr,flow_through;
queue<int> q; vector<bool>vis;
int maxid=0;
Dinic() {}
Dinic(int n) : n(n) {
vis.resize(n); adj.resize(n);
level.resize(n); ptr.resize(n);
}
void init(int n) {
vis.resize(n); adj.resize(n);
level.resize(n); ptr.resize(n);
}
void add_edge(int v, int u, long long cap,int id=-1) {
// debug(v,u,cap);
edges.emplace_back(v, u, cap,id);
edges.emplace_back(u, v, 0);
adj[v].push_back(m);
adj[u].push_back(m + 1);
m += 2;
if(id!=-1)maxid++;
}
void dfs2(int s) {
vis[s] = 1;
for(int i:adj[s]) {
int id = i; int u = edges[id].v;
int v = edges[id].u;
if(edges[id].flow!=edges[id].cap && !vis[v])
{
dfs2(v);
}
}
}
vector<int> getMinCut() {
dfs2(s); vector<int>ret;
for(int i=0; i<n; i++) {
if(vis[i]) ret.push_back(i);
}
return ret;
}
bool bfs() {
while (!q.empty()) {
int v = q.front();
q.pop();
for (int id : adj[v])
{
if (edges[id].cap - edges[id].flow < 1)
continue;
if (level[edges[id].u] != -1)
continue;
level[edges[id].u] = level[v] + 1;
q.push(edges[id].u);
}
}
return level[t] != -1;
}
long long dfs(int v, long long pushed) {
if (pushed == 0) return 0;
if (v == t) return pushed;
for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++){
int id = adj[v][cid]; int u = edges[id].u;
if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
continue;
long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
if (tr == 0)
continue;
edges[id].flow += tr; edges[id ^ 1].flow -= tr;
return tr;
}
return 0;
}
long long flow(int _s,int _t) {
s=_s; t=_t;
long long f = 0;
while (true)
{
fill(level.begin(), level.end(), -1);
level[s] = 0; q.push(s);
if (!bfs()) break;
fill(ptr.begin(), ptr.end(), 0);
while (long long pushed = dfs(s, flow_inf)){
f += pushed;
}
}
flow_through.assign(maxid+1, 0);
for(int i = 0; i < n; i++){
for(auto j : adj[i]) {
int idx = j;
FlowEdge e = edges[idx];
if(e.id >= 0)flow_through[e.id] = e.flow;
}
}
return f;
}
};
Dinic dinic1, dinic2;
int dead[1<<10+5];
int init;
int n,c;
int total;
void ADD(int u,int v)
{
dinic1.add_edge(u,v,1);
if(u!=init&&v!=init)
dinic2.add_edge(u,v,1);
}
void dfs(int mask)
{
dead[mask]=2;
int cnt=__builtin_popcount(mask);
if(cnt%2==__builtin_popcount(init)%2)
ADD((1<<n),mask),total++;
else
ADD(mask,(1<<n)+1);
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=Dinic((1<<n)+2);
dinic2=Dinic((1<<n)+2);
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 got1=dinic1.flow(1<<n,(1<<n)+1);
int got2=dinic2.flow(1<<n,(1<<n)+1);
// debug(got);
if(got1==total||(got1!=got2))
cout<<"Alice\n";
else cout<<"Bob\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: 3728kb
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: 3620kb
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: 1ms
memory: 3628kb
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: 1ms
memory: 3672kb
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'