And find out the detailed

Source: Internet
Author: User

When using and looking up a set, if the number of lookups is many, then the use of the naïve version of the lookup must be timed out. For example, there are 1 million elements, every time from the 1 millionth start to find, such an operation is 10^6, if the program asked to find 10 million times, so down is 10^13, it must be a problem.

This is the code of the simple lookup, suitable for a small amount of data

Findx (x)
{
R=x; 
while (parent[!=r)  
R=parent[r];  
R
}

Here's how to find the element using path compression:

Find (//Find the collection where the X element resides, and compress the path when backtracking )
{
if (parent[x]) 
{
parent[Find (parent[//Backtracking compression path  
The nodes that pass from the X-node search to the ancestor node point to the Ancestor node.
parent[x];
}

The above is a recursive way to compress the path, but the recursive compression path may cause overflow stack, I used to because of this re n times, below we say a non-recursive way to do the path compression:

Next is the merged code, which guarantees that the small subtree can be attached to the large subtree.

There are many groups of students, students in the same group often contact, there will be new students to join. But SARS is very easy to infect, as long as the reorganization of a classmate infected with SARS, then all of the group's students are considered SARS. The task now is to figure out how many students are infected with SARS. It is assumed that the student with number 0 is suffering from SARS.

The idea of solving the problem----> obviously and check the set. A detailed explanation of the check set can be learned by clicking and checking the set (disjoint collection). Use num[] to store the number of elements in the collection and update num[when the collection is merged]. Then find the root node x of the collection where 0 is located, so num[x] is answer.

  1. Code highlighting produced by Actipro Codehighlighter (freeware) http://www. codehighlighter.com/--> #include <stdio.h>//by ktyanny
  2. #include <iostream>
  3. Using namespace std;
  4. const int maxn = 30001; / * Number of nodes on line * /
  5. int PA[MAXN]; /*p[x] Represents the parent node of x * /
  6. int RANK[MAXN]; /*rank[x] is an upper bound of the height of x * /
  7. int NUM[MAXN]; /*num[] Stores the number of elements in the collection and updates num[when the collection is merged] * /
  8. void Make_set (int x)
  9. {/* Create a unit set * /
  10. PA[X] = x;
  11. Rank[x] = 0;
  12. NUM[X] = 1;
  13. }
  14. int Find_set (int x)
  15. {/* Lookup with path compression * /
  16. / * Save the number you want to find * /
  17. int r = x, temp;
  18. / * Locate the root node * /
  19. while (pa[r]! = r) R = Pa[r];
  20. While (x! = r)
  21. {
  22. temp = pa[x];
  23. Pa[x] = r;
  24. x = temp;
  25. }
  26. return x;
  27. //if (x! = Pa[x])//commented out is actually OK, but do not want to use the return to do
  28. //Pa[x] = Find_set (pa[x]);
  29. //return pa[x];
  30. }
  31. /* Merges the collection of x, y by rank */
  32. void Union_set (int x, int y)
  33. {
  34. x = Find_set (x);
  35. y = Find_set (y);
  36. if (x = = y)return;
  37. if (rank[x] > Rank[y])/* make rank higher as parent node * /
  38. {
  39. Pa[y] = x;
  40. NUM[X] + = Num[y];
  41. }
  42. Else
  43. {
  44. Pa[x] = y;
  45. if (rank[x] = = Rank[y])
  46. rank[y]++;
  47. Num[y] + = num[x];
  48. }
  49. }
  50. Answer to 1611
  51. int main ()
  52. {
  53. int N, m, x, y, I, T, J;
  54. while (scanf ("%d%d", &n, &m))
  55. {
  56. if (m==n && n = = 0) break ;
  57. if (m = = 0)
  58. {
  59. cout << "1\n";  continue;
  60. }
  61. For (i = 0; i < n; i++)
  62. Make_set (i);
  63. For (i = 0; i < m; i++)
  64. {
  65. scanf ("%d", &t);
  66. scanf ("%d", &x);
  67. For (j = 1; j < T; j + +) {
  68. scanf ("%d", &y);
  69. Union_set (x, y);
  70. x = y;
  71. }
  72. }
  73. x = Find_set (0); / * Find the root of the 0 tree * * *
  74. //int ans = 0;
  75. //for (i = 0; i < n; i++)
  76. //if (pa[i] = = x)
  77. //ans++;
  78. //cout << ans << endl;
  79. cout << num[x] << Endl;
  80. }
  81. return 0;
  82. }

And find out the detailed

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.