📜  检查给定Graph的结构是否与Rock-Paper-Scissor游戏相同

📅  最后修改于: 2021-10-25 05:14:23             🧑  作者: Mango

给定的阵列ARR [] N对代表一个有向图,其中每对字符串的表示由一对字符串的表示的节点之间的边缘字符串的,任务是检查给定的有向图的结构是否是和石头剪刀布游戏一样。如果发现为真,则打印“”。否则,打印“”。

笔记:

例子:

方法:根据观察有向图Rock-Paper-Scissor中只有三个节点,每个节点的入度和出度为一个,可以利用图的入度和出度来解决问题。请按照以下步骤解决问题。

  • 初始化{ 字符串 , int} 的两个映射,比如inDegreeoutDegree ,以存储这些映射中每个节点的入度和出度。
  • 初始化一组字符串say st并插入所有节点,即该集合中的字符串。
  • 检查任何地图的大小是否不等于3或节点集st的大小不等于3,然后打印“”。
  • 遍历两个映射并检查任何条目映射的第二个值是否不是1 ,然后打印“ No ”并中断。
  • 最后,如果以上情况都不满足,则打印“”。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the given directed
// graph has same structure to Rock-Paper-Scissor
string similarGame(vector > arr)
{
 
    // Stores indegree of each node
    unordered_map indegree;
 
    // Stores outdegree of each node
    unordered_map outdegree;
 
    // Stores all nodes
    set 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 > 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.


输出
Yes

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程