您的位置:首页 > 其它

HDU 1285 确定比赛名次

2012-04-29 10:54 239 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1285

拓扑排序 小心重边

View Code

#include <cstdio>
#include <cstring>
using namespace std;

const int N=510;
int g

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