Coede

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

#include <iostream>

#include<bits/stdc++.h>
using namespace std;

struct Node{
string val;
Node* parent;
vector<Node*> children;
bool isLocked;
int lcc;
int locked_by_id;
unordered_map<Node*,int> children_locked;

Node(string val, Node* parent)


{
this->val = val;
this->parent = parent;
this->isLocked = false;
vector<Node*>children;
this->children = children;
this->lcc = 0;
this->locked_by_id = -1;
unordered_map<Node*,int> children_locked;
this->children_locked = children_locked;
}
};

bool is_locking_allowed(Node* root)


{
if(root->lcc > 0) // checks for count of locks on children (locked children
count)
return false;

Node* temp = root->parent; /// checks for locks on parents


while(temp)
{
if(temp->isLocked)
{
return false;
}
temp = temp->parent;
}

return true;
}

bool lock(int id, Node* root)


{
if(root->isLocked)
{
return false;
}

if(is_locking_allowed(root)) // checks for locks on parents and children


{
root->isLocked = true; /// lock the node
root->locked_by_id = id;
// inform parents to inc lcc
Node* temp = root->parent;
while(temp)
{
temp->lcc+=1;
temp->children_locked[root] = id;
temp = temp->parent;
}

return true;
}
else
{
return false;
}

bool unlock(int id, Node* root)


{
if(!root->isLocked) // if already unlocked
{
return false;
}

if(root->locked_by_id!=id) /// someone else trying to unlock


{
return false;
}

root->isLocked = false;
root->locked_by_id = -1;

// now reduce the lcc of parents


Node* temp = root->parent;
while(temp)
{
temp->lcc-=1;
temp->children_locked.erase(root);
temp = temp->parent;
}
return true;
}

bool upgrade(int id, Node* root)


{
if(root->isLocked)
return false;

if(root->children_locked.size()==0) /// atleast 1 locked child should be


there
return false;

// 1. check if children_locked->values have the same id as passed in the


upgrade function
unordered_map<Node*,int>::iterator itr;
vector<Node*> children_to_be_unlocked;
for(itr=root->children_locked.begin(); itr!=root->children_locked.end(); itr++)
{
if(itr->second!=id) /// trying to upgrade on someone else's lock - not
allowed
{
return false;
}
children_to_be_unlocked.push_back(itr->first);

// 2. unlock all children


int total_children_unlocked = root->children_locked.size();
for(int k=0; k<children_to_be_unlocked.size(); k++)
{
if(!unlock(id,children_to_be_unlocked[k]))
{
return false;
}
}

// 3. Lock root now


if(!lock(id,root))
{
return false;
}

return true;

int main()
{
// int n,m,q;
// cin>>n>>m>>q;
// vector<string> arr;
// vector<string> queries;
// for(int i=0; i<n; i++)
// {
// string s;
// cin>>s;
// arr.push_back(s);
// }
// for(int i=0; i<q; i++)
// {
// string qu;
// cin>>qu;
// queries.push_back(qu);
// }

////////////////////// HARD CODED INPUT - TO BE DELETED


int n = 7;
int m = 2;
int q = 5;
vector<string> arr;
arr.push_back("World");
arr.push_back("Asia");
arr.push_back("Africa");
arr.push_back("China");
arr.push_back("India");
arr.push_back("Souafrica");
arr.push_back("Egypt");
vector<string> queries;
queries.push_back("1 China 9");
queries.push_back("1 India 9");
queries.push_back("3 Asia 9");
queries.push_back("2 India 9");
queries.push_back("1 Asia 9");

/////////////////////////////////////////////////////////////////////// TREE
AND MAP CREATION

unordered_map<string,Node*> m1;

Node* root = new Node(arr[0], NULL);


m1[arr[0]] = root;

int i = 0;
int j = 1;

while(j<n)
{
for(int k = 0; k<m; k++)
{
if(j<n)
{
Node* child = new Node(arr[j], m1[arr[i]]);
m1[arr[j]] = child;
m1[arr[i]]->children.push_back(child);
j++;
}
else
{
break;
}
}
i++;
}

//////////////////////////////////////////////////////// TREE CREATION


COMPLETED - QUERY RESULTS BEGIN

for(int que=0; que<q; que++)


{
string s = queries[que];
int l = s.length();
char operation = s[0];
int id = s[s.length()-1]-'0';
string valx = s.substr(2,l-4);
if(operation=='1')
{
/// lock
cout<<lock(id,m1[valx])<<"\n";
}
else if(operation=='2')
{
// unlock
cout<<unlock(id,m1[valx])<<"\n";
}
else if(operation=='3')
{
//upgrade
cout<<upgrade(id,m1[valx])<<"\n";
}
else
{
cout<<"test";
}

///////////////////////////////////////////////////////// PRINTING
cout<<"\nDETAILS----------------------------------\n";
unordered_map<string,Node*>::iterator itr;
for(itr=m1.begin(); itr!=m1.end(); itr++)
{

cout<<itr->first<<"---\n";
cout<<"isLocked: "<<itr->second->isLocked<<",LCC: "<<itr->second->lcc<<",
locked_by_id: "<<itr->second->locked_by_id;
unordered_map<Node*,int>::iterator itr2;
cout<<", Children Locked[";
for(itr2=itr->second->children_locked.begin(); itr2!=itr->second-
>children_locked.end(); itr2++)
{
cout<<itr2->first->val<<"-"<<itr2->second<<", ";
}
cout<<"]";
// for(int k=0; k<itr->second->children.size(); k++)
// {
// cout<<itr->second->children[k]<<",";
// }
cout<<"\n";
}

You might also like