确定比赛名次(拓扑排序 +有限对列)
/**
优先队列加拓扑排序
*/
include<stdio.h>
include
include
include<string.h>
using namespace std;
vector
int du[550];
void creatLinJieBiao(int x,int y)
{
edge[x].push_back(y);
du[y]++;
}
struct myComp///不是结构体时 定义优先队列的方法
{
bool operator ()(int a,int b)///小的优先出队
{
return a>b;
}
};
void topSort(int n)
{
priority_queue<int,vector
for(int i=1; i<=n; i++)
{
if(du[i]==0)
q.push(i);
}
int op=0;
while(!q.empty())
{
int a=q.top();
q.pop();
if(op==0)
printf("%d",a);
else printf(" %d",a);
op=1;
for(int i=0; i<edge[a].size(); i++)
{
du[edge[a][i]]--;
if(du[edge[a][i]]==0)
{
q.push(edge[a][i]);
}
}
}
printf("\n");
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(du,0,sizeof(du));
memset(edge,0,sizeof(edge));///注意初始化
for(int i=1; i<=m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
creatLinJieBiao(x,y);
}
topSort(n);
}
return 0;
}
/**
5 5
3 4
1 5 2 3
5 4 1 2
*/
梦里不知身是客,一晌贪欢。