9 degree question 1449: Determine the competition rankings, 9 degrees 1449

Source: Internet
Author: User

9 degree question 1449: Determine the competition rankings, 9 degrees 1449

Obviously, the Topology Sorting problem will be found to be easy to understand after drawing a graph. At the beginning, I did not quite understand it myself. After drawing a graph, the idea is very clear.

Reprinted please indicate this article address http://blog.csdn.net/yangnanhai93/article/details/41226221

Question link http://ac.jobdu.com/problem.php? Pid = 1, 1449


#include <iostream>#include <memory.h>#include <queue>using namespace std;int A[501],MapCount[501][501];void TopoSort(int n){    priority_queue<int,vector<int>,greater<int> > qu;    vector<int>result;    for(int i=1;i<=n;i++)        if(A[i]==0)            qu.push(i);    int count=0;    while(!qu.empty())    {        int front=qu.top();        qu.pop();        result.push_back(front);        count++;        for (int i=1;i<=n;i++)        {            if(MapCount[front][i])            {                A[i]--;                if(A[i]==0)                    qu.push(i);            }        }    }    cout<<result[0];    for (int i=1;i<result.size();i++)    {        cout<<" "<<result[i];    }    cout<<endl;}int main(){    //freopen("data.in","r",stdin);    int m,n,x,y;    while(cin>>m>>n&&m)    {        memset(A,0,sizeof(A));        memset(MapCount,0,sizeof(MapCount));        for(int i=0;i<n;i++)        {            cin>>x>>y;            MapCount[x][y]=1;        }        for(int i=1;i<=m;i++)            for(int j=1;j<=m;j++)                if(MapCount[i][j]!=0)                    A[j]++;        TopoSort(m);    }    return 0;}/**************************************************************    Problem: 1449    User: vincent_ynh    Language: C++    Result: Accepted    Time:30 ms    Memory:2500 kb****************************************************************/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.