Advertisement
Korotkodul

21-22_N5_v5_check

Mar 24th, 2023 (edited)
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <set>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. #define ob push_back
  10. #define pii pair <int, int>
  11.  
  12. bool is_dead = 0;
  13. pii ans = {-1, -1};
  14. vector <char> process(101, '.');
  15. map <char, int> resource;
  16.  
  17. bool sh = 0;
  18.  
  19.  
  20. void look() {
  21.     cout << "process depends on resource\n";
  22.     for (int i = 0; i <= 100; ++i) {
  23.         if (process[i] != '.') {
  24.             cout << i << " " << process[i] << "\n";
  25.         }
  26.     }
  27.     cout << "resource depends on process\n";
  28.     for (auto p: resource) {
  29.         cout << p.first << ' ' << p.second << "\n";
  30.     }
  31.     cout << "\n";
  32. }
  33.  
  34. bool dead(int x, int y) {
  35.     if (process[x] == '.') {
  36.         return 0;
  37.     }
  38.     if (process[y] == '.') {
  39.         return 0;
  40.     }
  41.     char rex = process[x];
  42.     char rey = process[y];
  43.     return resource[rey] == x && resource[rex] == y;
  44. }
  45.  
  46. void check() {
  47.     for (int i = 0; i <= 100; ++i) {
  48.         for (int j = 0; j <= 100; ++j) {
  49.             if (i == j) {
  50.                 continue;
  51.             }
  52.             if (dead(i,j) && !is_dead) {
  53.                 is_dead = 1;
  54.                 ans = {i, j};
  55.             }
  56.         }
  57.     }
  58. }
  59.  
  60. int main()
  61. {
  62.     for (int i = 0; i < 26; ++i) {
  63.         char k = (int)'A' + i;
  64.         resource[k] = -1;
  65.     }
  66.     while (1) {
  67.         if (sh) {
  68.             look();
  69.         }
  70.         check();
  71.         string prostr;
  72.         cin >> prostr;
  73.         int pro;
  74.         char re, act;
  75.         if (prostr == ".") {
  76.             break;
  77.         }
  78.         pro = stoi(prostr);
  79.         cin >> re >> act;
  80.         if (act == 'U') {
  81.             process[pro] = '.';
  82.             for (int i = 0; i <= 100; ++i) {
  83.                 if (process[i] == re) {
  84.                     process[i] = '.';
  85.                 }
  86.             }
  87.             resource[re] = -1;
  88.             continue;
  89.         }
  90.  
  91.  
  92.         /*if (process[pro] == -1) {
  93.             process[pro] = re;
  94.             continue;
  95.         }*/
  96.  
  97.         if (resource[re] == -1) {
  98.             resource[re] = pro;
  99.             continue;
  100.         }
  101.  
  102.  
  103.         process[pro] = re;
  104.     }
  105.  
  106.     if (is_dead) {
  107.         cout << "DEADLOCK\n";
  108.         cout << ans.first << " " << ans.second << "\n";
  109.         exit(0);
  110.     }
  111.  
  112.     cout << "NO DEADLOCK\n";
  113.     int locked = 0;
  114.     for (auto p: resource) {
  115.         if (p.second != -1) {
  116.             locked++;
  117.         }
  118.     }
  119.     cout << locked << "\n";
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement