您的位置:首页 > 其它

hdu 1285 确定比赛名次 //简单拓扑排序

2016-04-25 11:22 246 查看
题目链接

题意:给出n个点,m个条有向边,求字典序最小的拓扑排序。

入度为0的输出,去边。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 550

using namespace std;

int n,m,mp

,in
;

void toposort()
{
int k=0;
while(k<n)
{
for(int i=1;i<=n;i++)
if(in[i]==0)
{
in[i]--;
cout<<i;
if((++k)==n)  cout<<endl;
else cout<<" ";
for(int j=1;j<=n;j++)   in[j]-=mp[i][j];
break;
}
}
}

int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(in,0,sizeof(in));
memset(mp,0,sizeof(mp));
for(int i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
if(!mp[u][v])
mp[u][v]=1,in[v]++;
}
toposort();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: