Advertisement
asdfg0998

dcd

Nov 5th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_set>
  4. #include <functional>
  5.  
  6. using namespace std;
  7.  
  8. class Solution {
  9. public:
  10.     vector<int> timeOfInfection(int n, int m, vector<int>& initially_infected, vector<vector<int>>& updates) {
  11.         vector<int> parent(n), infectionTime(n, -1), vaccinated(n, 0);
  12.        
  13.         // Initialize Union-Find structure
  14.         for (int i = 0; i < n; ++i) parent[i] = i;
  15.  
  16.         // Set initial infection times
  17.         for (int person : initially_infected) infectionTime[person] = 0;
  18.        
  19.         // Define find function with an explicit return type
  20.         function<int(int)> find = [&](int x) {
  21.             if (x != parent[x]) parent[x] = find(parent[x]);
  22.             return parent[x];
  23.         };
  24.  
  25.         // Helper function to infect all connected components recursively
  26.         function<void(int, int)> propagateInfection = [&](int node, int time) {
  27.             int root = find(node);
  28.             for (int i = 0; i < n; ++i) {
  29.                 if (find(i) == root && infectionTime[i] == -1 && !vaccinated[i]) {
  30.                     infectionTime[i] = time; // Set infection time for non-vaccinated individuals
  31.                 }
  32.             }
  33.         };
  34.  
  35.         auto unionGroups = [&](int a, int b, int time) {
  36.             int rootA = find(a);
  37.             int rootB = find(b);
  38.             if (rootA != rootB) {
  39.                 // Merge rootB into rootA
  40.                 parent[rootB] = rootA;
  41.  
  42.                 if (infectionTime[rootA] != -1 && infectionTime[rootB] == -1) {
  43.                     propagateInfection(rootB, time);
  44.                 } else if (infectionTime[rootB] != -1
  45.                 && infectionTime[rootA] == -1) {
  46.                     propagateInfection(rootA, time);
  47.                 }
  48.             }
  49.         };
  50.  
  51.         // Process updates
  52.         for (int t = 0; t < updates.size(); ++t) {
  53.             int type = updates[t][0];
  54.             int a = updates[t][1];
  55.             int b = updates[t][2];
  56.  
  57.             if (type == 0) { // Contact
  58.                 unionGroups(a, b, t + 1);
  59.             } else if (type == 1) { // Vaccination
  60.                 // Only vaccinate if the person is not infected
  61.                 if (infectionTime[a] == -1) {
  62.                     vaccinated[a] = 1;
  63.                 }
  64.             }
  65.         }
  66.        
  67.         return infectionTime;
  68.     }
  69. };
  70.  
  71. int main() {
  72.     // Sample Input
  73.     int n = 5; // Number of people
  74.     int m = 2; // Number of initially infected people
  75.     vector<int> initially_infected = {1, 4}; // Initially infected people
  76.     vector<vector<int>> update = {
  77.         {0, 1, 2},
  78.         {1, 0, -1},
  79.         {0, 0, 2},
  80.         {0, 0, 3}
  81.     };
  82.    
  83.     // Solution
  84.     Solution sol;
  85.     vector<int> result = sol.timeOfInfection(n, m, initially_infected, update);
  86.  
  87.     // Output the result
  88.     cout << "Infection times for each person:" << endl;
  89.     for (int i = 0; i < result.size(); ++i) {
  90.         cout << "Person " << i << ": " << result[i] << endl;
  91.     }
  92.  
  93.     return 0;
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement