文件传输

在这里插入图片描述
输出格式:
对每个C开头的查询,如果c1和c2之间可以传输文件,就在一行中输出"yes",否则输出"no"。当读到终止符时,在一行中输出"The network is connected.“如果网络中所有计算机之间都能传输文件;或者输出"There are k components.”,其中k是网络中连通集的个数。

输入样例 1:

5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
S

输出样例 1:

no
no
yes

There are 2 components.
输入样例 2:

5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
I 1 3
C 1 5
S

输出样例 2:

no
no
yes
yes
The network is connected.
#include<bits/stdc++.h>
using namespace std;
int n,p[10009];
int find(int x){
	if(x==p[x]) return x;
	return p[x]=find(p[x]);
}

void merge(int x, int y){
	int xx=find(x), yy=find(y);
	if(xx!=yy) p[xx]=yy; 
}
 
int main(){
	cin>>n;
	for(int i=1; i<=n; i++) p[i]=i;
	getchar();
	char ch='a';
	int a,b;
	while(1){
		cin>>ch;
		if(ch=='S') break;
		cin>>a>>b;
		if(ch=='C'){
			if(find(a)==find(b)) cout<<"yes\n";
			else cout<<"no\n";
		}else if(ch=='I'){
			merge(a,b);
		}
	}
	int cnt=0;
	for(int i=1; i<=n; i++){
		if(i==find(i)) cnt++;
	}
	if(cnt==1) cout<<"The network is connected.\n";
	else cout<<"There are "<<cnt<<" components.\n";
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值