文章目录
- 并查集
- 一、并查集
一、并查集
#include#define MAXN 1000 typedef int ElementType; typedef int SetName; int MaxSize = 10; typedef struct{ ElementType Data; int Parent; }SetType; void Union( SetType S[], SetName Root1, SetName Root2 ) { if ( S[Root2].Data < S[Root1].Data ) { S[Root2].Data += S[Root1].Data; S[Root1].Data = Root2; } else { S[Root1].Data += S[Root2].Data; S[Root2].Data = Root1; } } // 查找根 SetName Find( SetType S[], ElementType X ) { int i; for (i = 0; i < MaxSize && S[i].Data != X; i++); if ( i >= MaxSize ) return -1; for ( ; S[i].Parent >= 0; i = S[i].Parent ); return i; } int main() { return 0; }



