题目描述
#include <stdio.h>
#define N 100int connected(int* m, int p, int q)
{return m[p]==m[q]? 1 : 0;
}void link(int* m, int p, int q)
{int i;if(connected(m,p,q)) return;int pID = m[p];int qID = m[q];for(i=0; i<N; i++) _____________________________________; //填空位置
}int main()
{int m[N];int i;for(i=0; i<N; i++) m[i] = i; //初始状态,每个节点自成一个连通域link(m,0,1); //添加两个账户间的转账关联link(m,1,2); link(m,3,4); link(m,5,6); link(m,6,7); link(m,8,9); link(m,3,7); printf("%d ", connected(m,4,7));printf("%d ", connected(m,4,5));printf("%d ", connected(m,7,9));printf("%d ", connected(m,9,2));return 0;
}
填空位置的最终目的是将两个数进行连接,并查集
填空位置:if(m[i] == pID)m[i] = qID;
如果找到m[i]与pID是一样的说明这个m[i]和qID有关联,直接用m[i] = qID使其建立关联