【PAT 甲级】 A1107 Social Clusters (30分)

我想骂人了
我他吗调了一晚上,第二天才发现,竟然在最后一步出岔子了。

sort(cluster,cluster+1000,cmp);

这条语句错了,应该是1001。我说怎么一直错四个测试点。
结论:只要这道题错最后四个测试点的,都是因为这个1000!!【100%】,我之前看过一篇博文就是这里错的,没想到我在最后排序的时候错了!!!
【版本一:无脑并查集,20分】
【版本二:边界修订,AC30分】

#include <bits/stdc++.h>
using namespace std;
int father[1024];
int person[1024];
int cluster[1024];
bool cmp(int a,int b)
{
	return a>b;
}
int main(void)
{
	int N,m=0;
	memset(father,-1,sizeof(father));
	memset(cluster,0,sizeof(cluster));
	scanf("%d",&N);
	for(int i=0;i<N;i++)
	{
		int n,temp;
		vector<int> num,head;
		scanf("%d:",&n);
		for(int j=0;j<n;j++)
		{
			scanf("%d",&temp);
			num.push_back(temp);
			if(father[temp]!=-1)
			{
				int x=temp;
				while(x!=father[x])
				x=father[x];
				head.push_back(x);
			}
		}
		if(head.size()==0)
		{
			for(int j=0;j<n;j++)
			father[num[j]]=num[0];
		}
		else
		{
			for(int j=0;j<n;j++)
			father[num[j]]=head[0];
			for(int j=0;j<head.size();j++)	//union into head[0]
			father[head[j]]=head[0];
		}
		person[i]=father[num[0]];
	}
	for(int i=0;i<N;i++)
	{
		int x=person[i];
		while(x!=father[x])
		x=father[x];
		cluster[x]++;
	}
	sort(cluster,cluster+1001,cmp);
	while(cluster[m]!=0) m++;
	printf("%d\n",m);
	for(int i=0;i<m;i++)
	{
		printf("%d",cluster[i]);
		if(i!=m-1) printf(" ");
	}
}
发布了15 篇原创文章 · 获赞 1 · 访问量 172

猜你喜欢

转载自blog.csdn.net/weixin_42278063/article/details/104671366
今日推荐