并查集 -------案例4-1.7 文件传输 (25分)

案例4-1.7 文件传输 (25分)
当两台计算机双向连通的时候,文件是可以在两台机器间传输的。给定一套计算机网络,请你判断任意两台指定的计算机之间能否传输文件?

输入格式:
首先在第一行给出网络中计算机的总数 N (2≤N≤10
​4
​​ ),于是我们假设这些计算机从 1 到 N 编号。随后每行输入按以下格式给出:

I c1 c2

其中I表示在计算机c1和c2之间加入连线,使它们连通;或者是

C c1 c2

其中C表示查询计算机c1和c2之间能否传输文件;又或者是

S

这里S表示输入终止。

输出格式:
对每个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<iostream>
#include<set>
using namespace std;

#define M 1004
int parent[M];
set<int>s1;
int find(int v);
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        parent[i]=i;
    }
    char ch;
    while(cin>>ch){
        if(ch=='S'){
            for(int i=1;i<=n;i++){
                s1.insert(find(i));
            }
            if(s1.size()==1){
                cout<<"the network is connected"<<endl;
            }else
                cout<<"there are"<<s1.size()<<"components."<<endl;
            exit(0);
        }
            int x,y;
            cin>>x>>y;
            int l=find(x);
            int r=find(y);
        if(ch=='I'){
            if(l!=r){
                parent[l]=r;
            }
        }
        else{
            if(l==r){
                cout<<"yes"<<endl;
            }else{
                cout<<"no"<<endl;}
        }
    }
    return 0;
}

int find(int v){
    if(v==parent[v]){
        return v;
    }else
        return parent[v]=find(parent[v]);
}

#include<iostream>
using namespace std;

int s[100];

void creatConnection(int* s);
void checkConnection(int* s);
void checkNetwork(int* s,int n);
int find(int* s,int x);
void unionxy(int* s,int x,int y);

int main(){
    int n;
    cin>>n;
    char ch;
    for(int i=1;i<=n;i++){s[i]=i;}
    do{
        cin>>ch;
        switch (ch) {
            case 'I':creatConnection(s);break;
            case 'C':checkConnection(s);break;
            case 'S':checkNetwork(s,n);break;
        }
    }while(ch!='S');
    return 0;
}

void creatConnection(int* s){
    int x,y,root1,root2;
    cin>>x>>y;
    root1=find(s,x);
    root2=find(s,y);
    if(root1!=root2){
        unionxy(s,root1,root2);
    }
}

int find(int* s,int root){
    if(s[root]==root){
        return root;
    }else
        return s[root]=find(s,s[root]);
}

void unionxy(int* s,int root1,int root2){
    s[root1]=root2;
}


void checkConnection(int* s){
    int x,y,root1,root2;
    cin>>x>>y;
    root1=find(s,x);
    root2=find(s,y);
    if(root1==root2){
        cout<<"yes"<<endl;
    }else cout<<"no"<<endl;
}

void checkNetwork(int* s,int n){
    int cnt=0;
    for(int i=1;i<=n;i++){
        if(s[i]==i){
            cnt++;
        }
    }
    if(cnt==1){
        cout<<"The network is connected."<<endl;
    }else
        cout<<"There are "<<cnt<<" components."<<endl;
}

发布了65 篇原创文章 · 获赞 0 · 访问量 1306

猜你喜欢

转载自blog.csdn.net/hellobettershero/article/details/104371983