心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0
My graph theory is poor.I had a better understanding of toposort through this problem.
Here is my code:
#include<iostream>
#include
<string.h>
#define maxn 107
using namespace std;
long n,counter,path[maxn];
bool used[maxn],g[maxn][maxn];
void topo(long s)
{
    
for(long i=1;i<=n;i++)
        
if(!used[i]&&g[s][i])
            topo(i);
    used[s]
=true;
    path[counter]
=s;
    counter
--;
}
int main()
{
    
/*
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    //
*/
    cin
>>n;
    memset(g,
false,sizeof(g));
    
for(long i=1;i<=n;i++)
    {
        
long t;
        
while(cin>>t&&t)
            g[i][t]
=true;
    }
    
//  Input
    counter=n;
    memset(used,
false,sizeof(used));
    
for(long i=1;i<=n;i++)
        
if(!used[i])
            topo(i);
    
//  Topo Sort
    for(long i=1;i<=n;i++)
    {
        
if(i>=2) cout<<" ";
        cout
<<path[i];
    }
    cout
<<endl;
    
//  Output
return 0;
}


posted on 2010-09-21 20:47 lee1r 阅读(128) 评论(0)  编辑 收藏 引用 所属分类: 题目分类:图论

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理