Advertisement
adityasuman100

infection 2nd sol

Mar 1st, 2025 (edited)
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class DisjointSet {
  6. private:
  7.     vector<int> parent;
  8.     vector<int> rank;
  9.     vector<bool> has_infected; // Whether the group has any infected member
  10.  
  11. public:
  12.     DisjointSet(int n) {
  13.         parent.resize(n);
  14.         rank.resize(n, 0);
  15.         has_infected.resize(n, false);
  16.        
  17.         for (int i = 0; i < n; i++) {
  18.             parent[i] = i;
  19.         }
  20.     }
  21.    
  22.     int find(int x) {
  23.         if (parent[x] != x) {
  24.             parent[x] = find(parent[x]);
  25.         }
  26.         return parent[x];
  27.     }
  28.    
  29.     void unite(int x, int y) {
  30.         int rootX = find(x);
  31.         int rootY = find(y);
  32.        
  33.         if (rootX == rootY) return;
  34.        
  35.         if (rank[rootX] < rank[rootY]) {
  36.             parent[rootX] = rootY;
  37.             has_infected[rootY] = has_infected[rootY] || has_infected[rootX];
  38.         } else {
  39.             parent[rootY] = rootX;
  40.             has_infected[rootX] = has_infected[rootX] || has_infected[rootY];
  41.            
  42.             if (rank[rootX] == rank[rootY]) {
  43.                 rank[rootX]++;
  44.             }
  45.         }
  46.     }
  47.    
  48.     bool groupHasInfected(int x) {
  49.         int root = find(x);
  50.         return has_infected[root];
  51.     }
  52.    
  53.     void markGroupAsInfected(int x) {
  54.         int root = find(x);
  55.         has_infected[root] = true;
  56.     }
  57. };
  58.  
  59. vector<int> timeOfInfection(int n, int m, vector<int>& initially_infected, vector<vector<int>>& updates) {
  60.     vector<int> infection_time(n, -1);
  61.     vector<bool> vaccinated(n, false);
  62.     DisjointSet ds(n);
  63.    
  64.     // Mark initially infected people
  65.     for (int person : initially_infected) {
  66.         infection_time[person] = 0;
  67.         ds.markGroupAsInfected(person);
  68.     }
  69.    
  70.     // Process each update
  71.     for (int i = 0; i < updates.size(); i++) {
  72.         int time = i + 1;
  73.         int type = updates[i][0];
  74.         int a = updates[i][1];
  75.        
  76.         if (type == 0) {  // Contact between a and b
  77.             int b = updates[i][2];
  78.            
  79.             int root_a = ds.find(a);
  80.             int root_b = ds.find(b);
  81.            
  82.             bool a_group_has_infected = ds.groupHasInfected(a);
  83.             bool b_group_has_infected = ds.groupHasInfected(b);
  84.            
  85.             // Merge the groups
  86.             ds.unite(a, b);
  87.            
  88.             // If one group has infected members, we need to spread infection
  89.             if (a_group_has_infected || b_group_has_infected) {
  90.                 // We need a second pass through all people to check who gets infected
  91.                 // This is more efficient than storing sets of group members
  92.                 for (int person = 0; person < n; person++) {
  93.                     if (ds.find(person) == ds.find(a) && infection_time[person] == -1 && !vaccinated[person]) {
  94.                         infection_time[person] = time;
  95.                     }
  96.                 }
  97.             }
  98.         } else {  // Vaccination of person a
  99.             vaccinated[a] = true;
  100.         }
  101.     }
  102.    
  103.     return infection_time;
  104. }
  105.  
  106. int main() {
  107.     // Example 1
  108.     int n1 = 6;
  109.     int m1 = 2;
  110.     vector<int> initially_infected1 = {0, 3};
  111.     vector<vector<int>> updates1 = {
  112.         {0, 0, 3}, {0, 0, 4}, {0, 5, 2},
  113.         {1, 3, -1}, {1, 2, -1}, {0, 2, 4}
  114.     };
  115.    
  116.     vector<int> result1 = timeOfInfection(n1, m1, initially_infected1, updates1);
  117.    
  118.     cout << "Example 1 output: ";
  119.     for (int t : result1) {
  120.         cout << t << " ";
  121.     }
  122.     cout << endl;
  123.    
  124.     // Example 2
  125.     int n2 = 5;
  126.     int m2 = 2;
  127.     vector<int> initially_infected2 = {3, 4};
  128.     vector<vector<int>> updates2 = {
  129.         {1, 1, -1}, {0, 0, 1}, {0, 0, 4}
  130.     };
  131.    
  132.     vector<int> result2 = timeOfInfection(n2, m2, initially_infected2, updates2);
  133.    
  134.     cout << "Example 2 output: ";
  135.     for (int t : result2) {
  136.         cout << t << " ";
  137.     }
  138.     cout << endl;
  139.    
  140.     // Example 3
  141.     int n3 = 6;
  142.     int m3 = 5;
  143.     vector<int> initially_infected3 = {0, 1, 2, 3, 4};
  144.     vector<vector<int>> updates3 = {
  145.         {0, 0, 1}, {0, 0, 2}, {0, 0, 3}, {0, 0, 4}, {0, 1, 2},
  146.         {0, 1, 3}, {0, 1, 4}, {0, 2, 3}, {0, 2, 4}, {0, 3, 4}
  147.     };
  148.    
  149.     vector<int> result3 = timeOfInfection(n3, m3, initially_infected3, updates3);
  150.    
  151.     cout << "Example 3 output: ";
  152.     for (int t : result3) {
  153.         cout << t << " ";
  154.     }
  155.     cout << endl;
  156.    
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement