POJ 1611 The Suspects 并查集
2016-10-19 22:01
344 查看
Problem:
给了一些队伍,求出0号学生所在队伍的人数,每一个学生可以属于多个团队。
Solution:
并查集的模板题,把各个学生merge,然后直接query就可以了。
给了一些队伍,求出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; }
相关文章推荐
- POJ 1611 The Suspects 并查集裸题
- POJ 1611 The Suspects 并查集
- POJ 1611 The Suspects 并查集
- POJ 1611:The Suspects 并查集求解
- 并查集--POJ - 1611 The Suspects
- [poj 1611]The Suspects[并查集模板][递归与非递归实现]
- POJ 1611 The Suspects 并查集问题
- poj 1611 The Suspects 并查集
- poj 1611 The Suspects 【并查集】
- POJ 1611 The Suspects 并查集
- POJ 1611 The Suspects 并查集 Union Find
- POJ 1611 The Suspects【并查集】
- 【原】 POJ 1611 The Suspects 并查集 解题报告
- POJ 1611 The Suspects 并查集
- POJ 1611 The Suspects - 并查集
- 【解题报告】 POJ 1611 The Suspects 并查集基础 (一点自己的感悟)
- poj 1611 The Suspects 并查集变形题目
- POJ 1611 The Suspects 并查集(代码带启发式合并)
- poj 1611 The Suspects 【并查集基础题】
- POJ1611:The Suspects【并查集】