// // 小米的2016的面试题==具体描述参见课件 // int Friends(int n, int m, int r[][2]) { assert(r); // 初始创建一个并查集 const int N = n+1; int* unionSet = new int[N]; memset(unionSet, -1, N*sizeof(int)); for (int i = 0; i < m; ++i) { int first = r[i][0]; int second = r[i][1]; int parent1 = FindRoot(unionSet, first); int parent2 = FindRoot(unionSet, second); if (parent1 != parent2) { UnionSet(unionSet, parent1, parent2); } } int count = 0; for (int i = 1; i < N; ++i) { if (unionSet[i] < 0) { ++count; } } delete[] unionSet; return count; }
int main() { Set S1, S2, S3; unsigned int x,M,N,i,Univ; printf("Enter the Universal Space\n"); scanf("%d",&Univ); /********** Create Set S1 **********/ CreateSet(S1,Univ); printf("S1 set is \n"); PrintSet(S1,Univ); /********** Create Set S2 **********/ CreateSet(S2,Univ); printf("S2 set is \n"); PrintSet(S2,Univ); /* Create Set S3 *****************/ CreateSet(S3,Univ); printf("S3 set is \n"); PrintSet(S3,Univ); printf("Enter the number of Elements in S1 and S2\n"); scanf("%d%d",&M,&N); /********* Add Elements to S1 ******/ printf("Add Elements to S1\n"); for(i=0;i<M;i++) { printf("Enter the Element\n"); scanf("%d",&x); AddElementSet(x,S1,Univ); } printf("Add Elements to S2\n"); for(i=0;i<N;i++) { printf("Enter the Element\n"); scanf("%d",&x); AddElementSet(x,S2,Univ); } printf("The Set S1 after Addition\n"); PrintSet(S1,Univ); printf("The Set S2 after Addition\n"); PrintSet(S2,Univ); printf("Enter the Element to Delete from S1\n"); scanf("%d",&x); RemoveElementSet(x,S1,Univ); printf("The Set S1 after Removal\n"); PrintSet(S1,Univ); printf("Union of S1 and S2 sets are\n"); UnionSet(S1,S2,S3,Univ); PrintSet(S3,Univ); printf("Intersection of S1 and S2 sets are\n"); IntersectSet(S1,S2,S3,Univ); PrintSet(S3,Univ); printf("Complement of set S1 is\n"); ComplementSet(S1,S2,Univ); PrintSet(S2,Univ); printf("Is set S1 EQUALS set S2 ?\n"); if(EqualsSet(S1,S2,Univ)) printf("The Sets are EQUAL\n"); else printf("The Sets are NOT EQUAL\n"); printf("Enter the value for finding element\n"); scanf("%d",&x); if(isElementOfSet(x,S1,Univ)) printf("The value %d is AVAILABLE in S1\n",x); else printf("The value %d is NOT AVAILABLE in S1\n",x); return 0; }
void PrintReduceOp( void * invec, void * inoutvec, int * len, MPI_Datatype * datatype ) { LineData * src_data = (LineData *)((char*)invec + 2 * sizeof(int)); LineData * dest_data = (LineData *)((char*)inoutvec + 2 * sizeof(int)); LineData * psrcline, * pdestline; int i, j; int count = ((int *)invec)[0]; int linesize = ((int *)invec)[1]; datatype = datatype; len = len; /* Run through each (valid) entry in dest_data and compare it with * each valid entry in src_data. */ for( pdestline = dest_data, i = 0; i < count; pdestline += linesize, i++ ) { /* An empty set indicates that the string is blank; skip it */ if( IsEmpty( pdestline+SET ) ) continue; /* We have a valid string; compare it with each of the * strings in the incoming vector. */ for( psrcline = src_data, j = 0; j < count; psrcline += linesize, j++ ) { /* Skip blank entries */ if( IsEmpty( psrcline+SET ) ) continue; /* If they match, remove this string (because it * won't match any other entries), and mark the * nodes where it appeared. Once we've found a * match to this dest string, there won't be any * more in the source array because it should * contain only one copy of each string, so we * can quit this inner loop after the first match. */ if( strcmp( pdestline+TEXT, psrcline+TEXT ) == 0 ) { UnionSet( pdestline+SET, psrcline+SET ); ClearSet( psrcline+SET ); /* We want to put the result in the * lowest-numbered position in the destination * array so items will come out in the order * of the lowest-numbered node in the set when * we print the sets out. */ if( j < i ) { /* Move to lower-numbered position */ LineData * lower = dest_data + (j * linesize); UnionSet( lower+SET, pdestline+SET ); ClearSet( pdestline+SET ); strcpy( lower+TEXT, pdestline+TEXT ); } break; } } } /* All the matching strings should now be copied into the source. * Those remaining are unique, so we can copy them all into the * dest array. Since each node sends only one string, there's no * danger of overwriting an entry in dest when we copy an entry * from source. */ for( pdestline = dest_data, psrcline = src_data, i = 0; i < count; pdestline += linesize, psrcline += linesize, i++ ) { /* If we find a string whose entry hasn't been cleared, * copy it and its list of nodes. */ if( !IsEmpty( psrcline+SET ) ) { UnionSet( pdestline+SET, psrcline+SET ); strcpy( pdestline+TEXT, psrcline+TEXT ); } } }