求找错1
#include<bits/stdc++.h> using namespace std; int fa[50005],a[50005]; char str[305][305]; int n,T,temp; map<int,int> mp; int getfa(int x){ //cout<<x<<endl; if(!x) return 0; if(fa[x]==x) return x; return fa[x]=getfa(fa[x]); } int main(){ cin>>T; ios::sync_with_stdio(0); while(T--){ cin>>n; memset(fa,0,sizeof(fa)); for(int i=1;i<=n;i++) scanf("%s",str[i]+1); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { fa[(i-1)*n+j]=(i-1)*n+j; } mp.clear(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(str[i][j]=='1') { if(str[i][j]=='1' && str[i-1][j]=='1') fa[getfa((i-1)*n+j)]=(i-2)*n+j; if(str[i][j]=='1'&& str[i][j-1]=='1' && (getfa((i-1)*n+j))!=getfa((i-1)*n+j-1)) fa[getfa((i-1)*n+j)]=(i-1)*n+j-1; mp[getfa((i-1)*n+j)]++; } temp=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(str[i][j]=='0'){ int s=0,x=0,z=0,y=0; if((i!=1)&&(str[i-1][j]=='1'))s=(i-2)*n+j; if((j!=1)&&(str[i][j-1]=='1'))z=(i-1)*n+j-1; if((j!=n)&&(str[i][j+1]=='1')) y=(i-1)*n+j+1; if((i!=n)&&(str[i+1][j]=='1')) x=i*n+j; s=getfa(s);z=getfa(z);y=getfa(y);x=getfa(x); if(s==z) z=0;if(y==s||y==z) y=0;if(x==s||x==z||x==y) x=0; temp++; if(s) a[temp]+=mp[s]; if(x) a[temp]+=mp[x]; if(z) a[temp]+=mp[z]; if(y) a[temp]+=mp[y]; } sort(a+1,a+1+temp); cout<<temp<<endl; for(int i=1;i<=temp;i++) cout<<a[i]<<' '; cout<<endl; } }