Question

In this code I want to check whether two trees are isomorphic or not. The algorithm that I've used is to get two strings with the help of DFS algorithm, sorting them and then comparing them.

This code works in visual studio 2010 but when I try to submit in UVA online judge I keep getting the Run Time Error and since I'm not a professional programmer I can't understand why.

I was also trying to optimize my code so I used short instead of int and scanf - printf instead of cin and cout.

I would appreciate some help on how to fix my code and also how to optimize it.

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cstdio>

using namespace std;

typedef vector<short> vi;  
typedef vector<vi> vvi;  

short V=0; // number of vertices for the trees.
string str;
string strrr;  
short  max1=0; 
short  max2=0; 
short  temp=0; 





short GetDeg1(vvi t1, short n1)  // for the first tree
{

    vi deg(n1+1);



    for( short i=1; i<n1+1 ;i++)    // to make a vector named deg that has the degree of each vertex
    {
        deg[i]= t1[i].size(); 
        if(deg[i]>max1)
        { 
            temp=i;                  // temp has the index of the vertex with the maximum degree
            max1=deg[i];
        }


    }


    return temp;

}



 short GetDeg2(vvi t2, short n2)  // for the second tree
{
    vi deg(n2+1);


    for( short i=1; i<n2+1 ;i++)
    {
        deg[i]= t2[i].size();
        if( deg[i]>max2)
          {
            temp=i;
            max2=deg[i];
          }


    }



    return temp;

}


string DFSUtil(vvi t ,short v, bool visited[])
{

    visited[v] = true;   // Mark the current node as visited


     // Recur for all the vertices adjacent to this vertex

    vector<string >strnode ; 

    for(short i = 0 ; i< (short)(t[v].size() ); ++i)
     {  

         if(!visited[t[v][i]])
        {   string a;
            a = DFSUtil(t, t[v][i], visited);
            strnode.push_back(a) ; 
         }
    }


      sort (strnode.begin(), strnode.end());
      str="(";
      for(short t=0; t< (short)(strnode.size()); t++)
         str+= strnode[t];


      str+=")";
     return str;

}


string DFS(vvi t, short v)
{

    bool *visited = new bool[V+1];       // Mark all the vertices as not visited
    for(short  i = 1; i < V+1; i++)
        visited[i] = false;


    strrr= DFSUtil(t, v, visited);     // Call the recursive helper function to print DFS traversal

    return strrr;
}


int main()

{
    while(cin>>V)
    {



    max1=0; 
    max2=0;  

    temp=0; 

    string str1; 
    string str2; 

    strrr.clear();

    str1.clear();
    str2.clear();

    str.clear();


    short  v1 ;
    short  v2;  


    vvi Tree1(V+1);   

    for( short  i=0; i<V-1; i++)  
    {

        scanf("%d",&v1);
        scanf("%d",&v2);

        Tree1[v1].push_back(v2);
        Tree1[v2].push_back(v1);

    }


    vvi Tree2(V+1);  

    for( short i=0; i<V-1; i++) 
    {

        scanf("%d",&v1);
        scanf("%d",&v2);


        Tree2[v1].push_back(v2);
        Tree2[v2].push_back(v1);
    }


    cout<<endl;

    short temp1=0; 
    short temp2=0;  

    temp1= GetDeg1(Tree1, V); 


    temp=0;

    temp2= GetDeg2(Tree2, V); 

    str1=DFS(Tree1, temp1);

    str.clear();
    strrr.clear();


    str2=DFS(Tree2, temp2);


    if( str1.compare(str2) == 0)
        printf("S\n");

    else
        printf("N\n");

  }

    return 0;

}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top