• Clone Graph


    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.

    How we serialize an undirected graph:

    Nodes are labeled uniquely.

    We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.

    As an example, consider the serialized graph {0,1,2#1,2#2,2}.

    The graph has a total of three nodes, and therefore contains three parts as separated by #.

    1. First node is labeled as 0. Connect node 0 to both nodes 1 and2.
    2. Second node is labeled as 1. Connect node 1 to node 2.
    3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

    Visually, the graph looks like the following:

       1
      / 
     /   
    0 --- 2
         / 
         \_/
    
    Example

    return a deep copied graph.

     1 /**
     2  * Definition for undirected graph.
     3  * struct UndirectedGraphNode {
     4  *     int label;
     5  *     vector<UndirectedGraphNode *> neighbors;
     6  *     UndirectedGraphNode(int x) : label(x) {};
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     /**
    12      * @param node: A undirected graph node
    13      * @return: A undirected graph node
    14      */
    15     UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
    16         // write your code here
    17         
    18         // suppose the value of nodes are unique.
    19         // For every node, construct a new node of the same value. For its neighbours, first check if the neighbour has been created (value equals or not), if not, create a node and push into a queue, if so, link the it to the neighbor. 
    20         // To check whether the neighbour has been visited, maintain a hash map to map the original node to a newly created node.
    21         if (!node) return node;
    22         queue<UndirectedGraphNode* > qu;
    23         qu.push(node);
    24         unordered_map<UndirectedGraphNode*, UndirectedGraphNode* > um;
    25         UndirectedGraphNode* result = new UndirectedGraphNode(node->label);
    26         um[node] = result;
    27         while (!qu.empty()) {
    28             UndirectedGraphNode* temp = qu.front();
    29             qu.pop();
    30             UndirectedGraphNode* move = um[temp];
    31             
    32             for (auto neighbor : temp->neighbors) {
    33                 // if the neighbor is not created, created it and map neighbor to a new create node, update the neighbor of move with newly created node
    34                 if (um.find(neighbor) == um.end()) {
    35                     UndirectedGraphNode* newNeighbor = new UndirectedGraphNode(neighbor->label);
    36                     um[neighbor] = newNeighbor;
    37                     move->neighbors.push_back(newNeighbor);
    38                     qu.push(neighbor);
    39                 } else {
    40                     move->neighbors.push_back(um[neighbor]);
    41                 }
    42             }
    43         }
    44         return result;
    45     }
    46 };
  • 相关阅读:
    P2114 [NOI2014]起床困难综合症(二进制)
    P4577 [FJOI2018]领导集团问题
    P5290 [十二省联考2019]春节十二响(堆+启发式合并)
    P2048 [NOI2010]超级钢琴(RMQ+堆+贪心)
    P4890 Never·island(dp)
    P2617 Dynamic Rankings(树状数组套主席树)
    P5241 序列(滚动数组+前缀和优化dp)
    P3243 [HNOI2015]菜肴制作(拓扑排序)
    【LeetCode每天一题】Combination Sum II(组合和II)
    【LeetCode每天一题】Combination Sum(组合和)
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5816558.html
Copyright © 2020-2023  润新知