URAL 1022. Genealogical tree

Problem url: http://acm.timus.ru/problem.aspx?space=1&num=1022

This is a typical problem that uses topological sorting. At first I misunderstood it as a normal internal sorting issue and got wrong through sort tree. Later through some hints, I got AC via topo sort algorithm. Here are two AC programs(use different format of topo sorting). In both cases, I use Adjacdent Matrix to represent the Directed Acycline Graph

** using Depth-First Traverse

 

/*   time:0.001s
    memory: 267 KB 


    using Adjacent Matrix to perform topological sort against a directed acycline graph(through depth first search)

    
*/

#include 
< cstdio >
#include 
< iostream >


#define  MAXID 100   // max vertex number


using   namespace  std;


bool  g_gene[MAXID + 1 ][MAXID + 1 ];     // Matrix of the graph
bool  visited[MAXID + 1 ];     // array used to record whether vertex has been visited
int  stack[MAXID];  //  used as stack


int  N  = 0 ;   // number of martians

int  stop  =   0 // top of stack



// Depth First Search against a Graph from the given vertex
void  DFS( int  id)
{
    
int  j =   0 ;

    visited[id] 
=   true ;

    
for (j = 1 ;j <= MAXID; ++ j)
    {
        
if (g_gene[id][j]  &&   ! visited[j]) DFS(j);
    }

    
// push the current vertext into stack
    stack[stop ++ =  id;
}


void  main()
{
int  i  =   0 ;
int  curkid  =   0 ;

memset(g_gene, 
0 sizeof ( bool ) *  (MAXID + 1 *  (MAXID + 1 ));
memset(visited, 
0 sizeof ( bool *  (MAXID + 1 ));

scanf(
" %d " & N);

for (i = 1 ;i <= N; ++ i)
{

    
do
    {
        scanf(
" %d " & curkid);
        
        
if (curkid  !=   0 )    
        {
            g_gene[i][curkid] 
=   1 ;
        }

    }
while (curkid  !=   0 );
}


stop 
=   0 ;

for (i = 1 ;i <= N; ++ i)
{
    
if ( ! visited[i]) DFS(i);
}


// print out the pushed nodes reversely
for (i = N - 1 ;i >= 0 ; -- i)
{
    printf(
" %d  " , stack[i]);
}

}

 

** use Standard Topo Sort approach:

 

/*   time:0.001s
    memory: 267 KB 


    using Adjacent Matrix to perform topological sort against a directed acycline graph(through standard topo sort)

    
*/

#include 
< cstdio >
#include 
< iostream >


#define  MAXID 100   // max vertex number


using   namespace  std;


bool  g_gene[MAXID + 1 ][MAXID + 1 ];     // Matrix of the graph
int  indegree[MAXID + 1 ];  // store the in-degree of each vertex

int  stack[MAXID];  //  used as stack


int  N  = 0 ;   // number of martians

int  stop  =   0 // top of stack





void  main()
{
int  i  =   0 ;
int  j  =   0 ;
int  curkid  =   0 ;

memset(g_gene, 
0 sizeof ( bool ) *  (MAXID + 1 *  (MAXID + 1 ));
memset(indegree,
0 sizeof ( int ) *  (MAXID + 1 ));


scanf(
" %d " & N);

for (i = 1 ;i <= N; ++ i)
{

    
do
    {
        scanf(
" %d " & curkid);
        
        
if (curkid  !=   0 )    
        {
            g_gene[i][curkid] 
=   true ;
            indegree[curkid]
++ ;
        }

    }
while (curkid  !=   0 );
}


stop 
=   0 ;

for (i = 1 ;i <= N; ++ i)
{
    
if (indegree[i] == 0 ) stack[stop ++ =  i;
}


// topo sort

while (stop  >   0 )
{
    i 
=  stack[ -- stop];

    printf(
" %d  " , i); 

    
for (j = 1 ;j <= N; ++ j)
    {
        
if (g_gene[i][j])
        {
            
-- indegree[j];
        
            
if (indegree[j]  ==   0 ) stack[stop ++ =  j;
        }
    }

}



}



 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值