HDU3926 判断图同构(只有环、链)

http://acm.hdu.edu.cn/showproblem.php?pid=3926


用并查集合并,得到若干个集合,比较两个图中集合是否完全相同,包括集合的个数,集合中点的个数,集合中点的构成是链还是环。

const int Max_N = 10008 ;

struct  Graph{
        int son ;
        int is_ring ;
        friend bool operator < (const Graph &A , const Graph &B){
             if(A.son != B.son)
                return A.son < B.son ;
             else
                return A.is_ring < B.is_ring ;
        }
};
Graph g1[Max_N] , g2[Max_N] ;

int  find_father(int x , int father[]){
     if(x == father[x])
         return x ;
     else
         return father[x] = find_father(father[x] , father) ;
}

void Merg(int x , int y , int father[] , Graph g[]){
     int f_x = find_father(x , father) ;
     int f_y = find_father(y , father) ;
     if(f_x == f_y){
          g[f_x].is_ring = 1 ;
          return ;
     }
     if(g[f_x].son >= g[f_y].son){
          father[f_y] = f_x ;
          g[f_x].son += g[f_y].son ;
     }
     else{
          father[f_x] = f_y ;
          g[f_y].son += g[f_x].son ;
     }
}

int  N ;
int  father1[Max_N] , father2[Max_N] ;

void Init(){
     for(int i = 1 ; i <= N ; i++){
          father1[i] = father2[i] = i ;
          g1[i].son = g2[i].son = 1 ;
          g1[i].is_ring = g2[i].is_ring = 0 ;
     }
}

int  Judge(){
     sort(g1+1 , g1+1+N) ;
     sort(g2+1 , g2+1+N) ;
     for(int i = 1 ; i <= N ; i++){
         if(g1[i].son != g2[i].son ||(g1[i].son == g2[i].son && g1[i].is_ring != g2[i].is_ring))
              return  0 ;
     }
     return 1 ;
}

int  main(){
     int T , i , u  , v ,M , k = 1 ;
     cin>>T ;
     while(T--){
          scanf("%d%d" ,&N ,&M) ;
          Init() ;
          for(i = 1 ; i <= M ; i++){
              scanf("%d%d" ,&u ,&v) ;
              Merg(u , v ,father1 , g1) ;
          }
          scanf("%d%d" ,&N ,&M) ;
          for(i = 1 ; i <= M ; i++){
              scanf("%d%d" ,&u ,&v) ;
              Merg(u , v ,father2 , g2) ;
          }
          printf("Case #%d: %s\n", k++ , Judge() ? "YES" : "NO");
     }
     return 0 ;
}



你可能感兴趣的:(C++足迹)