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]);
}
}
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;
}
}
}
}
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;
}
}
}
}