File Transfer (25)

这是一道考察“并查集”的题目

并查集只有并和查这两种操作

值得注意的是,因为查的操作是O(height)的,所以我们可以依靠一些小技巧降低树的高度,并且不增加时间复杂度

#include <iostream>
using namespace std;
bool check(int x, int y);
void connect(int x, int y);
int father(int x);

int *a;
int main()
{
    int n;    cin >> n;
    a = (int *)malloc((n + 1)*sizeof(int));
    for (int i = 1; i <= n; i++){
        a[i] = i;
    }

    char get;
    int x, y;
    while ( cin>>get , get != ‘S‘){
        if (get == ‘C‘){
            cin >> x >> y;
            if (check(x, y) == true){
                cout << "yes" << endl;
            }
            else{
                cout << "no" << endl;
            }
        }
        else if (get == ‘I‘){
            cin >> x >> y;
            connect(x, y);
        }
    }

    int count = 0;
    for (int i = 1; i <= n; i++){
        if (a[i] == i){
            count++;
        }
    }
    if (count == 1){
        cout << "The network is connected." << endl;
    }
    else{
        cout << "There are " << count << " components." <<endl;
    }

    cin >> n;
}
bool check(int x, int y)
{
    if (father(x) == father(y)){
        return true;
    }
    else{
        return false;
    }
}
void connect(int x, int y)
{
    int fx = father(x), fy = father(y);
    if (fx != fy){
        a[fy] = fx;
    }
    return;
}
int father(int x)
{
    if (a[x] == x){
        return x;
    }
    return a[x] = father(a[x]);  //这就是巧妙的地方,如果x在树中很深,这种操作使其直接连接到父亲节点上,节省了很多时间
}
时间: 2024-08-10 23:30:12

File Transfer (25)的相关文章

04-树5. File Transfer (25)

04-树5. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it

04-2. File Transfer (25)并查集

04-2. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it

pat04-树5. File Transfer (25)

04-树5. File Transfer (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it

PAT 5-8 File Transfer (25分)

We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other? Input Specification:

PTA 5-8(English) File Transfer (25) - 并查集 - 数组实现

题目:http://pta.patest.cn/pta/test/16/exam/4/question/670 PTA - Data Structures and Algorithms (English) - 5-8 We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer t

05-树8 File Transfer (25分)

题目描述 We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other? Input Specificat

05-树8 File Transfer (25 分)

05-树8 File Transfer (25 分) We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any o

PAT File Transfer

File Transfer We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other? Input S

PAT 05-树7 File Transfer

这次的题让我对选择不同数据结构所产生的结果惊呆了,一开始用的是结构来存储集合,课件上有现成的,而且我也是实在不太会,150ms的时间限制过不去,不得已,看到这题刚好可以用数组,结果7ms最多,有意思!什么是时间复杂度,终于有了一次感性的认识.这题还有个check的要求,我用了一个链表来存储,感觉挺麻烦的,不知是否有更好的方法,题设要求及代码实现如下 1 /* 2 Name: 3 Copyright: 4 Author: 5 Date: 06/04/15 09:46 6 Description: