感觉自己......
写了个并查集炸了然后打了个暴力竟然a了
并查集代码:
#include<bits/stdc++.h>
#define rint register int
#define fu(i,a,b,del,con) for(rint i=a;i<=b&&con;i+=del)
#define fd(i,a,b,del,con) for(rint i=a;i>=b&&con;i-=del)
using namespace std;
const int way[8][2]={{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1},{1,0},{-1,0}};
int g[1502][1502],f[1500*1500+1];
int find(int x){
if(f[x]!=x)f[x]=find(f[x]);
return f[x];
}
int n,m,x[1500*1500+1],y[1500*1500+1],num[1500*1500+1],sum[1500*1500+1],galaxies,maxsize;
int main(){
scanf("%d%d\n",&n,&m);
fu(i,1,n,1,1){
fu(j,1,m,1,1)if(getchar()=='*')x[++g[0][0]]=i,y[g[0][0]]=j,f[g[0][0]]=g[0][0],g[i][j]=g[0][0];
getchar();
}
fu(i,1,g[0][0],1,1){
fu(p,0,7,1,1){
int di=g[x[i]+way[p][0]][y[i]+way[p][1]];
if(di==0)continue;
else f[di]=find(i);
}
}
int tmp=0;
fu(i,1,g[0][0],1,1)num[find(f[i])]++;
fu(i,1,g[0][0],1,1)sum[num[i]]++,tmp=max(tmp,num[i]);
fu(i,1,tmp,1,1){
if(sum[i]!=0)galaxies++;
if(sum[i]*i>maxsize)maxsize=sum[i]*i;
}
printf("%d %d",galaxies,maxsize);
}
下面是暴力,不是很懂为什么过了,大佬们帮忙讲一下
#include<bits/stdc++.h>
#define rint register int
#define fu(i,a,b,del,con) for(rint i=a;i<=b&&con;i+=del)
#define fd(i,a,b,del,con) for(rint i=a;i>=b&&con;i-=del)
using namespace std;
const int way[8][2]={{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1},{1,0},{-1,0}};
int n,m,galaxies,maxsize;
char ma[1501][1501];
int a[1501][1501],tmpsize,tmpgroup;
vector<pair<int,int> > sizes;
void dfs(int x,int y){
a[x][y]=tmpgroup;
fu(p,0,7,1,1){
int dx=x+way[p][0],dy=y+way[p][1];
if(x<1||x>n||y<1||y>m)continue;
if(ma[dx][dy]=='*'&&!a[dx][dy]){
tmpsize++;
dfs(dx,dy);
}
}
}
bool cmp(pair<int,int> x,pair<int,int> y){
return x.second!=y.second?x.second>y.second:x.first<y.first;
}
int main(){
// freopen("star.in","r",stdin);
// freopen("star.out","w",stdout);
scanf("%d%d\n",&n,&m);
fu(i,1,n,1,1){
fu(j,1,m,1,1)
if(getchar()=='*'){
ma[i][j]='*';
}
getchar();
}
fu(i,1,n,1,1){
fu(j,1,m,1,1){
if(ma[i][j]=='*'&&!a[i][j]){
tmpgroup++;
tmpsize=1;
dfs(i,j);
sizes.push_back(make_pair(tmpgroup,tmpsize));
}
}
}
sort(sizes.begin(),sizes.end(),cmp);
int tmp=sizes[0].second;
tmpsize=sizes[0].second;
galaxies=1;
fu(i,1,sizes.size()-1,1,1){
if(sizes[i].second==tmp){
tmpsize+=sizes[i].second;
}
else{
maxsize=max(maxsize,tmpsize);
galaxies++;
tmpsize=sizes[i].second;
tmp=sizes[i].second;
}
}
maxsize=max(maxsize,tmpsize);
printf("%d %d",galaxies,maxsize);
}
大佬帮我解答一下并查集哪里错了,以及这个暴力写的和*一样咋就过了。谢谢大佬了。
不过我online炸了