Tribal 7-9 (25 points)

Here Insert Picture Description

Sample input:

4
3 10 1 2
2 3 4
4 1 5 7 8
3 9 6 4
2
10 5
3 7

Sample output:

10 2
Y
N

The most basic disjoint-set, with the right path compression

Source:

#include<stdio.h>
#define Max 10010 
int find(int x,int* S)
{
	if(S[x]<0)return x;
	else
	return S[x]=find(S[x],S);
} 
void Union(int x,int y,int* S)
{
	int rx=find(x,S);
	int ry=find(y,S);
	
	if(rx!=ry)
	{
		if(S[rx]<=S[ry])
		{
			S[rx]+=S[ry];
			S[ry]=rx;
		}
		else
		{
			S[ry]+=S[rx];
			S[rx]=ry;
		}
	}
}
int main()
{
	int N,n;
	int count=0;
	int i;
	int circle[Max];
	for(i=0;i<Max;i++)
	{
		circle[i]=-1;
	}
	scanf("%d",&N);

	for(i=0;i<N;i++)
	{
		scanf("%d",&n);
		int t[n+1];
		int j;int index;
		for(j=0;j<n;j++)
		{
			scanf("%d",&t[j]);
			if(t[j]>count)count=t[j];
		}
		for(j=0;j<n-1;j++)
		{
			Union(t[j],t[j+1],circle);
		}
		
	}
	int num=0;
	for(i=1;i<=count;i++)
	{
		if(circle[i]<=0)num++;
	}
	printf("%d %d\n",count,num);
	int q;
	scanf("%d",&q);
	for(i=0;i<q;i++)
	{
		int a,b;
		scanf("%d%d",&a,&b);
		if(find(a,circle)==find(b,circle))
		printf("Y\n");
		else
		printf("N\n");
	}
}
Published 37 original articles · won praise 9 · views 1462

Guess you like

Origin blog.csdn.net/weixin_43301333/article/details/103981328