2019-06-13  2024-09-15    602 字  2 分钟

POJ-1611 The Suspects

并查集板子题

Code
  1/**
  2 *    author: Akvicor
  3 *    created: 2019-06-13 15-21-28
  4**/
  5
  6#include <cstdio>
  7#include <cstring>
  8#include <iomanip>
  9#include <ctime>
 10#include <algorithm>
 11#include <iostream>
 12#include <string>
 13#include <vector>
 14#include <stack>
 15#include <bitset>
 16#include <complex>
 17#include <cstdlib>
 18#include <cmath>
 19#include <set>
 20#include <list>
 21#include <deque>
 22#include <map>
 23#include <queue>
 24
 25using namespace std;
 26
 27#ifdef DEBUG
 28string to_string(string s) {
 29	return '"' + s + '"';
 30}
 31
 32string to_string(const char* s) {
 33	return to_string((string) s);
 34}
 35
 36string to_string(bool b) {
 37return (b ? "true" : "false");
 38}
 39
 40template <typename A, typename B>
 41string to_string(pair<A, B> p) {
 42	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
 43}
 44
 45template <typename A>
 46string to_string(A v) {
 47	bool first = true;
 48	string res = "{";
 49	for (const auto &x : v) {
 50		if (!first) {
 51			res += ", ";
 52		}
 53		first = false;
 54		res += to_string(x);
 55	}
 56	res += "}";
 57	return res;
 58}
 59
 60void debug_out() { cerr << endl; }
 61
 62template <typename Head, typename... Tail>
 63void debug_out(Head H, Tail... T) {
 64	cerr << " " << to_string(H);
 65	debug_out(T...);
 66}
 67#endif
 68
 69#ifdef DEBUG
 70#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
 71#else
 72#define debug(...) 17
 73#endif
 74
 75#ifdef DEBUG
 76#define FAST_IO 17
 77#define cout cout << "-->"
 78#else
 79#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
 80#define endl '\n'
 81#endif
 82
 83#define LL long long
 84#define ULL unsigned long long
 85#define rep(i, n) for(int i = 0; i < (n); ++i)
 86#define reep(i, n) for(int i = 0; i <= (n); ++i)
 87#define lop(i, a, n) for(int i = a; i < (n); ++i)
 88#define loop(i, a, n) for(int i = a; i <= (n); ++i)
 89#define ALL(v) (v).begin(), (v).end()
 90#define PB push_back
 91#define VI vector<int>
 92#define PII pair<int,int>
 93#define FI first
 94#define SE second
 95#define SZ(x) ((int)(x).size())
 96
 97const double EPS = 1e-6;
 98const double PI = acos(-1.0);
 99const int INF = 0x3f3f3f3f;
100const LL LINF = 0x7f7f7f7f7f7f7f7f;
101const int MAXN = (int)1e6 + 10;
102const int MOD = (int)1e9 + 7;
103
104class UnionFind{
105	private:
106		int * parent;
107		int * sz;
108		int count;
109	public:
110		UnionFind(int n){
111			parent = new int[n];
112			sz = new int[n];
113			this->count = n;
114			for(int i = 0; i < n; ++i){
115				parent[i] = i;
116				sz[i] = 1;
117			}
118		}
119		~UnionFind(){
120			delete[] parent;
121			delete[] sz;
122		}
123
124		int find(int p){
125			while(p != parent[p])
126				p = parent[p];
127			return p;
128		}
129
130		bool isConnected(int p, int q){
131			return find(p) == find(q);
132		}
133
134		void unionElements(int p, int q){
135			int pRoot = find(p);
136			int qRoot = find(q);
137
138			if(pRoot == qRoot) return;
139
140			if(sz[pRoot] < sz[qRoot]){
141				parent[pRoot] = qRoot;
142				sz[qRoot] += sz[pRoot];
143			}else{
144				parent[qRoot] = pRoot;
145				sz[pRoot] += sz[qRoot];
146			}
147		}
148
149		int cnt(int p){
150			return sz[find(p)];
151		}
152};
153
154int n, m;
155
156int main(){
157	FAST_IO;
158
159	while(cin >> n >> m){
160		if(n == 0 && m == 0) break;
161		int k, p, q;
162		UnionFind uf = UnionFind(n);
163		while(m--){
164			cin >> k;
165			if(k == 0) continue;
166			if(k == 1){cin >> k; continue;}
167			cin >> p;
168			while(--k){
169				cin >> q;
170				uf.unionElements(p, q);		
171			}
172		}
173		cout << uf.cnt(0) << endl;
174	}
175
176	return 0;
177}

除另有声明外本博客文章均采用 知识共享 (Creative Commons) 署名 4.0 国际许可协议 进行许可转载请注明原作者与文章出处