您的位置:首页 > 其它

hdu 1285 确定比赛名次

2016-05-01 12:53 253 查看
几个月前学了的邻接表是时候复习一下了。。

从最简单的做起。。

邻接表比邻接矩阵快了2倍啊。。 好厉害

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
struct node
{
int who;
int cnt;
node *next;
}way[510];
int find(int a,int b)
{
for(node *p=way[a].next;p;p=p->next)
{
if(p->who==b)
{
return 1;
}
}
return 0;
}
void add(int a,int b)
{
if(!find(a,b))
{
way[b].cnt++;
node *p=new node;
p->next=way[a].next;
p->who=b;
way[a].next=p;
}
}
int main()
{
int n,m;
while(cin>>n>>m)
{
for(int i=1;i<=n;i++)
{
way[i].cnt=0;
way[i].next=NULL;
}
int a,b;
while(m--)
{
cin>>a>>b;
add(a,b);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(way[j].cnt==0)
{
way[j].cnt--;
if(i!=n)
{
cout<<j<<" ";
}
else
{
cout<<j<<endl;
}
for(node *p=way[j].next;p;p=p->next)
{
way[p->who].cnt--;
}
break;
}
}
}
}
return 0;
}


邻接矩阵就不放了。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  邻接表 hdu