"Pen test" and check the set implementation

Source: Internet
Author: User

and check Set (Unionset) is a tree-based data structure used to handle some disjoint collections, merging and querying problems. Often used in the forest to express.

And the search set realizes that n different elements are divided into a set of disjoint sets. At the beginning, each element is a collection, and then the two collections are merged by law.

For example: There are now a total of 10 elements 0,1,2,3,4,5,6,7,8,9. They are made up of 3 sets,

At the beginning, each element is treated as a collection of elements, and each element is initialized to-1,

Merges 22 sets According to the set by law, 6, 7, 8 belong to the set of 0, so 6, 7, 8 three positions are placed 0, and 0 this position minus 3;1 and 2 are similar, 4 and 9 are all 1 of this set, so 4, 9 both positions are 1, and the position 1 of the data minus 2; 5 belongs to set 2, so the data for position 3, 5 is set to 2, and the data for position 2 is subtracted by 2, namely:

So we can see a few sets of the number of negative numbers clearly, and we can clearly see which node belongs to which set. There are 3 negative numbers, so there are 3 sets, 3, 5 position data is 2, so they belong to the collection 2;6, 7, 8 position data are 0, so they belong to the collection 0;4, 9 belongs to the collection collection 1.

merging of collections:

Shaped like:

Merging collection 1 into set 0, set 1 has 1, 4, 93 elements, so position 1 is set to 0, and the data for position 0 is subtracted by 3, namely:

We can see that 4, 9 belongs to the set 1, the set 1 belongs to the set 0; there are 2 negative numbers in the array, so there are two sets!

Code implementation:

Class Unionset{public:unionset (size_t size): _size (size), _array (new Int[size]) {for (int i = 0; i < size; i++) {//sets each Elements are initialized to -1_array[i] =-1;}} Merge two sets void merge (int root1, int root2) {//Find the representative element of                the collection where the ROOT1 is located and the representative elements of the Root2 collection, link while (_array[root2] >= 0) {Root2 = _array[root2];} while (_array[root1] >= 0) {root1 = _array[root1];} _ARRAY[ROOT1] + = _array[root2];_array[root2] = root1;} Find root corresponding set (root) represents element int find (int root) {while (_array[root]>=0) {root = _array[root];} return root;} print void print () {for (int i = 0; i < _size; i++) {cout << _array[i] << "";} cout << Endl;} public:int* _array;size_t _size;};

 

Written questions:

If there is a known n-person and M-to-friend relationship (stored in array R), if two people are a direct or indirect friend relationship (friend of friend's friend ...). ), think that they belong to the same circle of friends, requesting that there are several friends in this N person circle.

For example: n=5,m=3,r={{1,2},{2,3},{4,5}, which indicates that there are 5 people, 1 and 2 are friends, 2 and 3 are friends, 4 and 5 are friends, 1.2.3 belongs to a circle of friends, 4.5 belongs to a different circle of friends, and results in two circle of friends.

Finally, analyze the time and space complexity of the code you write.

This problem with the use and check the implementation will be more simple and efficient!

You need to create a function to calculate the number of circle of friends:

Count the number of friends in int friends (int n, int m, int r[][2])//n number of elements, m for each circle of friends {Unionset uf (n + 1);//Initialize friend circle for (int i =0; i <= m; i + +) {int first = R[i][0];int second = R[i][1];uf. Merge (first, second);} uf. Print ();//count the number of circle of friends int count = 0;for (int i = 1; I <= n; i++) {if (Uf._array[i] < 0) {count++;}} return count;}

  

 

"Pen test" and check the set implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.