QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#303581 | #5202. Dominoes | LaStataleBlue | AC ✓ | 746ms | 9048kb | C++23 | 4.7kb | 2024-01-12 19:08:35 | 2024-01-12 19:08:35 |
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,-1));
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++;
}
}
}
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;
}
ans=min(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;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3600kb
input:
3 6 ...#.. ...... #...##
output:
52
result:
ok 1 number(s): "52"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
2 2 .. ..
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3776kb
input:
2 2 #. #.
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
4 5 ###.. .###. .##.. #...#
output:
34
result:
ok 1 number(s): "34"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3636kb
input:
11 12 .#######..## .##..#.....# #####..##.#. #..##...#... ###..#..##.. ####..###... .#....##..## .#####....## .###..###### .######...## #....##...##
output:
1674
result:
ok 1 number(s): "1674"
Test #6:
score: 0
Accepted
time: 86ms
memory: 3752kb
input:
50 45 ####...#.#####..#..#.#########.##.#####..#..# ##.#.....#..#####....##..###...##.....##....# ##.#...####.##.#..#...####.#....##.###....... ...#...#..#..#.##.######...##..#...###..####. ##.....#.###.####.###..#....##..##..##.###... ..#.##.......##...##.........#..##.###.###... ###..##.###...###....
output:
496312
result:
ok 1 number(s): "496312"
Test #7:
score: 0
Accepted
time: 198ms
memory: 3800kb
input:
34 65 ...............#####.#..##..############.....###....#..########## .........#......#.......##..############.##..##........########## ..............#.......#.....##########..............##.########## ...##............#..............######.......#......#..########## ......#....#.....##......#.......
output:
279744
result:
ok 1 number(s): "279744"
Test #8:
score: 0
Accepted
time: 297ms
memory: 4076kb
input:
44 44 ............................................ ............................................ ............................................ ............................................ ............................................ ............................................ ...........................
output:
936056
result:
ok 1 number(s): "936056"
Test #9:
score: 0
Accepted
time: 746ms
memory: 4352kb
input:
45 45 ............................................. ............................................. ............................................. ............................................. ............................................. ............................................. .....................
output:
999000
result:
ok 1 number(s): "999000"
Test #10:
score: 0
Accepted
time: 459ms
memory: 4268kb
input:
59 59 ...#.......#.......#...#...#...................#........... .#.#.#####.#.#####.#.#.#.#.#.#################.#.#########. .#.#.#.....#.#...#.#.#.#.#.#.#...............#.#.#...#...#. .#.#.#.#####.#.#.#.#.#.#.#.#.#.#############.#.#.#.#.#.#.#. .#.#.#.#...#.#.#.#...#...#...#.#...#.........#...#.#.#...
output:
809100
result:
ok 1 number(s): "809100"
Test #11:
score: 0
Accepted
time: 580ms
memory: 4088kb
input:
39 99 ...#.......#...#...................#...#...#...#...#...........#...#.......#....................... .#.#.#####.#.#.#.#################.#.#.#.#.#.#.#.#.#.#########.#.#.#.#####.#.#####################. .#.#.....#.#.#.#.........#.........#.#.#.#.#.#.#.#.#.#...#.....#.#.#.#.....#.....#.......#...#...
output:
999000
result:
ok 1 number(s): "999000"
Test #12:
score: 0
Accepted
time: 493ms
memory: 4092kb
input:
99 39 .......#.......#....................... .#####.#.#####.#.#####################. .....#.#.....#.#.#.......#............. ####.#.#####.#.#.#.#####.#.############ .....#.#.....#...#.#.....#.#........... .#####.#.#########.#.#####.#.#########. .....#.#.....#...#.#.....#.#.....#..... ####.#.#####.#...
output:
999000
result:
ok 1 number(s): "999000"
Test #13:
score: 0
Accepted
time: 658ms
memory: 4156kb
input:
45 45 #.......................................###.. .........................................##.. ............................................. ............................................. ............................................. ............................................. .....................
output:
999000
result:
ok 1 number(s): "999000"
Test #14:
score: 0
Accepted
time: 2ms
memory: 4084kb
input:
132 453 ###########################################################..####################..###################################################################.#################################################..############################..############################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #15:
score: 0
Accepted
time: 59ms
memory: 4088kb
input:
312 14 ##........#... ##............ ...#...#...... .............. .............. ......#....... .............. ......##...... .............. ...#.......... .............. .............. .............. .............. .............. .............. .............. .............. .............. ...........
output:
1000000
result:
ok 1 number(s): "1000000"
Test #16:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
1 2 ..
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
2 1 . .
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 54ms
memory: 3784kb
input:
1 1000 ........................................................................................................................................................................................................................................................................................................
output:
374250
result:
ok 1 number(s): "374250"
Test #19:
score: 0
Accepted
time: 50ms
memory: 3972kb
input:
1000 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
output:
374250
result:
ok 1 number(s): "374250"
Test #20:
score: 0
Accepted
time: 11ms
memory: 9048kb
input:
1000 1000 ###############################################################################################.##################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #21:
score: 0
Accepted
time: 14ms
memory: 8928kb
input:
1000 1000 ##################################################################################################################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #22:
score: 0
Accepted
time: 9ms
memory: 8072kb
input:
1000 1000 ##################################################################################################################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #23:
score: 0
Accepted
time: 7ms
memory: 8060kb
input:
1000 1000 ##################################################################################################################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #24:
score: 0
Accepted
time: 8ms
memory: 8144kb
input:
999 999 .......#...............#...................................#.......#...............#.......#...............#.......#...#...#...............................#...#...........#.......#...........................#.......#...........#...........#.......#...#.......#.......#...#...#...................
output:
1000000
result:
ok 1 number(s): "1000000"