Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <set>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- #define ob push_back
- #define pii pair <int, int>
- bool is_dead = 0;
- pii ans = {-1, -1};
- vector <char> process(101, '.');
- map <char, int> resource;
- bool sh = 0;
- void look() {
- cout << "process depends on resource\n";
- for (int i = 0; i <= 100; ++i) {
- if (process[i] != '.') {
- cout << i << " " << process[i] << "\n";
- }
- }
- cout << "resource depends on process\n";
- for (auto p: resource) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- bool dead(int x, int y) {
- if (process[x] == '.') {
- return 0;
- }
- if (process[y] == '.') {
- return 0;
- }
- char rex = process[x];
- char rey = process[y];
- return resource[rey] == x && resource[rex] == y;
- }
- void check() {
- for (int i = 0; i <= 100; ++i) {
- for (int j = 0; j <= 100; ++j) {
- if (i == j) {
- continue;
- }
- if (dead(i,j) && !is_dead) {
- is_dead = 1;
- ans = {i, j};
- }
- }
- }
- }
- int main()
- {
- for (int i = 0; i < 26; ++i) {
- char k = (int)'A' + i;
- resource[k] = -1;
- }
- while (1) {
- if (sh) {
- look();
- }
- check();
- string prostr;
- cin >> prostr;
- int pro;
- char re, act;
- if (prostr == ".") {
- break;
- }
- pro = stoi(prostr);
- cin >> re >> act;
- if (act == 'U') {
- process[pro] = '.';
- for (int i = 0; i <= 100; ++i) {
- if (process[i] == re) {
- process[i] = '.';
- }
- }
- resource[re] = -1;
- continue;
- }
- /*if (process[pro] == -1) {
- process[pro] = re;
- continue;
- }*/
- if (resource[re] == -1) {
- resource[re] = pro;
- continue;
- }
- process[pro] = re;
- }
- if (is_dead) {
- cout << "DEADLOCK\n";
- cout << ans.first << " " << ans.second << "\n";
- exit(0);
- }
- cout << "NO DEADLOCK\n";
- int locked = 0;
- for (auto p: resource) {
- if (p.second != -1) {
- locked++;
- }
- }
- cout << locked << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement