QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#80459 | #5202. Dominoes | lijunyi | AC ✓ | 22ms | 9300kb | C++20 | 2.9kb | 2023-02-23 20:41:44 | 2023-02-23 20:41:46 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=8000;
const int dx[5]={0,1,-1,0,0};
const int dy[5]={0,0,0,1,-1};
int n,m,x,y;
int u,v,d;
int match[maxn],vis[maxn];
int tot=0,pre[maxn],son[maxn],now[maxn];
int id[1200][1200],ok[maxn],cnt=0,sum=0,del[maxn];
int to[maxn],tmp[maxn];
char s[1200][1200];
void put(int x,int y)
{
pre[++tot]=now[x];
now[x]=tot;
son[tot]=y;
}
int dfs(int x,int tag)
{
if(vis[x]==tag)
return 0;
vis[x]=tag;
for(int p=now[x];p;p=pre[p])
{
int t=son[p];
if(del[t])
continue;
if(!match[t]||dfs(match[t],tag))
{
match[t]=x,match[x]=t;
return 1;
}
}
return 0;
}
void calc(int x,int tag)
{
if(del[x]||vis[x]==tag)
return ;
vis[x]=tag;sum--;
for(int p=now[x];p;p=pre[p])
{
int t=son[p];
if(del[t])
continue;
calc(match[t],tag);
}
}
int get(int x,int tag)
{
if(vis[x]==tag)
return 0;
if(!match[x])
return 1;
vis[x]=tag;
for(int p=now[x];p;p=pre[p])
{
int t=son[p];
if(get(match[t],tag))
return 1;
}
return 0;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",s[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(s[i][j]=='.')
{
id[i][j]=++cnt,ok[cnt]=((i+j)&1);
if((i+j)&1)
x++;
else
y++;
}
if((x+y)%2==1||abs(x-y)>=3)
return printf("%lld\n",min(1000000ll,1ll*(x+y)*(x+y-1)/2)),0;
if(x>=1001)
return puts("1000000"),0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(s[i][j]=='.')
for(int k=1;k<=4;k++)
{
int p=i+dx[k],q=j+dy[k];
if(p&&q&&p<=n&&q<=m&&s[p][q]=='.')
put(id[i][j],id[p][q]);
}
int ans=0;
for(int i=1;i<=cnt;i++)
if(ok[i]&&dfs(i,i))
ans++;
if(x+y-2*ans>2)
return printf("%lld\n",min(1000000ll,1ll*(x+y)*(x+y-1)/2)),0;
if(x==y&&ans==x)
{
sum=x*(x-1);
for(int i=1;i<=cnt;i++)
if(ok[i])
{
sum+=y;
int t=match[i];
del[i]=1;match[i]=match[t]=0;
calc(t,1e9-i);
del[i]=0;match[i]=t,match[t]=i;
}
printf("%d\n",min(1000000,sum));
}
if(x==y&&ans==x-1)
{
sum=x*(x*2-1);
for(int i=1;i<=cnt;i++)
if(match[i])
to[i]=get(i,1e9-i);
else
to[i]=1;
int ct=0;
for(int i=1;i<=cnt;i++)
if(!ok[i])
ct+=to[i];
for(int i=1;i<=cnt;i++)
if(ok[i]&&to[i])
sum-=ct;
printf("%d\n",min(1000000,sum));
}
if(abs(x-y)==2)
{
if(x>=820)
return puts("1000000"),0;
int sum=1ll*(x+y)*(x+y-1)/2,p=1,po=1e9;
if(x==y-2)
p=0;
for(int i=1;i<=cnt;i++)
if(ok[i]==p)
for(int j=i+1;j<=cnt;j++)
if(ok[j]==p)
{
for(int k=1;k<=cnt;k++)
tmp[k]=match[k];
del[i]=del[j]=1;
int p=match[i],q=match[j];
match[p]=match[q]=0;
if((!p||dfs(p,po--))&&(!q||dfs(q,po--)))
sum--;
for(int k=1;k<=cnt;k++)
match[k]=tmp[k];
del[i]=del[j]=0;
}
printf("%d\n",min(1000000,sum));
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3628kb
input:
3 6 ...#.. ...... #...##
output:
52
result:
ok 1 number(s): "52"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3524kb
input:
2 2 .. ..
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3628kb
input:
2 2 #. #.
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 2ms
memory: 3568kb
input:
4 5 ###.. .###. .##.. #...#
output:
34
result:
ok 1 number(s): "34"
Test #5:
score: 0
Accepted
time: 2ms
memory: 3688kb
input:
11 12 .#######..## .##..#.....# #####..##.#. #..##...#... ###..#..##.. ####..###... .#....##..## .#####....## .###..###### .######...## #....##...##
output:
1674
result:
ok 1 number(s): "1674"
Test #6:
score: 0
Accepted
time: 2ms
memory: 3904kb
input:
50 45 ####...#.#####..#..#.#########.##.#####..#..# ##.#.....#..#####....##..###...##.....##....# ##.#...####.##.#..#...####.#....##.###....... ...#...#..#..#.##.######...##..#...###..####. ##.....#.###.####.###..#....##..##..##.###... ..#.##.......##...##.........#..##.###.###... ###..##.###...###....
output:
496312
result:
ok 1 number(s): "496312"
Test #7:
score: 0
Accepted
time: 7ms
memory: 3784kb
input:
34 65 ...............#####.#..##..############.....###....#..########## .........#......#.......##..############.##..##........########## ..............#.......#.....##########..............##.########## ...##............#..............######.......#......#..########## ......#....#.....##......#.......
output:
279744
result:
ok 1 number(s): "279744"
Test #8:
score: 0
Accepted
time: 14ms
memory: 3940kb
input:
44 44 ............................................ ............................................ ............................................ ............................................ ............................................ ............................................ ...........................
output:
936056
result:
ok 1 number(s): "936056"
Test #9:
score: 0
Accepted
time: 18ms
memory: 3944kb
input:
45 45 ............................................. ............................................. ............................................. ............................................. ............................................. ............................................. .....................
output:
999000
result:
ok 1 number(s): "999000"
Test #10:
score: 0
Accepted
time: 8ms
memory: 4188kb
input:
59 59 ...#.......#.......#...#...#...................#........... .#.#.#####.#.#####.#.#.#.#.#.#################.#.#########. .#.#.#.....#.#...#.#.#.#.#.#.#...............#.#.#...#...#. .#.#.#.#####.#.#.#.#.#.#.#.#.#.#############.#.#.#.#.#.#.#. .#.#.#.#...#.#.#.#...#...#...#.#...#.........#...#.#.#...
output:
809100
result:
ok 1 number(s): "809100"
Test #11:
score: 0
Accepted
time: 15ms
memory: 3916kb
input:
39 99 ...#.......#...#...................#...#...#...#...#...........#...#.......#....................... .#.#.#####.#.#.#.#################.#.#.#.#.#.#.#.#.#.#########.#.#.#.#####.#.#####################. .#.#.....#.#.#.#.........#.........#.#.#.#.#.#.#.#.#.#...#.....#.#.#.#.....#.....#.......#...#...
output:
999000
result:
ok 1 number(s): "999000"
Test #12:
score: 0
Accepted
time: 16ms
memory: 4200kb
input:
99 39 .......#.......#....................... .#####.#.#####.#.#####################. .....#.#.....#.#.#.......#............. ####.#.#####.#.#.#.#####.#.############ .....#.#.....#...#.#.....#.#........... .#####.#.#########.#.#####.#.#########. .....#.#.....#...#.#.....#.#.....#..... ####.#.#####.#...
output:
999000
result:
ok 1 number(s): "999000"
Test #13:
score: 0
Accepted
time: 16ms
memory: 3992kb
input:
45 45 #.......................................###.. .........................................##.. ............................................. ............................................. ............................................. ............................................. .....................
output:
999000
result:
ok 1 number(s): "999000"
Test #14:
score: 0
Accepted
time: 2ms
memory: 4608kb
input:
132 453 ###########################################################..####################..###################################################################.#################################################..############################..############################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #15:
score: 0
Accepted
time: 20ms
memory: 4916kb
input:
312 14 ##........#... ##............ ...#...#...... .............. .............. ......#....... .............. ......##...... .............. ...#.......... .............. .............. .............. .............. .............. .............. .............. .............. .............. ...........
output:
1000000
result:
ok 1 number(s): "1000000"
Test #16:
score: 0
Accepted
time: 1ms
memory: 3636kb
input:
1 2 ..
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 0
Accepted
time: 2ms
memory: 3796kb
input:
2 1 . .
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 1ms
memory: 3836kb
input:
1 1000 ........................................................................................................................................................................................................................................................................................................
output:
374250
result:
ok 1 number(s): "374250"
Test #19:
score: 0
Accepted
time: 6ms
memory: 8968kb
input:
1000 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
output:
374250
result:
ok 1 number(s): "374250"
Test #20:
score: 0
Accepted
time: 16ms
memory: 8208kb
input:
1000 1000 ###############################################################################################.##################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #21:
score: 0
Accepted
time: 22ms
memory: 5376kb
input:
1000 1000 ##################################################################################################################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #22:
score: 0
Accepted
time: 4ms
memory: 5960kb
input:
1000 1000 ##################################################################################################################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #23:
score: 0
Accepted
time: 5ms
memory: 9200kb
input:
1000 1000 ##################################################################################################################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #24:
score: 0
Accepted
time: 4ms
memory: 9300kb
input:
999 999 .......#...............#...................................#.......#...............#.......#...............#.......#...#...#...............................#...#...........#.......#...........................#.......#...........#...........#.......#...#.......#.......#...#...#...................
output:
1000000
result:
ok 1 number(s): "1000000"