Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.2 kB
4
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

class LockSystem {
private:
    int N, m, Q;
    unordered_map<string, int> indexOf; // To get index of node by name
    vector<string> node;
    unordered_map<string, int> isLocked; // this is a map that stores if a specific node is locked or not
    unordered_map<string, set<string>> lockedChildList; // this variable stores list of descendents of a node that are locked

    int getParent(int i) {
        if(i==0) return -1;
        return (i-1)/m;
    }

public:
    LockSystem(int N, int m, int Q, vector<string> node) : N(N), m(m), Q(Q), node(node) {
        for(int i=0; i<N; i++){
            indexOf[node[i]] = i; // Assigned every node one index, so that finding its parent became easy
        }
    }

    string lock(string X, int id) {
        if(isLocked.find(X) != isLocked.end()) return "false";
        if(lockedChildList[X].size() > 0) return "false"; 
        int i = indexOf[X]; 
        while(i!=-1){
            if(isLocked.find(node[i]) != isLocked.end()) return "false"; 
            i = getParent(i);
        }
        i = getParent(indexOf[X]); 
        while(i!=-1){
            lockedChildList[node[i]].insert(X);
            i = getParent(i);
        }
        isLocked[X] = id;
        return "true";
    }

    string unlock(string X, int id) {
        if(isLocked.find(X) == isLocked.end()) return "false"; 
        int lockedByID = isLocked[X]; 
        if(lockedByID != id) return "false"; 
        isLocked.erase(X); 
        int i = getParent(indexOf[X]); 
        while(i!=-1){
            lockedChildList[node[i]].erase(X);
            i = getParent(i);
        }
        return "true";
    }

    string upgradeLock(string X, int id) {
        int i = indexOf[X];
        if(isLocked.find(X)!=isLocked.end()) return "false";
        set<int>ids;
        for(auto it = lockedChildList[X].begin(); 
            it != lockedChildList[X].end(); 
            it++) {
                ids.insert(isLocked[(*it)]);
            }
        if(ids.size() > 1) return "false";
        if(ids.size() == 0){ 
            return "false";
        }
        int tid = *(ids.begin());
        set<string>temp(lockedChildList[X]);
        for(auto it = temp.begin(); 
            it != temp.end(); 
            it++) {
                unlock((*it), id);
            }
        unlock(X,id);
        return lock(X,id); 
    }
};

int main() {
    int N, m, Q;
    cin >> N >> m >> Q; 
    vector<string> node(N);
    for(int i=0; i<N; i++){
        cin >> node[i]; 
    }
    LockSystem lockSystem(N, m, Q, node);
    while(Q--){
        int operationType; 
        string nodeName; 
        int userID;
        cin >> operationType >> nodeName >> userID;
        if(operationType==1){
            cout << lockSystem.lock(nodeName, userID) << endl;
        }
        else if(operationType==2){
            cout << lockSystem.unlock(nodeName, userID) << endl;
        }
        else{
            cout << lockSystem.upgradeLock(nodeName, userID) << endl;
        }
    }
    return 0;
}
Leave a Comment