133. Clone Graph (Graph, Map; DFS)

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

OJ‘s undirected graph serialization:

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 and 2.
  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
         /          \_/
struct UndirectedGraphNode {
    int label;
    vector<UndirectedGraphNode *> neighbors;
    UndirectedGraphNode(int x) : label(x) {};
};

class Solution {
public:
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        if(!node) return NULL;
        UndirectedGraphNode *current;
        map<UndirectedGraphNode*,UndirectedGraphNode*> flag; //前一个元素是节点在原Graph中的地址,后一个元素是节点在新拷贝的图中的位置
        UndirectedGraphNode *root = cloneNode(node,flag);
        return root;
    }

    UndirectedGraphNode * cloneNode(UndirectedGraphNode *source, map<UndirectedGraphNode*,UndirectedGraphNode*> &flag)
    {
        if(flag.find(source)!= flag.end()) return flag[source];

        //如果map中没有该节点,那么创建该节点
        UndirectedGraphNode *target = new UndirectedGraphNode(source->label);
        flag[source] = target;

        for(vector<UndirectedGraphNode *>::iterator it = source->neighbors.begin(); it < source->neighbors.end(); it++ )
        {
            UndirectedGraphNode *newRoot = cloneNode(*it, flag); //深度优先,先递归处理它的某一个邻居,再处理其他邻居
            target->neighbors.push_back(newRoot);
        }
        return target; //返回已经处理好的节点
    }
};
时间: 2024-12-14 07:19:42

133. Clone Graph (Graph, Map; DFS)的相关文章

Graph 133. Clone Graph in three ways(bfs, dfs, bfs(recursive))

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neigh

133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node lab

133. Clone Graph (Graph)

1 class Solution { 2 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 3 Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<>(); 4 return dfs(node, map); 5 } 6 7 8 public UndirectedGraphNode dfs(UndirectedGraphNode

193 - Graph Coloring(DFS)

题目:193 - Graph Coloring 题目大意:给出一个图,图里面有点和边,要求相邻的点不可以都是黑色的,问怎样上色黑色的点最多的,给出字典序最大的那种组合情况. 解题思路:dfs每个点是黑是白,将黑的点保存记录下来,然后下次再试探某个点是黑点的时候,就看看这个点和之前的那些点有没有相邻,相邻就表示这个点不是黑点.每个试探的点只需要是这个点之后的点就可以了,因为前面的点已经试探过了. 代码: #include <stdio.h> #include <string.h> c

HDU 4921 Map DFS+状态压缩+乘法计数

算最多十条链,能截取某前缀段,每种方案都可以算出一个权值,每种方案的概率都是总数分之一,问最后能构成的所有可能方案数. 对计数原理不太敏感,知道是DFS先把链求出来,但是想怎么统计方案的时候想了好久,其实因为只能取某个链的前缀,所以直接取链长加+1 然后相乘即可,当然因为会出现都是空的那种情况,要去掉,全部乘完之后,要-1 然后就是算权值了,权值等于当前加进来的点的总和 以及 等级相同的点的加成,并不是特别好算,这时候考虑每个状态下的点对全局的贡献,对,就是这个思想,用状态压缩来表示状态,然后这

133. Clone Graph

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each n

leetcode 133. Clone Graph ----- java

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neigh

133. Clone Graph (3 solutions)——无向无环图复制

Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label an

[leedcode 133] Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neigh