题目

题意:如果I,a,b:表示a,b之间建立联系,C,a,b:表示查询a,b之间是否有联系,S表示结束操作,a,b之间可以间接连接

tip:并查集

#include<iostream>
using namespace std;
int father[10003];
void init() {
	for(int i=0; i<10003; ++i)
		father[i]=i;
}
int find(int x) {
	return x==father[x]?father[x]:father[x]=find(father[x]);
}
void Uion(int a,int b) {
	int x=find(a);
	int y=find(b);
	if(x!=y)
		father[x]=y;
}
int main() {
	init();
	int n;
	cin>>n;
	string query;
	cin>>query;
	while(query!="S") {
		int a,b;
		cin>>a>>b;
		if(query=="C") {
			if(find(a)==find(b))
				cout<<"yes\n";
			else cout<<"no\n";
		} else if(query=="I")
			Uion(a,b);
		cin>>query;
	}
	int count=0;
	for(int i=1; i<=n; ++i)
		if(father[i]==i)
			count++;
	if(count==1)
		cout<<"The network is connected.\n";
	else cout<<"There are "<<count<<" components.\n";
	return 0;
}