File Transfer

File Transfer

#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define N 10010

int Fa[N];


void Init()
{
    for(int i=0;iint Find(int x)
{
    int r=x;
    while(r!=Fa[r])
        r=Fa[r];
    int i=x,j;
    while(i!=r)
    {
        j=Fa[i];
        Fa[i]=r;
        i=j;
    }
    return r;

}
int main()
{

    Init();
    int n,a,b,fa,fb;
    char c;
    scanf("%d",&n);
    while(scanf("%c",&c))
    {
        if(c=='C')
        {
            scanf("%d%d",&a,&b);
            fa=Find(a);
            fb=Find(b);
            if(fa!=fb)
                printf("no\n");
            else    
                printf("yes\n");

        }
        if(c=='I')
        {
            scanf("%d%d",&a,&b);
            fa=Find(a);
            fb=Find(b);
            if(fa!=fb)
                Fa[fb]=fa;

        }
        if(c=='S')
        {
            int ans=0;
            for(int i=1;i<=n;i++)
            {
                if(Fa[i]==i)
                    ans++;
            }

            if(ans==1)
                printf("The network is connected.");
            else
                printf("There are %d components.",ans);
            return 0;
        }
    }
    return 0;
}


你可能感兴趣的:(数据结构)