QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#425550 | #4773. Piece it together | Ge_QingHui | AC ✓ | 138ms | 26652kb | C++14 | 2.8kb | 2024-05-30 13:32:23 | 2024-05-30 13:32:24 |
Judging History
answer
#pragma GCC optmize(2,3,"Ofast","inline","unroint-loops")
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+9;
const int M=5e6+9;
int n,m,cnt,mat[N],id[2][509][509],s,t;
const int INF=0x3f3f3f3f;
bool vst[N];
char mp[509][509];
struct Dinic{
struct Edge{int u,v,nxt;} Eg[M];
int hd[N],tot=1;
void add(int x,int y,int z){
tot++; Eg[tot]=(Edge){y,z,hd[x]}; hd[x]=tot;
tot++; Eg[tot]=(Edge){x,0,hd[y]}; hd[y]=tot;
}
void clear(){
tot=1;
for(int i=1;i<=cnt;i++) hd[i]=0;
}
int d[N];
queue<int> q;
bool bfs(){
for(int i=1;i<=cnt;i++) d[i]=0;
while(!q.empty()) q.pop();
q.push(s); d[s]=1;
while(!q.empty()){
int pre=q.front(); q.pop();
for(int i=hd[pre],u;i;i=Eg[i].nxt)
if(Eg[i].v && !d[u=Eg[i].u]){
q.push(u); d[u]=d[pre]+1;
if(u==t) return true;
}
}
return false;
}
int dfs(int x,int flow){
if(x==t) return flow;
int rest=flow;
for(int i=hd[x],u;i && rest;i=Eg[i].nxt)
if(Eg[i].v && d[u=Eg[i].u]==d[x]+1 && d[x]!=0){
int k=dfs(u,min(rest,Eg[i].v));
if(!k) d[u]=-INF;
Eg[i].v-=k;
Eg[i^1].v+=k;
rest-=k;
}
return flow-rest;
}
int dinic(){
int ans=0;
while(bfs()){
int cnt=0;
while(1){
cnt=dfs(s,INF);
if(cnt==0) break;
ans+=cnt;
}
}
return ans;
}
} T;
const int dx[5]={0,0,1,-1};
const int dy[5]={1,-1,0,0};
void solve(){
cnt=0; cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) cin>>mp[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(mp[i][j]=='.') continue;
if(mp[i][j]=='W') id[0][i][j]=++cnt;
if(mp[i][j]=='B') id[0][i][j]=++cnt,id[1][i][j]=++cnt;
}
s=++cnt; t=++cnt;
T.clear();
int sB=0,sW=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(mp[i][j]=='B'){
for(int k=0;k<4;k++){
int ni=i+dx[k],nj=j+dy[k];
if(1<=ni && ni<=n && 1<=nj && nj<=m && mp[ni][nj]=='W'){
T.add(id[(k<2)][i][j],id[0][ni][nj],1);
}
}
sB++;
T.add(s,id[0][i][j],1);
T.add(s,id[1][i][j],1);
}
else if(mp[i][j]=='W'){
sW++;
T.add(id[0][i][j],t,1);
}
}
if(sB*2!=sW) puts("NO");
else if(T.dinic()!=sW) puts("NO");
else puts("YES");
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
int T; cin>>T;
while(T--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3660kb
input:
2 3 4 BWW. WWBW ..WB 3 3 W.. BW. WBW
output:
YES NO
result:
ok 2 token(s): yes count is 1, no count is 1
Test #2:
score: 0
Accepted
time: 138ms
memory: 26652kb
input:
70 3 4 BWW. WWBW ..WB 3 3 W.. BW. WBW 1 1 B 3 3 ... .W. ... 2 2 W. BW 2 3 .W. WBW 1 3 WBW 2 5 .W.W. WB.BW 2 2 WW .B 2 2 WB .. 3 3 WWB BWW WWB 3 5 .W.WB WBW.W ...WB 4 5 ..W.. .WBW. WBWBW .WBW. 3 9 BWW...W.. WWBW..BW. ..WB..WBW 4 12 BWWBWWBWWBWW WWBWWBWWBWWB BWWBWWBWWBWW WWBWWBWWBWWB 7 7 BWWBBWW WBWWW...
output:
YES NO NO NO YES NO NO YES NO NO NO NO YES NO YES YES NO YES YES YES NO YES YES NO NO NO NO NO NO NO NO NO YES YES YES NO YES YES YES YES YES YES YES YES YES YES YES YES NO YES YES YES NO YES NO YES NO NO NO NO NO YES NO YES YES NO NO NO NO NO
result:
ok 70 token(s): yes count is 34, no count is 36