# 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

rockdominates over thescissor,scissordominates over thepaperandpaperdominates over therock.The game can be modelled using a directed graph as below, in which the directededge (a, b)shows thatadominates overb.

**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++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to check if the given directed` `// graph has same structure to Rock-Paper-Scissor` `string similarGame(vector<vector<string> > arr)` `{` ` ` `// Stores indegree of each node` ` ` `unordered_map<string, ` `int` `> indegree;` ` ` `// Stores outdegree of each node` ` ` `unordered_map<string, ` `int` `> outdegree;` ` ` `// Stores all nodes` ` ` `set<string> st;` ` ` `// Traverse through the edges` ` ` `for` `(` `int` `i = 0; i < arr.size(); i++) {` ` ` `// Check presence of self loop` ` ` `if` `(arr[i][0] == arr[i][1])` ` ` `return` `"No"` `;` ` ` `// Insert the node arr[i][0] and arr[i][1]` ` ` `// into the set` ` ` `st.insert(arr[i][0]);` ` ` `st.insert(arr[i][1]);` ` ` `// Increment the outdegree and indegree of` ` ` `// nodes arr[i][0] and arr[i][1]` ` ` `outdegree[arr[i][0]]++;` ` ` `indegree[arr[i][1]]++;` ` ` `}` ` ` `// Check base conditions` ` ` `if` `(outdegree.size() != 3 || indegree.size() != 3` ` ` `|| st.size() != 3)` ` ` `return` `"No"` `;` ` ` `// Traverse the array outdegree` ` ` `for` `(` `auto` `it : outdegree) {` ` ` `if` `(it.second != 1)` ` ` `return` `"No"` `;` ` ` `}` ` ` `// Traverse the array indegree` ` ` `for` `(` `auto` `it : indegree) {` ` ` `if` `(it.second != 1)` ` ` `return` `"No"` `;` ` ` `}` ` ` `// Return` ` ` `return` `"Yes"` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given Input` ` ` `vector<vector<string> > arr = { { ` `"Snake"` `, ` `"Water"` `},` ` ` `{ ` `"Water"` `, ` `"Gun"` `},` ` ` `{ ` `"Gun"` `, ` `"Snake"` `} };` ` ` `// Function Call` ` ` `cout << similarGame(arr);` ` ` `return` `0;` `}` |

## Python3

`# Python 3 program for the above approach` `# Function to check if the given directed` `# graph has same structure to Rock-Paper-Scissor` `def` `similarGame(arr):` ` ` `# Stores indegree of each node` ` ` `indegree ` `=` `{}` ` ` `# Stores outdegree of each node` ` ` `outdegree ` `=` `{}` ` ` `# Stores all nodes` ` ` `st ` `=` `set` `()` ` ` `# Traverse through the edges` ` ` `for` `i ` `in` `range` `(` `len` `(arr)):` ` ` `# Check presence of self loop` ` ` `if` `(arr[i][` `0` `] ` `=` `=` `arr[i][` `1` `]):` ` ` `return` `"No"` ` ` `# Insert the node arr[i][0] and arr[i][1]` ` ` `# into the set` ` ` `st.add(arr[i][` `0` `])` ` ` `st.add(arr[i][` `1` `])` ` ` `# Increment the outdegree and indegree of` ` ` `# nodes arr[i][0] and arr[i][1]` ` ` `if` `arr[i][` `0` `] ` `in` `outdegree:` ` ` `outdegree[arr[i][` `0` `]] ` `+` `=` `1` ` ` `else` `:` ` ` `outdegree[arr[i][` `0` `]] ` `=` `0` ` ` `if` `arr[i][` `1` `] ` `in` `indegree:` ` ` `indegree[arr[i][` `1` `]] ` `+` `=` `1` ` ` `else` `:` ` ` `indegree[arr[i][` `1` `]] ` `=` `0` ` ` `# Check base conditions` ` ` `if` `(` `len` `(outdegree) !` `=` `3` `and` `len` `(indegree) !` `=` `3` `and` `len` `(st) !` `=` `3` `):` ` ` `return` `"No"` `;` ` ` `# Traverse the array outdegree` ` ` `for` `key,value ` `in` `outdegree.items():` ` ` `if` `(value ` `=` `=` `1` `):` ` ` `return` `"No"` ` ` `# Traverse the array indegree` ` ` `for` `key,value ` `in` `indegree.items():` ` ` `if` `(value ` `=` `=` `1` `):` ` ` `return` `"No"` ` ` `# Return` ` ` `return` `"Yes"` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `# Given Input` ` ` `arr ` `=` `[[` `"Snake"` `, ` `"Water"` `],[` `"Water"` `, ` `"Gun"` `],[` `"Gun"` `, ` `"Snake"` `]]` ` ` `# Function Call` ` ` `print` `(similarGame(arr))` ` ` ` ` `# This code is contributed by SURENDRA_GANGWAR.` |

**Output**

Yes

**Time Complexity:** O(N)**Auxiliary Space: **O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.