zoukankan      html  css  js  c++  java
  • How Many Tables

    #include <iostream>
    #include <string>
    using namespace std;

    const int MAX = 1005;

    int father[MAX], rank[MAX];
    int n, m;

    void MakeSet()
    {
        for (int i = 1; i <= n; i++)
        {
            father[i] = i;
        }
    }

    int FindSet(int x)
    {
        if (x != father[x])
        {
            father[x] = FindSet(father[x]);
        }
        return father[x];
    }

     
    bool Union(int x, int y)
    {
        x = FindSet(x);
        y = FindSet(y);
        if (x == y) return false;
        father[y] = x;
        return true;  
    }

    int main()
    {
        int T, i, cnt;
        int x, y;
        cin >> T;  
        while (T--)
        {
            cin >> n >> m;
            MakeSet();  
            cnt = 0;
            for (i = 1; i <= m; i++)
            {
                cin >> x >> y;
                if (Union(x, y))
                {
                    cnt++;
                }
            }
            cout << n-cnt <<endl;
        }
        return 0;
    }

  • 相关阅读:
    分享一个详情页
    ES6初探,变量的声明
    ES6初探,什么是ES6
    提问回顾
    个人阅读&个人总结
    结对项目-数独程序扩展
    个人作业Week3-案例分析
    个人作业Week2-代码复审
    个人作业1
    【个人项目】数独
  • 原文地址:https://www.cnblogs.com/xuwanghu/p/3024601.html
Copyright © 2011-2022 走看看