例题11.9确定比赛名次
/*
只需要把拓扑排序里存入度为0的结点的队列改成greater的优先队列
*/
#include
#include
#include
#include
#include
using namespace std;
vector graph[125000];
int indegree[501];
vector TuoPu(int n)
{
priority_queue,greater> q;
for(int i=1;i<=n;i++)
{
if(indegree[i]==0)q.push(i);
}
vector ans;
while(!q.empty())
{
int u=q.top();
q.pop();
ans.push_back(u);
for(int i=0;i {
int v=graph[u][i];
indegree[v]--;
if(indegree[v]==0)q.push(v);
}
}
return ans;
}
int main()
{
int n,m;
while(cin>>n>>m)
{
memset(graph,0,sizeof(graph));
memset(indegree,0,sizeof(indegree));
for(int i=1;i<=m;i++)
{
int from,to;
cin>>from>>to;
graph[from].push_back(to);
indegree[to]++;
}
}
vector ans=TuoPu(n);
for(int i=0;i {
cout< }
cout<
}
只需要把拓扑排序里存入度为0的结点的队列改成greater的优先队列
*/
#include
#include
#include
#include
#include
using namespace std;
vector
int indegree[501];
vector
{
priority_queue
for(int i=1;i<=n;i++)
{
if(indegree[i]==0)q.push(i);
}
vector
while(!q.empty())
{
int u=q.top();
q.pop();
ans.push_back(u);
for(int i=0;i
int v=graph[u][i];
indegree[v]--;
if(indegree[v]==0)q.push(v);
}
}
return ans;
}
int main()
{
int n,m;
while(cin>>n>>m)
{
memset(graph,0,sizeof(graph));
memset(indegree,0,sizeof(indegree));
for(int i=1;i<=m;i++)
{
int from,to;
cin>>from>>to;
graph[from].push_back(to);
indegree[to]++;
}
}
vector
for(int i=0;i
cout<
cout<
}
全部评论
相关推荐
09-24 00:31
北京理工大学 嵌入式硬件工程师 点赞 评论 收藏
分享