Advertisement
adityasuman100

Untitled

Mar 1st, 2025
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.50 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 root) {
  49.         return has_infected[root];
  50.     }
  51.    
  52.     void markGroupAsInfected(int x) {
  53.         int root = find(x);
  54.         has_infected[root] = true;
  55.     }
  56. };
  57.  
  58. vector<int> timeOfInfection(int n, int m, vector<int>& initially_infected, vector<vector<int>>& updates) {
  59.     vector<int> infection_time(n, -1);
  60.     vector<bool> vaccinated(n, false);
  61.     DisjointSet ds(n);
  62.    
  63.     // Mark initially infected people
  64.     for (int person : initially_infected) {
  65.         infection_time[person] = 0;
  66.         ds.markGroupAsInfected(person);
  67.     }
  68.    
  69.     // Process each update
  70.     for (int i = 0; i < updates.size(); i++) {
  71.         int time = i + 1;
  72.         int type = updates[i][0];
  73.         int a = updates[i][1];
  74.        
  75.         if (type == 0) {  // Contact between a and b
  76.             int b = updates[i][2];
  77.            
  78.             int root_a = ds.find(a);
  79.             int root_b = ds.find(b);
  80.            
  81.             // Check if the groups are already merged
  82.             if (root_a == root_b) continue;
  83.            
  84.             bool a_group_has_infected = ds.groupHasInfected(root_a);
  85.             bool b_group_has_infected = ds.groupHasInfected(root_b);
  86.            
  87.             // Track people in group b before merging
  88.             vector<int> group_b_members;
  89.             if (a_group_has_infected && !b_group_has_infected) {
  90.                 for (int person = 0; person < n; person++) {
  91.                     if (ds.find(person) == root_b) {
  92.                         group_b_members.push_back(person);
  93.                     }
  94.                 }
  95.             }
  96.            
  97.             // Track people in group a before merging
  98.             vector<int> group_a_members;
  99.             if (!a_group_has_infected && b_group_has_infected) {
  100.                 for (int person = 0; person < n; person++) {
  101.                     if (ds.find(person) == root_a) {
  102.                         group_a_members.push_back(person);
  103.                     }
  104.                 }
  105.             }
  106.            
  107.             // Merge the groups
  108.             ds.unite(a, b);
  109.            
  110.             // Infect unvaccinated members of group b if group a has infected
  111.             if (a_group_has_infected && !b_group_has_infected) {
  112.                 for (int person : group_b_members) {
  113.                     if (infection_time[person] == -1 && !vaccinated[person]) {
  114.                         infection_time[person] = time;
  115.                     }
  116.                 }
  117.             }
  118.            
  119.             // Infect unvaccinated members of group a if group b has infected
  120.             if (!a_group_has_infected && b_group_has_infected) {
  121.                 for (int person : group_a_members) {
  122.                     if (infection_time[person] == -1 && !vaccinated[person]) {
  123.                         infection_time[person] = time;
  124.                     }
  125.                 }
  126.             }
  127.         } else {  // Vaccination of person a
  128.             vaccinated[a] = true;
  129.         }
  130.     }
  131.    
  132.     return infection_time;
  133. }
  134.  
  135. int main() {
  136.     // Example 1
  137.     int n1 = 6;
  138.     int m1 = 2;
  139.     vector<int> initially_infected1 = {0, 3};
  140.     vector<vector<int>> updates1 = {
  141.         {0, 0, 3}, {0, 0, 4}, {0, 5, 2},
  142.         {1, 3, -1}, {1, 2, -1}, {0, 2, 4}
  143.     };
  144.    
  145.     vector<int> result1 = timeOfInfection(n1, m1, initially_infected1, updates1);
  146.    
  147.     cout << "Example 1 output: ";
  148.     for (int t : result1) {
  149.         cout << t << " ";
  150.     }
  151.     cout << endl;
  152.    
  153.     // Example 2
  154.     int n2 = 5;
  155.     int m2 = 2;
  156.     vector<int> initially_infected2 = {3, 4};
  157.     vector<vector<int>> updates2 = {
  158.         {1, 1, -1}, {0, 0, 1}, {0, 0, 4}
  159.     };
  160.    
  161.     vector<int> result2 = timeOfInfection(n2, m2, initially_infected2, updates2);
  162.    
  163.     cout << "Example 2 output: ";
  164.     for (int t : result2) {
  165.         cout << t << " ";
  166.     }
  167.     cout << endl;
  168.    
  169.     // Example 3
  170.     int n3 = 6;
  171.     int m3 = 5;
  172.     vector<int> initially_infected3 = {0, 1, 2, 3, 4};
  173.     vector<vector<int>> updates3 = {
  174.         {0, 0, 1}, {0, 0, 2}, {0, 0, 3}, {0, 0, 4}, {0, 1, 2},
  175.         {0, 1, 3}, {0, 1, 4}, {0, 2, 3}, {0, 2, 4}, {0, 3, 4}
  176.     };
  177.    
  178.     vector<int> result3 = timeOfInfection(n3, m3, initially_infected3, updates3);
  179.    
  180.     cout << "Example 3 output: ";
  181.     for (int t : result3) {
  182.         cout << t << " ";
  183.     }
  184.     cout << endl;
  185.    
  186.     return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement