QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#303572 | #5202. Dominoes | LaStataleBlue | WA | 33ms | 3660kb | C++23 | 4.7kb | 2024-01-12 18:59:38 | 2024-01-12 18:59:38 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using pi = pair<int,int>;
#define sz(x) int((x).size())
template<class F> struct Dinic {
struct Edge { int to, rev; F cap; };
int N; vector<vector<Edge>> adj;
void init(int _N) { N = _N; adj.resize(N); } //0-based
pi ae(int a, int b, F cap, F rcap = 0) {
assert(min(cap,rcap) >= 0); // saved me > once
adj[a].push_back({b,sz(adj[b]),cap});
adj[b].push_back({a,sz(adj[a])-1,rcap});
return {a,sz(adj[a])-1};
}
F edgeFlow(pi loc) { // get flow along original edge
const Edge& e = adj.at(loc.first).at(loc.second);
return adj.at(e.to).at(e.rev).cap;
}
vector<int> lev, ptr;
bool bfs(int s,int t){//level=shortest dist from source
lev = ptr = vector<int>(N);
lev[s] = 1; queue<int> q({s});
while (sz(q)) { int u = q.front(); q.pop();
for(auto& e: adj[u]) if (e.cap && !lev[e.to]) {
q.push(e.to), lev[e.to] = lev[u]+1;
if (e.to == t) return 1;
}
}
return 0;
}
F dfs(int v, int t, F flo) {
if (v == t) return flo;
for (int& i = ptr[v]; i < sz(adj[v]); i++) {
Edge& e = adj[v][i];
if (lev[e.to]!=lev[v]+1||!e.cap) continue;
if (F df = dfs(e.to,t,min(flo,e.cap))) {
e.cap -= df; adj[e.to][e.rev].cap += df;
return df; } // saturated $\geq1$ one edge
}
return 0;
}
F maxFlow(int s, int t) {
F tot = 0; while (bfs(s,t)) while (F df =
dfs(s,t,numeric_limits<F>::max())) tot += df;
return tot;
}
};
void solve(int t){
int n,m;
cin>>n>>m;
vector mat(n,vector(m,'.'));
vector ind(n,vector(m,'.'));
int cont=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>mat[i][j];
if(mat[i][j]=='.'){
ind[i][j]=cont++;
}else{
ind[i][j]=-1;
}
}
}
const int inf = 1'000'000;
if(cont>2000)cout<<inf<<"\n";
else{
int ans=cont/2*(cont/2-1);
vector grafo(cont,vector<int>());
vector par(cont,false);
const pair<int,int> dir[4] = {{1,0},{-1,0},{0,1},{0,-1}};
for(int x=0;x<n;x++){
for(int y=(x%2);y<m;y+=2){
if(ind[x][y]>=0)par[ind[x][y]]=true;
for(auto [dx,dy] : dir){
int sx = x+dx, sy = y+dy;
if(sx>=0 && sx<n && sy>=0 && sy<m){
if(ind[x][y]>=0 && ind[sx][sy]>=0){
grafo[ind[x][y]].push_back(ind[sx][sy]);
}
}
}
}
}
auto check = [&](int x)->void{
Dinic<int> ds;
ds.init(cont+2);
int source = cont, sink = cont+1;
//cout<<"check "<<x<<"\n";
for(int i=0;i<cont;i++){
if(par[i])ds.ae(source,i,1);
else ds.ae(i,sink,1);
}
for(int i=0;i<cont;i++){
if(!par[i] || i==x)continue;
for(auto j : grafo[i]){
//cout<<i<<"->"<<j<<"\n";
ds.ae(i,j,1);
}
}
ds.maxFlow(source,sink);
ans+=cont/2;
vector vis(cont,false);
auto dfs = [&](auto &dfs,int nodo)->void{
if(vis[nodo])return;
vis[nodo]=true;
//cout<<nodo<<"\n";
if(!par[nodo])ans--;
for(auto i : ds.adj[nodo]){
if(i.to<cont && i.cap==0){
dfs(dfs,i.to);
}
}
};
for(auto i : ds.adj[sink]){
if(i.cap==0){
//cout<<"Inizio da "<<i.to<<"\n";
dfs(dfs,i.to);
}
}
//cout<<ans<<" dopo "<<x<<"\n";
};
//cout<<ans<<" inizio\n";
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mat[i][j]=='.' && (i+j)%2==0){
check(ind[i][j]);
}
if(ans==inf)break;
}
if(ans==inf)break;
}
assert(ans<=inf);
cout<<ans<<"\n";
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
//cin>>t;
for(int i=1;i<=t;i++)solve(i);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3436kb
input:
3 6 ...#.. ...... #...##
output:
52
result:
ok 1 number(s): "52"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3576kb
input:
2 2 .. ..
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3472kb
input:
2 2 #. #.
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3452kb
input:
4 5 ###.. .###. .##.. #...#
output:
34
result:
ok 1 number(s): "34"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
11 12 .#######..## .##..#.....# #####..##.#. #..##...#... ###..#..##.. ####..###... .#....##..## .#####....## .###..###### .######...## #....##...##
output:
1674
result:
ok 1 number(s): "1674"
Test #6:
score: -100
Wrong Answer
time: 33ms
memory: 3660kb
input:
50 45 ####...#.#####..#..#.#########.##.#####..#..# ##.#.....#..#####....##..###...##.....##....# ##.#...####.##.#..#...####.#....##.###....... ...#...#..#..#.##.######...##..#...###..####. ##.....#.###.####.###..#....##..##..##.###... ..#.##.......##...##.........#..##.###.###... ###..##.###...###....
output:
63500
result:
wrong answer 1st numbers differ - expected: '496312', found: '63500'