HDU-1285-拓扑排序-确定比赛名次


点击打开题目链接

# include 
# include 
# include 
# include 
# include 
# include 
using namespace std;

const int maxn = 500 + 5;
vector  g[maxn];
int n,m,du[maxn],l[maxn];

void topsort(){
    int tot = 0;
    priority_queue  q; //使用优先队列,默认重大到小;所以存负数;
    for(int i = 1;i <= n;i++)   if(!du[i])  q.push(-i); //将入度为0的点放入队列;
    while(!q.empty()){
        int x = -q.top();q.pop();
        l[tot++] = x;
        for(int j = 0;j < g[x].size();j++){
            int t = g[x][j];
            du[t] --;
            if(!du[t])  q.push(-t);
        }
    }
}

int main()
{
    //freopen("a.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)){
        memset(du,0,sizeof(du));
        memset(l,0,sizeof(l));
        //vector数组清零;
        for(int i = 1;i <= n;i++)   g[i].clear();
        vector ::iterator it;
        for(int i = 0;i < m;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            //避免重复边计数;
            it = find(g[x].begin(),g[x].end(),y);
            if(it == g[x].end()){
                g[x].push_back(y);
                du[y] ++;
            }
        }
//        for(int i = 1;i <= n;i++){
//            for(int j = 0;j 


你可能感兴趣的:(拓扑排序)