/*简单的拓扑排序*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 200
int t, n, m, data[M][M];
int topo[M], c[M], data[M][M];
int dfs( int u )
{int v;c[u] = -1;for( v = 1; v <= n; v++ )if( data[u][v] ){if( c[v] < 0 )return 0;else if( !c[v] && !dfs(v) )return 0;}c[u] = 1; topo[t--] = u;return 1;
}
int main()
{int i, w, x, y, u;while( scanf( "%d%d", &n, &m ) ){if( m == 0 && n == 0 ) //因为可能n = 1; m = 0break; //最开始写的是whiel( scanf(n,m) && n, m ) WA!!memset(c, 0, sizeof(c) );memset(data, 0, sizeof(data) );while( m-- ){scanf( "%d%d", &x, &y );data[x][y] = 1;}t = n;for( u = 1; u <= n; u++ )if( !c[u] )dfs(u);for( i = 1; i <= n-1; i++ )printf( "%d ", topo[i] );printf( "%d\n", topo[n] );}return 0;
}
详细解决方案
UVA 10305
热度:62 发布时间:2024-01-11 16:41:33.0