Ejemplo n.º 1
0
main()
{
	int n,m,q;
	int i,a,b;
	
	while(1)
	{
		scanf("%d %d",&n,&m);
		if(!n && !m)
			break;
			
		for(i=1;i<=n;++i)
			createset(i);
			
		for(i=0;i<m;++i)
		{
			scanf("%d %d",&a,&b);
			setunion(a,b);
		}
		
		scanf("%d",&q);
		for(i=0;i<q;++i)
		{
			scanf("%d %d",&a,&b);
			if(findset(a)==findset(b))
				puts("YES");
			else
				puts("NO");
		}
		puts("");
	}
	return 0;
}
Ejemplo n.º 2
0
    }
    return num;
}

/* the following functions have a more specific term of use, thus in the following a set is called */
/* group (of communicating processes) in a certain speed-class */

/* automerge will first create a new group for a certain class containing 2 endpoints elem1 and elem2 */
/* Afterwards all groups of same class containing one of the 2 endpoints will be merged */
/* all groups empty after merging will be freed */
/* automerge returns the number of remaining groups */
int automerge(t_set* pSets, int num, int elem1, int elem2, int class){
    int i,last = -1;
    
    /* create a new group containing the new found elements */
    pSets[num] = createset(size);
    addset(pSets[num], elem1);
    addset(pSets[num], elem2);
    c_table[num] = class;
    num++;
    
    /* connect all groups of same class  containing at least one of the elements */
    for (i=0; i<num; i++) {
	if (c_table[i] == class) {
	    if ( (inset(pSets[i], elem1)) || (inset(pSets[i], elem2)) ) {
		if (last != -1) {
		    merge(pSets[last], pSets[i]);
		}
		else {
		    last = i;
		}