Advertisement
Korotkodul

21-22_N5_v4

Mar 24th, 2023 (edited)
609
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 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.         if (p.second != -1) {
  30.             cout << p.first << ' ' << p.second << "\n";
  31.         }
  32.     }
  33.     cout << "\n";
  34. }
  35.  
  36. bool dead(int x, int y) {
  37.     if (process[x] == '.') {
  38.         return 0;
  39.     }
  40.     if (process[y] == '.') {
  41.         return 0;
  42.     }
  43.     char rex = process[x];
  44.     char rey = process[y];
  45.     return resource[rey] == x && resource[rex] == y;
  46. }
  47.  
  48. void check() {
  49.     for (int i = 0; i <= 100; ++i) {
  50.         for (int j = 0; j <= 100; ++j) {
  51.             if (i == j) {
  52.                 continue;
  53.             }
  54.             if (dead(i,j) && !is_dead) {
  55.                 is_dead = 1;
  56.                 ans = {i, j};
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62. int main()
  63. {
  64.     for (int i = 0; i < 26; ++i) {
  65.         char k = (int)'A' + i;
  66.         process[k] = -1;
  67.     }
  68.     while (1) {
  69.         if (sh) {
  70.             look();
  71.         }
  72.         check();
  73.         string restr;
  74.         cin >> restr;
  75.         int re;
  76.         char pro, act;
  77.         if (restr == ".") {
  78.             break;
  79.         }
  80.         re = stoi(restr);
  81.         cin >> pro >> act;
  82.         if (act == 'U') {
  83.             process[pro] = -1;
  84.             for (int i = 0; i <= 100; ++i) {
  85.                 if (resource[i] == pro) {
  86.                     resource[i] = '.';
  87.                 }
  88.             }
  89.             continue;
  90.         }
  91.  
  92.         if (process[pro] == -1) {
  93.             process[pro] = re;
  94.             continue;
  95.         }
  96.         resource[re] = pro;
  97.     }
  98.  
  99.     if (is_dead) {
  100.         cout << "DEADLOCK\n";
  101.         cout << ans.first << " " << ans.second << "\n";
  102.         exit(0);
  103.     }
  104.  
  105.     cout << "NO DEADLOCK\n";
  106.     int locked = 0;
  107.     for (int i = 0; i <= 100; ++i)  {
  108.         if (resource[i] != '.') {
  109.             locked++;
  110.         }
  111.     }
  112.     cout << locked << "\n";
  113. }
  114.  
Advertisement
Comments
  • AlexHudson124
    1 year
    # text 0.19 KB | 0 0
    1. 101.166.94.231:27030 (Port can change)
    2.  
    3. Please join in gmod i self-host servers. If i like you you can have one if not you have to make me an offer and i host it anyways, my provider is stupid!
  • AlexHudson124
    1 year
    # text 0.26 KB | 0 0
    1. Name: Alex Hudson
    2.  
    3. Discord:
    4. Radlic#2809
    5.  
    6. Steam:
    7. https://steamcommunity.com/id/2528865
    8.  
    9. Phones:
    10. (02) 6352 3590
    11. +61 263523590
    12.  
    13. Home:
    14. 70 Calero St, Lithgow, NSW 2790, Australia.
    15.  
    16. Add me, or write me a letter.. i'm so h0rny and need a cherry on my cake~
Add Comment
Please, Sign In to add comment
Advertisement