Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <set>
- using namespace std;
- #define pb push_back
- bool sh = 1;
- map <char, vector <char> > G;
- map <char, int> clr;
- set <char> all_char;
- void check() {
- cout << "CHECK\n";
- cout << "G\n";
- for (auto p: G) {
- char l = p.first;
- vector <char> v = p.second;
- cout << "key = " << l << "\n";
- cout << "value\n";
- for (char l: v) {
- cout << l << ' ' ;
- } cout << "\n";
- }
- cout << "END OF CHECK\n";
- cout << "\n";
- }
- void unlock(char proc, char rec) {
- auto pointer = find(begin(G[rec]), end(G[rec]), proc);
- G[rec].erase(pointer);
- }
- void lock(char proc, char rec) {
- if (G.find(rec) == G.end()) {
- G[rec] = {};
- }
- G[rec].pb(proc);
- /*
- так в mape - нормально сработает?
- то есть если до этого не было ключа rec - как он сдлетаь push_back в конец вектора, к-рого не существовало?
- */
- }
- void get_cycle(char v) {
- char start = v;
- vector <char> cycle = {v};
- v = G[v][0];
- while (v != start) {
- cycle.pb(v);
- v = G[v][0];
- }
- vector <char> ans;
- for (char l: cycle) {
- if (l - '0' >= 0 && l - '0' <= 9) {
- ans.pb(l);
- }
- }
- sort(ans.begin(), ans.end());
- cout << "DEADLOCK\n";
- for (char l: ans) {
- cout << l << " ";
- }
- cout << "\n";
- exit(0);
- }
- void dfs(char v) {
- if (sh) {
- cout << "v = " << v << "\n";
- cout << "clr\n";
- for (auto p: clr) {
- cout << p.first << ' ' << p.second << "\n";
- }
- }
- clr[v] = 1;
- for (char u: G[v]) {
- if (clr[u] == 0) {
- dfs(u);
- }
- else {
- get_cycle(u);
- }
- }
- clr[v] = 2;
- }
- void get_ok_ans() {
- int locked = 0;
- for (char l: all_char) {
- if (l - '0' >= 0 && l - '0' <= 9) {
- continue;
- }
- if (G.find(l) == G.end()) {
- continue;
- }
- if (!G[l].empty()) {
- locked++;
- }
- }
- cout << "NO DEADLOCK\n";
- cout << locked << "\n";
- }
- int main()
- {
- while (1) {
- char proc, rec, act;
- cin >> proc;
- if (proc == '.') {
- break;
- }
- cin >> rec >> act;
- all_char.insert(proc);
- all_char.insert(rec);
- for (char l: all_char) {
- clr[l] = 0;
- }
- cout << "LOCKING/UNLOCKING\n";
- if (act == 'L') {
- lock(proc, rec);
- } else {
- unlock(proc, rec);
- }
- if (sh) {
- check();
- }
- cout << "DFS\n";
- for (char l: all_char) {
- if (G.find(l) == G.end()) {
- continue;
- }
- if (clr[l] == 0) {
- dfs(l);
- }
- }
- }
- get_ok_ans();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement