QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#152713#6424. GoqzezWA 13ms97868kbC++144.4kb2023-08-28 18:17:232023-08-28 18:17:23

Judging History

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

  • [2023-08-28 18:17:23]
  • 评测
  • 测评结果:WA
  • 用时:13ms
  • 内存:97868kb
  • [2023-08-28 18:17:23]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
	if(x.empty())return out<<"[]";
	out<<'['<<x[0];
	for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
	return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
	cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
	cerr<<x<<' ',debug(y...);
}
const int N=1e3+10,M=N*N,base=1e6+7,mod=1e9+7;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
#define To(i,j,k) \
	for(int k=0,x,y;k<4;k++)\
		if(x=i+dx[k],y=j+dy[k],min(x,y)>=1&&max(x,y)<=n)
int T,n;
char a[N][N];
int id[N][N],ans[N][N];
int k,fa[M],siz[M],is[M];
int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int x,int y){
	x=find(x),y=find(y);
	if(x==y)return;
	siz[x]+=siz[y],fa[y]=x;
}
vector<int>A[M];
void add(int u,int v){
	merge(u,v);
	// debug(u,v);
	A[u].push_back(v),A[v].push_back(u);
}
int now;
void solve1(){
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(a[i][j]=='x'){
				bool flag=0;
				To(i,j,k)flag|=a[x][y]=='.';
				if(!flag){
					To(i,j,k)flag|=a[x][y]=='o'&&is[find(id[x][y])];
				}
				if(flag){
					ans[i][j]=now;
					vector<int>t;
					To(i,j,k)if(a[x][y]=='o')
						t.push_back(find(id[x][y]));
					sort(t.begin(),t.end());
					t.erase(unique(t.begin(),t.end()),t.end());
					// debug(i,j,t);
					for(int x:t)if(!is[x])ans[i][j]-=siz[x];
				}else{
					ans[i][j]=now+1;
				}
			}
		}
	}
}
const int V=M*2;
int m,dft,top,dfn[M],low[M],stk[M];
vector<int>B[V];
void adde(int u,int v){
	// debug("edge",u,v);
	B[u].push_back(v),B[v].push_back(u);
}
void tarjan(int u,int fa=0){
	dfn[u]=low[u]=++dft,stk[++top]=u;
	for(int v:A[u])if(v^fa){
		if(!dfn[v]){
			tarjan(v,u),low[u]=min(low[u],low[v]);
			if(low[v]>=dfn[u]){
				adde(++m,u);
				// debug("stk",ary(stk,1,top),v);
				do adde(stk[top],m);while(stk[top--]^v);
			}
		}else low[u]=min(low[u],dfn[v]);
	}
}
int cnt[V],tot[V],p[V];
void dfs(int u,int fa=0){
	p[u]=fa;
	for(int v:B[u])if(v^fa){
		dfs(v,u);
		tot[u]+=tot[v],cnt[u]+=cnt[v];
	}
}
void solve2(){
	m=k,dft=top=0;
	fill(dfn+1,dfn+1+k,0);
	for(int i=1;i<=k;i++)if(!dfn[i])tarjan(i);
	fill(tot+1,tot+1+m,0);
	for(int i=1;i<=m;i++)tot[i]=i<=k;
	// debug("cnt",ary(cnt,1,m));
	for(int i=1;i<=k;i++)if(fa[i]==i)dfs(i);
	// debug("cnt",ary(cnt,1,m));
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(a[i][j]!='o')continue;
			int u=id[i][j];
			// debug(i,j,u);
			if(!is[find(u)])ans[i][j]=now-1;
			else{
				ans[i][j]=now;
				// debug("ans",i,j,ans[i][j]);
				for(int v:B[u])if(v^p[u]){
					if(!cnt[v])ans[i][j]+=tot[v];
				}
				if(cnt[find(u)]==cnt[u]){
					ans[i][j]+=tot[find(u)]-tot[u];
				}
			}
		}
	}
}
void get(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%s",a[i]+1);
	k=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(a[i][j]=='o')id[i][j]=++k;
			else id[i][j]=0;
			ans[i][j]=0;
		}
	}
	// debug("k",k);
	iota(fa,fa+1+k,0),fill(siz+1,siz+1+k,1),fill(is+1,is+1+k,0);
	// for(int i=1;i<=k;i++)debug(i,A[i]);
	// for(int i=1;i<=n;i++)debug(ary(id[i],1,n));
	for(int i=1;i<=n;i++)for(int j=1;j<n;j++){
		if(a[i][j]=='o'&&a[i][j+1]=='o'){
			add(id[i][j],id[i][j+1]);
		}
	}
	// for(int i=1;i<=k;i++)debug(i,A[i]);
	for(int i=1;i<n;i++)for(int j=1;j<=n;j++){
		if(a[i][j]=='o'&&a[i+1][j]=='o'){
			add(id[i][j],id[i+1][j]);
		}
	}
	// for(int i=1;i<=k;i++)debug(i,A[i]);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			bool flag=0;
			To(i,j,k)flag|=a[x][y]=='.';
			if(flag){
				if(a[i][j]=='o'){
					// debug(i,j);
					cnt[id[i][j]]=1;
					is[find(id[i][j])]=1;
				}
			}
		}
	}
	// debug("fa",ary(fa,1,k));
	// debug("siz",ary(siz,1,k));
	// debug("is",ary(is,1,k));
	now=0;
	for(int i=1;i<=k;i++)if(fa[i]==i)now+=!is[i]*siz[i];
	// debug("now",now);
	// debug(A[4]);
	solve1();
	// return;
	solve2();
	int res=0;
	for(int i=1;i<=n;i++){
		// debug(ary(ans[i],1,n));
		for(int j=1;j<=n;j++){
			if(a[i][j]!='.'){
				res=(1ll*res*base+ans[i][j])%mod;
			}
		}
	}
	printf("%d\n",res);
}
void clr(){
	for(int i=1;i<=k;i++)A[i].clear();
	for(int i=1;i<=m;i++)B[i].clear(),cnt[i]=tot[i]=0;
}
int main(){
	for(scanf("%d",&T);T--;clr())get();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 9ms
memory: 97868kb

input:

3
2
.o
..
3
.x.
xoo
ox.
2
oo
oo

output:

0
870527216
485539347

result:

ok 3 number(s): "0 870527216 485539347"

Test #2:

score: 0
Accepted
time: 10ms
memory: 95936kb

input:

2
4
xoxx
oxo.
oo.o
oo.x
6
.o.xox
oox..x
o.xxxo
o..xxo
xxo.o.
xo...o

output:

409509910
370473030

result:

ok 2 number(s): "409509910 370473030"

Test #3:

score: -100
Wrong Answer
time: 13ms
memory: 97840kb

input:

800
2
.o
xx
7
ooo.oox
oo.ooox
.ooxo..
o.oo.oo
x.ooooo
ooxoooo
xoo.ooo
7
oxxxx.o
oxxxoox
x.o.ox.
xoo.xxo
o.oxxox
xxooxxx
oooxxox
4
o.xo
o...
..xo
oxx.
7
ooooxoo
.xoxx.x
xoxoxxx
oxoxoox
xxxxoxx
ooxoxox
ooxoxox
10
x.ox.xxxxo
xoxxooxoox
xxxxoo.ooo
xxxoxxxoxo
xxx.xoooxx
.oxxooxxxo
xxoooxxxxo
oxooxxxoxx
o...

output:

0
249234238
540967184
0
576468701
764482080
0
0
902304782
819328244
160846442
0
352620216
0
0
370473030
668019878
941153329
21426912
663359588
440560176
995720548
1000007
0
0
9889997
214191552
197876321
64395670
120935390
377081136
565792142
295776125
0
135122993
203830701
8109391
158328425
0
0
0
35...

result:

wrong answer 28th numbers differ - expected: '788820559', found: '197876321'