QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#162794 | #5202. Dominoes | karuna# | AC ✓ | 29ms | 22988kb | C++17 | 2.7kb | 2023-09-03 16:48:08 | 2023-09-03 16:48:08 |
Judging History
answer
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 3000;
int N, M, K;
char S[MAXN+10][MAXN+10];
int A[MAXN+10][MAXN+10];
struct Edge
{
int v, c, r;
};
vector<Edge> adj[MAXN+10];
void addEdge(int u, int v, int c)
{
adj[u].push_back({v, c, adj[v].size()});
adj[v].push_back({u, 0, adj[u].size()-1});
}
int lvl[MAXN+10];
bool bfs()
{
queue<int> Q;
for(int i=0; i<=K+K+1; i++) lvl[i]=0;
lvl[0]=1;
Q.push(0);
while(!Q.empty())
{
int now=Q.front(); Q.pop();
for(auto &[nxt, c, r] : adj[now])
{
if(lvl[nxt]) continue;
if(!c) continue;
Q.push(nxt);
lvl[nxt]=lvl[now]+1;
}
}
return lvl[K+K+1];
}
int pos[MAXN+10];
bool dfs(int now)
{
if(now==K+K+1) return true;
for(; pos[now]<adj[now].size(); pos[now]++)
{
auto &[nxt, c, r] = adj[now][pos[now]];
if(!c) continue;
if(lvl[nxt]!=lvl[now]+1) continue;
if(dfs(nxt))
{
c=0;
adj[nxt][r].c=1;
return true;
}
}
return false;
}
int X[MAXN+10];
vector<int> adj2[MAXN+10];
bool vis[MAXN+10];
void dfs2(int now)
{
vis[now]=1;
for(int nxt : adj2[now])
{
if(vis[nxt]) continue;
dfs2(nxt);
}
}
int ans;
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
scanf("%d%d", &N, &M);
for(int i=1; i<=N; i++) scanf(" %s", S[i]+1);
int p=0, q=0;
for(int i=1; i<=N; i++) for(int j=1; j<=M; j++)
{
if(S[i][j]=='#') continue;
if((i+j)%2==0) A[i][j]=++p;
else A[i][j]=++q;
}
for(int i=1; i<=N; i++) for(int j=1; j<=M; j++) if(S[i][j]=='.' && (i+j)%2) A[i][j]+=p;
assert(p==q);
if(p>1000) return !printf("1000000\n");
K=p;
ans=K*(K-1);
for(int i=1; i<=N; i++)
{
for(int j=1; j<=M; j++)
{
if(A[i][j]==0) continue;
if(i+1<=N && A[i+1][j]!=0)
{
int u=A[i][j], v=A[i+1][j];
if(u>v) swap(u, v);
addEdge(u, v, 1);
}
if(j+1<=M && A[i][j+1]!=0)
{
int u=A[i][j], v=A[i][j+1];
if(u>v) swap(u, v);
addEdge(u, v, 1);
}
}
}
for(int i=1; i<=K; i++) addEdge(0, i, 1), addEdge(i+K, K+K+1, 1);
while(bfs())
{
for(int i=0; i<=K+K+1; i++) pos[i]=0;
while(dfs(0));
}
for(int i=K+1; i<=K+K; i++)
{
for(auto &[nxt, c, r] : adj[i])
{
if(nxt==K+K+1) continue;
if(c==1) X[i]=nxt, X[nxt]=i;
}
for(auto &[nxt, c, r] : adj[i])
{
if(nxt==K+K+1) continue;
if(c==0) adj2[X[i]].push_back(nxt);
}
}
for(int i=1; i<=K; i++)
{
for(int j=1; j<=K; j++) vis[j]=0;
dfs2(i);
for(int j=1; j<=K; j++) if(!vis[j]) ans++;
}
ans=min(ans, 1000000);
printf("%d\n", ans);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 6372kb
input:
3 6 ...#.. ...... #...##
output:
52
result:
ok 1 number(s): "52"
Test #2:
score: 0
Accepted
time: 2ms
memory: 6080kb
input:
2 2 .. ..
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 2ms
memory: 6064kb
input:
2 2 #. #.
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 2ms
memory: 6152kb
input:
4 5 ###.. .###. .##.. #...#
output:
34
result:
ok 1 number(s): "34"
Test #5:
score: 0
Accepted
time: 2ms
memory: 6052kb
input:
11 12 .#######..## .##..#.....# #####..##.#. #..##...#... ###..#..##.. ####..###... .#....##..## .#####....## .###..###### .######...## #....##...##
output:
1674
result:
ok 1 number(s): "1674"
Test #6:
score: 0
Accepted
time: 3ms
memory: 6316kb
input:
50 45 ####...#.#####..#..#.#########.##.#####..#..# ##.#.....#..#####....##..###...##.....##....# ##.#...####.##.#..#...####.#....##.###....... ...#...#..#..#.##.######...##..#...###..####. ##.....#.###.####.###..#....##..##..##.###... ..#.##.......##...##.........#..##.###.###... ###..##.###...###....
output:
496312
result:
ok 1 number(s): "496312"
Test #7:
score: 0
Accepted
time: 5ms
memory: 6340kb
input:
34 65 ...............#####.#..##..############.....###....#..########## .........#......#.......##..############.##..##........########## ..............#.......#.....##########..............##.########## ...##............#..............######.......#......#..########## ......#....#.....##......#.......
output:
279744
result:
ok 1 number(s): "279744"
Test #8:
score: 0
Accepted
time: 6ms
memory: 8408kb
input:
44 44 ............................................ ............................................ ............................................ ............................................ ............................................ ............................................ ...........................
output:
936056
result:
ok 1 number(s): "936056"
Test #9:
score: 0
Accepted
time: 12ms
memory: 8392kb
input:
45 45 ............................................. ............................................. ............................................. ............................................. ............................................. ............................................. .....................
output:
999000
result:
ok 1 number(s): "999000"
Test #10:
score: 0
Accepted
time: 8ms
memory: 6596kb
input:
59 59 ...#.......#.......#...#...#...................#........... .#.#.#####.#.#####.#.#.#.#.#.#################.#.#########. .#.#.#.....#.#...#.#.#.#.#.#.#...............#.#.#...#...#. .#.#.#.#####.#.#.#.#.#.#.#.#.#.#############.#.#.#.#.#.#.#. .#.#.#.#...#.#.#.#...#...#...#.#...#.........#...#.#.#...
output:
809100
result:
ok 1 number(s): "809100"
Test #11:
score: 0
Accepted
time: 9ms
memory: 6716kb
input:
39 99 ...#.......#...#...................#...#...#...#...#...........#...#.......#....................... .#.#.#####.#.#.#.#################.#.#.#.#.#.#.#.#.#.#########.#.#.#.#####.#.#####################. .#.#.....#.#.#.#.........#.........#.#.#.#.#.#.#.#.#.#...#.....#.#.#.#.....#.....#.......#...#...
output:
999000
result:
ok 1 number(s): "999000"
Test #12:
score: 0
Accepted
time: 10ms
memory: 10676kb
input:
99 39 .......#.......#....................... .#####.#.#####.#.#####################. .....#.#.....#.#.#.......#............. ####.#.#####.#.#.#.#####.#.############ .....#.#.....#...#.#.....#.#........... .#####.#.#########.#.#####.#.#########. .....#.#.....#...#.#.....#.#.....#..... ####.#.#####.#...
output:
999000
result:
ok 1 number(s): "999000"
Test #13:
score: 0
Accepted
time: 7ms
memory: 6588kb
input:
45 45 #.......................................###.. .........................................##.. ............................................. ............................................. ............................................. ............................................. .....................
output:
999000
result:
ok 1 number(s): "999000"
Test #14:
score: 0
Accepted
time: 2ms
memory: 8588kb
input:
132 453 ###########################################################..####################..###################################################################.#################################################..############################..############################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #15:
score: 0
Accepted
time: 12ms
memory: 9264kb
input:
312 14 ##........#... ##............ ...#...#...... .............. .............. ......#....... .............. ......##...... .............. ...#.......... .............. .............. .............. .............. .............. .............. .............. .............. .............. ...........
output:
1000000
result:
ok 1 number(s): "1000000"
Test #16:
score: 0
Accepted
time: 1ms
memory: 6352kb
input:
1 2 ..
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 0
Accepted
time: 1ms
memory: 6324kb
input:
2 1 . .
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 2ms
memory: 6188kb
input:
1 1000 ........................................................................................................................................................................................................................................................................................................
output:
374250
result:
ok 1 number(s): "374250"
Test #19:
score: 0
Accepted
time: 1ms
memory: 20908kb
input:
1000 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
output:
374250
result:
ok 1 number(s): "374250"
Test #20:
score: 0
Accepted
time: 5ms
memory: 22988kb
input:
1000 1000 ###############################################################################################.##################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #21:
score: 0
Accepted
time: 29ms
memory: 10704kb
input:
1000 1000 ##################################################################################################################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #22:
score: 0
Accepted
time: 5ms
memory: 16228kb
input:
1000 1000 ##################################################################################################################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #23:
score: 0
Accepted
time: 0ms
memory: 21140kb
input:
1000 1000 ##################################################################################################################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #24:
score: 0
Accepted
time: 8ms
memory: 20700kb
input:
999 999 .......#...............#...................................#.......#...............#.......#...............#.......#...#...#...............................#...#...........#.......#...........................#.......#...........#...........#.......#...#.......#.......#...#...#...................
output:
1000000
result:
ok 1 number(s): "1000000"