const int MAX = 30010; int nSets[MAX]; int nNum[MAX]; int nRank[MAX];
void MakeSets(int nN) { for (int i = 0; i < nN; ++i) { nSets[i] = i; nNum[i] = 1; nRank[i] = 0; } }
int FindSet(int nI) { if (nSets[nI] != nI) { int nPre = nSets[nI]; nSets[nI] = FindSet(nSets[nI]); nRank[nI] += nRank[nPre]; }
return nSets[nI]; }
void Move(int nX, int nY) { int nA = FindSet(nX); int nB = FindSet(nY); //printf("nA:%d,nB:%d\n", nA, nB); if (nA != nB) { nSets[nA] = nB; nRank[nA] += nNum[nB]; nNum[nB] += nNum[nA]; } }