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;
    }

  • 相关阅读:
    C++学习网址
    python学习网址
    python之raw_input()函数
    APP营销模式
    计划任务
    多线程
    Spring Aware
    事件(Application Event)
    Spring 的AOP
    Java配置
  • 原文地址:https://www.cnblogs.com/xuwanghu/p/3024601.html
Copyright © 2011-2022 走看看