QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#417830#5202. DominoesmayunfeiTL 803ms6176kbC++143.2kb2024-05-22 23:04:232024-05-22 23:04:23

Judging History

你现在查看的是最新测评结果

  • [2024-05-22 23:04:23]
  • 评测
  • 测评结果:TL
  • 用时:803ms
  • 内存:6176kb
  • [2024-05-22 23:04:23]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define lll __int128
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
ll rint(ll l,ll r){return uniform_int_distribution<ll>(l,r)(rnd);}
const int maxn=1010,N=2010,M=2e4+10,dx[4]={-1,0,0,1},dy[4]={0,-1,1,0};
int n,m,S,T,tot,tot1,pf[maxn][maxn],typ[N],f[N],cnt,fir[N],fir_[N],dep[N],use[N],tim;
char s[maxn][maxn];
struct node{int u,v,w,op,next;}e[M];
void add_edge(int u,int v,int w)
{
    cnt++,e[cnt].u=u,e[cnt].v=v,e[cnt].w=w,e[cnt].next=fir[u],fir[u]=cnt;
    cnt++,e[cnt].u=v,e[cnt].v=u,e[cnt].w=0,e[cnt].next=fir[v],fir[v]=cnt;
    e[fir[u]].op=fir[v],e[fir[v]].op=fir[u];
}
queue<int> q;
bool bfs()
{
    memset(dep+1,0x3f,sizeof(dep[1])*T),q.push(S),dep[S]=1;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=fir[x];i;i=e[i].next)
            if(e[i].w&&dep[e[i].v]>dep[x]+1)
                dep[e[i].v]=dep[x]+1,q.push(e[i].v);
    }
    return dep[T]<1e9;
}
int dfs(int now,int flow)
{
    if(now==T) return flow;
    int res=0;
    for(int &i=fir_[now];i&&flow;i=e[i].next)
    {
        if(e[i].w&&dep[e[i].v]==dep[now]+1)
        {
            int tmp=dfs(e[i].v,min(flow,e[i].w));
            if(!tmp)
            {
                dep[e[i].v]=-1;
                continue;
            }
            flow-=tmp,res+=tmp,e[i].w-=tmp,e[e[i].op].w+=tmp;
            if(!flow) return res;
        }
    }
    return res;
}
int dinic()
{
    int res=0;
    while(bfs()) memcpy(fir_+1,fir+1,sizeof(fir[1])*T),res+=dfs(S,1e9);
    return res;
}
int bfs2(int st)
{
    int res=1;
    use[st]=++tim,q.push(st);
    while(!q.empty())
    {
        int x=q.front();
        if(typ[x]==1&&x!=f[st]) cerr<<st<<"->"<<x<<endl,res++;
        q.pop();
        for(int i=fir[x];i;i=e[i].next)
            if(e[i].w&&use[e[i].v]!=tim&&typ[e[i].v])
                use[e[i].v]=tim,q.push(e[i].v);
    }
    cerr<<st<<":"<<res<<endl;
    return tot1-res;
}
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	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]=='.') pf[i][j]=++tot;
	if(tot>2000) printf("1000000"),exit(0);
	S=tot+1,T=S+1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(s[i][j]=='.')
			{
			    typ[pf[i][j]]=((i+j)&1)+1;
			    if((i+j)&1)
                {
                    add_edge(S,pf[i][j],1);
                    for(int k=0;k<4;k++)
                    {
                        int nx=i+dx[k],ny=j+dy[k];
                        if(s[nx][ny]=='.') add_edge(pf[i][j],pf[nx][ny],1);
                    }
                }
				else add_edge(pf[i][j],T,1),tot1++;
			}
		}
	}
    int myf=dinic(),ans=tot1*(tot1-1);
    for(int i=1;i<=cnt;i++)
        if(e[i].w==0&&typ[e[i].u]==2&&typ[e[i].v]==1)
            f[e[i].u]=e[i].v,f[e[i].v]=e[i].u;
    for(int i=1;i<=tot;i++) cerr<<i<<"<->"<<f[i]<<endl;
    for(int i=1;i<=tot;i++)
    {
        if(typ[i]==2)
        {
            ans+=bfs2(i);
            if(ans>=1e6) printf("1000000"),exit(0);
        }
    }
    printf("%d",ans);
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3748kb

input:

3 6
...#..
......
#...##

output:

52

result:

ok 1 number(s): "52"

Test #2:

score: 0
Accepted
time: 1ms
memory: 6052kb

input:

2 2
..
..

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3848kb

input:

2 2
#.
#.

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 1ms
memory: 5896kb

input:

4 5
###..
.###.
.##..
#...#

output:

34

result:

ok 1 number(s): "34"

Test #5:

score: 0
Accepted
time: 2ms
memory: 6020kb

input:

11 12
.#######..##
.##..#.....#
#####..##.#.
#..##...#...
###..#..##..
####..###...
.#....##..##
.#####....##
.###..######
.######...##
#....##...##

output:

1674

result:

ok 1 number(s): "1674"

Test #6:

score: 0
Accepted
time: 3ms
memory: 5960kb

input:

50 45
####...#.#####..#..#.#########.##.#####..#..#
##.#.....#..#####....##..###...##.....##....#
##.#...####.##.#..#...####.#....##.###.......
...#...#..#..#.##.######...##..#...###..####.
##.....#.###.####.###..#....##..##..##.###...
..#.##.......##...##.........#..##.###.###...
###..##.###...###....

output:

496312

result:

ok 1 number(s): "496312"

Test #7:

score: 0
Accepted
time: 191ms
memory: 6176kb

input:

34 65
...............#####.#..##..############.....###....#..##########
.........#......#.......##..############.##..##........##########
..............#.......#.....##########..............##.##########
...##............#..............######.......#......#..##########
......#....#.....##......#.......

output:

279744

result:

ok 1 number(s): "279744"

Test #8:

score: 0
Accepted
time: 803ms
memory: 6084kb

input:

44 44
............................................
............................................
............................................
............................................
............................................
............................................
...........................

output:

936056

result:

ok 1 number(s): "936056"

Test #9:

score: -100
Time Limit Exceeded

input:

45 45
.............................................
.............................................
.............................................
.............................................
.............................................
.............................................
.....................

output:


result: