- Graph

Check whether the structure of given Graph is same as the game of Rock-Paper-Scissor

Given an array arr[] of N pairs of strings representing a directed graph, where each pair of the strings represents an edge between the nodes represented by the pair of strings, the task is to check whether the structure of the given directed graph is the same as that of the game of Rock-Paper-Scissor. If found true, then print “Yes“. Otherwise, print “No“.Note:In the real game of Rock-Paper-Scissor, the rock dominates over the scissor, scissor dominates over the paper and paper dominates over the rock.The game can be modelled using a directed graph as below, in which the directed edge (a, b) shows that a dominates over b. Examples:Input: arr[] = {{“Snake”, “Water”}, {“Water”, “Gun”}, {“Gun”, “Snake”}}Output: YesExplanation:  Therefore, from the above image it can be seen that the given graph structure is similar to Rock-Paper-Scissor.Input: arr[]={{“Snake”, “Water”}, {“Water”, “Gun”}, {“Gun”, “Gun”}}Output: No Approach: The problem can be solved by using the indegree and outdegree of the graphs based on the observation that in the directed graph Rock-Paper-Scissor there are only three nodes and indegree and outdegree of each node is one. Follow the steps below to solve the problem.Initialize two maps of {strings, int} say inDegree and outDegree, to store the indegree and outdegree of each node in these maps.Initialize a set of strings say st and insert all the nodes i.e. strings in this set.Check if the size of any map is not equal to 3 or the size of the set st of nodes is not equal to 3, then print “No“.Iterate over both the maps and check if the second value of any entry map is not 1, then, print “No” and break.Finally, if none of the above cases satisfy, then print “Yes“.Below is the implementation of the above approach:       C++#include using namespace std;  string similarGame(vector arr){          unordered_map indegree;          unordered_map outdegree;          set st;          for (int i = 0; i < arr.size(); i++) {                  if (arr[i][0] == arr[i][1])            return "No";                          st.insert(arr[i][0]);        st.insert(arr[i][1]);                            outdegree[arr[i][0]]++;        indegree[arr[i][1]]++;    }          if (outdegree.size() != 3 || indegree.size() != 3        || st.size() != 3)        return "No";          for (auto it : outdegree) {        if (it.second != 1)            return "No";    }          for (auto it : indegree) {        if (it.second != 1)            return "No";    }          return "Yes";}  int main(){          vector arr = { { "Snake", "Water" },                                    { "Water", "Gun" },                                    { "Gun", "Snake" } };          cout