zoukankan      html  css  js  c++  java
  • poj2524

    简单并查集

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    using namespace std;

    #define maxn 50004

    int father[maxn];

    int getanc(int a)
    {
    if (father[a] == a)
    return a;
    return father[a] = getanc(father[a]);
    }

    void merge(int a, int b)
    {
    if (father[a] == -1)
    father[a]
    = a;
    if (father[b] == -1)
    father[b]
    = b;
    father[getanc(a)]
    = getanc(b);
    }

    int main()
    {
    // freopen("t.txt", "r", stdin);
    int t = 0;
    int n, m;
    while (scanf("%d%d", &n, &m), n | m)
    {
    memset(father,
    -1, sizeof(father));
    t
    ++;
    for (int i = 0; i < m; i++)
    {
    int a, b;
    scanf(
    "%d%d", &a, &b);
    a
    --;
    b
    --;
    merge(a, b);
    }
    int ans = 0;
    for (int i = 0; i < n; i++)
    if (father[i] == -1 || father[i] == i)
    ans
    ++;
    printf(
    "Case %d: %d\n", t, ans);
    }
    return 0;
    }

  • 相关阅读:
    TinyOS在ubuntu 14.04下安装教程
    C++ STL标准入门
    C++ 模板
    多态
    C++继承
    C++类型转换 -- 由其他类型转换到自定义类型
    运算符重载
    友元
    typedef用法
    c++细节--section1
  • 原文地址:https://www.cnblogs.com/rainydays/p/2049201.html
Copyright © 2011-2022 走看看