并差集

Description

Do you know a software called "WeiXin", a kind of chatting tool? There is an application called "Shaking for a while". Once you shake your cellphone, you can find those who shake their cellphone at same time.

One day when Tom was shaking his cellphone, he thought about a problem: how large the friends group would be if everyone made friends once they find others who shake cellphone at same time and combine their friends group?

We assume all the people are lonely and have no friends at the beginning. In other word, their friends group only contains themselves.

Input Format

First line contains an integer T (0 < T <= 10), indicate there are T cases.

For each case, the first line contains an integer N (0 < N <= 1000), indicate the number of people who would shake their cellphone.

Then follows N lines. Every line comes with an integer K[i] (0 <= K[i] <= 1440), indicate the i-th person shakes cellphone K times a day. Then follows K[i] strings shown as "HH:MM", indicate the time the i-th person shakes cellphone.

Output Format

For each test case, the first line contains an integer X indicate how many friends group amount the people; the second line comes X integers in nondescending order, indicate the scale of each friends group.

Sample Input

1

4

3 00:00 00:01 00:02

2 00:01 00:03

1 00:03

1 00:04

Sample Output

2

1 3

solution:

#include<iostream>
using namespace std;
int father[1444];
int rank[1444];
int hh[1010][1444];
int mm[1010][1444];
void init(int n)
{
	for(int i=1;i<=n;i++)
	{
		father[i]=i;
		rank[i]=1;
	}
}
int find_father(int x)
{
	if(x!=father[x])
	{
		x=find_father(father[x]);
	}
	return father[x];
}
bool same_father(int x,int y)
{
	int fx=find_father(x);
	int fy=find_father(y);
	if(fx==fy)
		return true;
	else 
		return false;
}
void set_union(int x,int y)
{
	int fx=find_father(x);
	int fy=find_father(y);
	father[fy]=fx;
	rank[fx]++;
}
int main()
{
	int i=0;
	int j=0;
	int k[1444]={0};
	int q=0;
	int p=0;
	int n=0;
	int t;
	cin>>t;
	while(i<t)
	{
		cin>>n;
		init(n);
		for(j=1;j<=n;j++)
		{
			cin>>k[j];
			for(q=1;q<=k[j];q++)
			{
				char c;
				cin>>hh[j][q]>>c>>mm[j][q];
			}
		}
		for(j=1;j<=n;j++)
		{
			for(p=j+1;p<=n;p++)
			{
				int flag=false;
				for(int l=1;l<=k[j] && !flag;l++)
				{
					for(int s=1;s<=k[p] && !flag;s++)
					{
						if(hh[j][l]==hh[p][s] && mm[j][l]==mm[p][s])
						{
							flag=true;
							set_union(j,p);
						}
					}
				}
			}
		}
		int sum=1;
		for(j=2;j<=n;j++)
		{
			if(!same_father(j-1,j))
				sum++;
		}
		cout<<sum<<endl;
		int result[1440]={0};
		int s[1440]={0};
		q=1;
		result[q]=father[1];
		for(j=2;j<=n;j++)
		{
			bool b=false;
			for(p=j-1;p>=1;p--)
			{
				if(father[p]==father[j])
				{
					b=true;
					break;
				}
			}
			if(!b)
			{
				result[++q]=father[j];
			}
		}
		for(j=1;j<=sum;j++)
		{
			for(p=1;p<=n;p++)
			{
				if(father[p]==result[j])
					s[j]++;
			}
		}
		for(j=1;j<=sum;j++)
		{
			for(p=j+1;p<=sum;p++)
			{
				if(s[j]>s[p])
				{
					int temp=s[j];
					s[j]=s[p];
					s[p]=temp;
				}
			}
		}
		for(j=1;j<sum;j++)
			cout<<s[j]<<' ';
		cout<<s[sum]<<endl;
		i++;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值