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> resource(101, '.');
- map <char, int> process;
- bool sh = 1;
- void look() {
- cout << "resource\n";
- for (int i = 0; i <= 100; ++i) {
- if (resource[i] != '.') {
- cout << i << " " << resource[i] << "\n";
- }
- }
- cout << "process\n";
- for (auto p: process) {
- if (p.second != -1) {
- cout << p.first << ' ' << p.second << "\n";
- }
- }
- cout << "\n";
- }
- bool dead(int x, int y) {
- if (resource[x] == '.') {
- return 0;
- }
- if (resource[y] == '.') {
- return 0;
- }
- char prox = process[x];
- char proy = process[y];
- return process[proy] == x && process[prox] == 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;
- process[k] = -1;
- }
- while (1) {
- if (sh) {
- look();
- }
- check();
- string restr;
- cin >> restr;
- int re;
- char pro, act;
- if (restr == ".") {
- break;
- }
- re = stoi(restr);
- cin >> pro >> act;
- if (act == 'U') {
- process[pro] = -1;
- for (int i = 0; i <= 100; ++i) {
- if (resource[i] == pro) {
- resource[i] = '.';
- }
- }
- continue;
- }
- if (process[pro] == -1) {
- process[pro] = re;
- continue;
- }
- resource[re] = pro;
- }
- if (is_dead) {
- cout << "DEADLOCK\n";
- cout << ans.first << " " << ans.second << "\n";
- exit(0);
- }
- cout << "NO DEADLOCK\n";
- int locked = 0;
- for (int i = 0; i <= 100; ++i) {
- if (resource[i] != '.') {
- locked++;
- }
- }
- cout << locked << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement