POJ 1611 The Suspects 并查集

Problem:
给了一些队伍,求出0号学生所在队伍的人数,每一个学生可以属于多个团队。
Solution:
并查集的模板题,把各个学生merge,然后直接query就可以了。

#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<vector>
#include<fstream>
#include<list>
#include<iomanip>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s,0,sizeof(s))

const double PI = 3.141592653589;
const int INF = 0x3fffffff;

template <typename Type>
class DisjointSet {
    struct node {
        int parent_;
        Type relation_;
        node() {
            relation_ = 0;
        }
    };

    vector<node> nodes;
    vector<int> parents;

public:
    Type number_relations_;
    bool is_relation;


    DisjointSet(int number_nodes) {
        parents.resize(number_nodes+1);
        is_relation = false;

        for(int i = 0; i <= number_nodes; ++i)
            parents[i] = i;
    }

    DisjointSet(int number_nodes, Type number_relations) {
        number_relations_ = number_relations;
        nodes.resize(number_nodes+1);
        is_relation = true;

        for(int i = 0; i <= number_nodes; ++i)
            nodes[i].parent_ = i;
    }

    int find(int x) {
        if(is_relation == true) {
            if(nodes[x].parent_ != x) {
                int tdad = nodes[x].parent_; // x's old parent
                nodes[x].parent_ = find(nodes[x].parent_);
                nodes[x].relation_ = (nodes[x].relation_ + nodes[tdad].relation_) % number_relations_;
            }
            return nodes[x].parent_;
        } else {
            if(parents[x] != x)
                parents[x] = find(parents[x]);
            return parents[x];
        }
    }

    Type get_relation(int x) {
        return nodes[x].relation_;
    }

    void merge(int x, int y) {
        parents[find(y)] = find(x);
    }

    void merge(int x, int y, Type relation) {
        // relation: x->y
        int ydad = find(y);
        nodes[ydad].parent_ = find(x);
        nodes[ydad].relation_ = (nodes[x].relation_ + relation + (number_relations_-nodes[y].relation_)) % number_relations_;
    }
};

int main() {
    //    freopen("/Users/really/Documents/code/input","r",stdin);

    int m, n, t, f, num, ans;
    while((~scanf("%d%d", &n, &m)) && n){
        ans = 1;
        DisjointSet<int> ds(n);
        for(int i = 0; i < m; ++i){
            scanf("%d",&t);
            if(t != 0)
                scanf("%d",&f);
            for(int j = 1 ; j < t; ++j){
                scanf("%d", & num);
                ds.merge(f, num);
            }
        }
        for(int i = 1; i < n; ++i){
            if(ds.find(0) == ds.find(i))
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值