运行时错误 - 树同构

时间:2014-01-14 18:23:15

标签: c++

在这段代码中,我想检查两棵树是否同构。 我使用的算法是在DFS算法的帮助下得到两个字符串,对它们进行排序然后进行比较。

此代码适用于Visual Studio 2010,但当我尝试在UVA在线判断时,我不断收到运行时错误,因为我不是专业程序员,所以我无法理解为什么。

我也试图优化我的代码所以我使用short而不是int和scanf - printf而不是cin和cout。

我很感激有关如何修复代码以及如何优化代码的一些帮助。

#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;

}

0 个答案:

没有答案