Clone graph the case {-1,1#1} is wrong


  • 0
    M

    #include <set>
    #include <vector>
    #include <stdlib.h>
    #include <map>
    #include <iostream>
    using namespace std;
    struct UndirectedGraphNode {
    int label;
    vector<UndirectedGraphNode *> neighbors;
    UndirectedGraphNode(int x) : label(x) {};
    };

    class Solution {
    public:
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node)
    {
    if(node ==NULL)
    return NULL;
    set<UndirectedGraphNode *> flags;
    tranvel(node,flags );
    set<UndirectedGraphNode > flags2;
    getNeighbor(node,flags2);
    return reflect[node];
    //
    }
    void tranvel(UndirectedGraphNode * node,set<UndirectedGraphNode
    > &flags)
    {
    if(flags.count(node)==0){

              flags.insert(node);
              UndirectedGraphNode *  temp=(UndirectedGraphNode * )malloc(sizeof(UndirectedGraphNode  )); 
              temp->neighbors.clear();
              temp->label=node->label;
              reflect[node]=temp;
              cout<< node->label<<endl;
    
              for(vector<UndirectedGraphNode * >::iterator  it= node->neighbors.begin();it!=node->neighbors.end();it++)
              {   
                  tranvel(*it,flags);
    
             }   
             }   
     
          }   
          void getNeighbor(UndirectedGraphNode * node,set<UndirectedGraphNode*> &flags)
          {   
              if(flags.count(node)==0)
              {   
                  flags.insert(node);
                   vector<UndirectedGraphNode *> neighbors;
                   for( vector<UndirectedGraphNode *> ::iterator it=node->neighbors.begin();it!=node->neighbors.end();it++)
                   {   
                       neighbors.push_back(reflect[*it]);
                       cout<<"node is "<<node->label<<" neighbors is"<< reflect[*it]->label<<endl;
                   }   
                  
                   reflect[node]->neighbors=neighbors;
                     for(vector<UndirectedGraphNode * >::iterator  it= node->neighbors.begin();it!=node->neighbors.end();it++)
                       {   
                 //         reverse(*it,flags);
                          getNeighbor(*it,flags); 
                      }   
    
              }
          }
      private:
          map<UndirectedGraphNode *,UndirectedGraphNode *> reflect;
    

    };


  • 0
    M

    i test myself . the answer is true , but the platform tells me wrong.


  • 0
    M

    Why did you answer your own question, when this isn't an answer? This kind of thing should go in the question.


  • 0
    M

    can you answer my question?


  • 0
    M

    What does the platform say is the problem? Just wrong answer? What input was it on? What answer did it expect? What answer did you give?

    These are all things that help discover where the mistake is.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.