POJ 1611 并查集

23333......简单的并查集模板。就是被并查集递归式路径压缩RE坑过。。。学习一下非递归式路径压缩。

附代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#define maxn 30100
using namespace std;

int fa[maxn];
int n, m, k;

void init()
{
    for (int i=0; i<=n; ++i)
        fa[i] = i;
}

//int find1(int v)  // 递归式路径压缩查找
//{
//    if (fa[v] == v) return v;
//    return fa[v] = find1(fa[v]);
//}

int find1(int v)
{
    int k, j, r;
    r = v;
    while (r != fa[r])
        r = fa[r];
    k = v;
    while(k != r)
    {
        j = fa[k];
        fa[k] = r;
        k = j;
    }
    return r;
}

void unin(int u, int v)
{
    int fau = find1(u);
    int fav = find1(v);
    if (fau != fav)
        fa[fau] = fav;
}

int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        if (n ==0 && m == 0)
            break;
        init();
        while(m--)
        {
            scanf("%d", &k);
            int t, tt;
            if (k != 0)
                scanf("%d", &t);
            for (int i=1; i<k; ++i)
            {
                scanf("%d", &tt);
                unin(t, tt);
            }
        }
        int num = 0;
        for (int i=0; i<n; ++i)
        {
            if (find1(i) == find1(0))
                num++;
        }
        printf("%d\n", num);
    }
    return 0;
}

时间: 2024-07-29 23:14:16

POJ 1611 并查集的相关文章

poj 1611~并查集基础

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 33160   Accepted: 16080 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

The Suspects POJ - 1611 并查集,同集合的数

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.In the Not-Spread

POJ 2524 并查集

Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23580 Accepted: 11609 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding o

poj 2513 并查集,Trie(字典树), 欧拉路径

- Colored Sticks POJ - 2513 You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

POJ 2492 并查集扩展(判断同性恋问题)

G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2492 Appoint description: Description BackgroundProfessor Hopper is researching the sexual behavior of a rare species of bugs. H

poj 1417 并查集+dp

转自:点我 题目:给出p1+p2个人,其中p1个是好人,p2个是坏人.然后有一些关系 ,a说b是好人(坏人).其中没有矛盾的,判断是否有唯一解判断哪些人是好人,哪些人是坏人. 其中比较重要的是,好人总说真话,坏人总说假话.不需要判断矛盾.唯一解 http://poj.org/problem?id=1417 其中好人说真话,坏人说假话这点很重要. 那么如果一个人说另一个人是好人,那么如果这个人是好人,说明 对方确实是好人,如果这个是坏人,说明这句话是假的,对方也是坏人. 如果一个人说另一个人是坏人

POJ 2492 并查集应用的扩展

A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and

poj 1984 并查集

题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 1 /* 2 POJ 1984 3 并查集 4 */ 5 6 #include <stdio.h> 7 #include <string.h> 8 #include <iostream> 9 #include <algorithm> 10 #include <math.h> 11 using namespace std; 12

Wireless Network (poj 2236 并查集)

Language: Default Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17602   Accepted: 7418 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network wit