Advertisement
asdfg0998

cdsa

Nov 5th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1.  auto unionGroups = [&](int a, int b, int time) {
  2.             int rootA = find(a);
  3.             int rootB = find(b);
  4.  
  5.             if (rootA != rootB) {
  6.                 // If one component is infected, propagate the infection time to the other component
  7.                 if (infectionTime[rootA] != -1 && infectionTime[rootB] == -1 && !vaccinated[rootB]) {
  8.                     infectionTime[rootB] = time; // Set infection time for rootB
  9.                 } else if (infectionTime[rootB] != -1 && infectionTime[rootA] == -1 && !vaccinated[rootA]) {
  10.                     infectionTime[rootA] = time; // Set infection time for rootA
  11.                 }
  12.  
  13.                 // Merge groups by rank
  14.                 if (rank[rootA] > rank[rootB]) {
  15.                     parent[rootB] = rootA;
  16.                 } else if (rank[rootA] < rank[rootB]) {
  17.                     parent[rootA] = rootB;
  18.                 } else {
  19.                     parent[rootB] = rootA;
  20.                     rank[rootA]++;
  21.                 }
  22.             }
  23.         };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement