并查集
#include <stdio.h> #include <iostream> using namespace std; #define N 10 int set[N+1]; int find(int x) { return x == set[x] ? x : set[x] = find(set[x]); } void join(int a,int b) { int x = find(a); int y = find(b); if (x != y) { set[x] = y; } } int main() { for (int i = 1; i <= 10; i++) { set[i] = i; } return 0; }