Topology sort template + Example

Posted by gumby51 on Mon, 02 Dec 2019 05:00:50 +0100

A - very simple

Description

There are N teams (1 < = N < = 500) numbered 1, 2, 3,.... , N for the competition, after the competition, the referee committee will rank all the participating teams in turn from the front to the back, but now the referee committee can not directly obtain the competition results of each team, only know the results of each game, that is, P1 wins P2, expressed by P1, P2, P1 is in front of P2 when ranking. Now please program your ranking.  

Input

There are several groups of input, the first row in each group is two numbers N (1 < = N < = 500), m; where N is the number of teams, and M is the input data followed by M rows. In the next row M data, there are also two integers P1 in each row. P2 means that team P1 wins team P2.  

Output

Give a satisfactory ranking. When outputting, there is a space between the team numbers, and there is no space after the last one.  

Other notes: the qualified ranking may not be unique. At this time, the team with small number is in the front when output is required; the input data is guaranteed to be correct, i.e. the input data ensures that there must be a qualified ranking.  

Sample Input

4 3
1 2
2 3
4 3 
 
Sample Output

1 2 4 3 
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
typedef long long ll;
priority_queue<int,vector<int>,greater<int> > q;
int num[550];
int n,m;
int ans[550];
vector <int> v[550];
void toposort()
{
    int i,len=0;
    for(i=1;i<=n;i++)
    {
        if(num[i]==0)
            q.push(i);
    }
    while(!q.empty())
    {
        int u=q.top();
        q.pop();
        ans[len++]=u;
        for(i=0;i<v[u].size();i++)
        {
            int t=v[u][i];
            num[t]--;
            if(num[t]==0)
                q.push(t);
        }
    }
    cout<<ans[0];
    for(i=1;i<len;i++)
        cout<<" "<<ans[i];
    cout<<endl;
}
int main()
{
    int i;
    while(cin>>n>>m)
   {
      memset(num,0,sizeof(num));
    for(i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
        num[y]++;
        v[x].push_back(y);
    }
    toposort();
    for(i=0;i<=n;i++)
        v[i].clear();
   }
    return 0;
}

B - so easy

Description

The structure of a graph is given, and its topological ordering sequence is output. Under the same conditions, the vertices with small number are in front.

Input

Several lines of integers, the first line has two numbers, which are vertex number v and arc number a, the next line has a, each line has two numbers, which are the two vertex numbers associated with the arc.  
v<=100, a<=500

Output

A sequence of vertices separated by spaces (in lowercase letters).

Sample Input

6 8
1 2
1 3
1 4
3 2
3 5
4 5
6 4
6 5
Sample Output

v1 v3 v2 v6 v4 v5
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
typedef long long ll;
priority_queue<int,vector<int>,greater<int> > q;
int num[550];
int n,m;
int ans[550];
vector <int> v[550];
void toposort()
{
    int i,len=0;
    for(i=1;i<=n;i++)
    {
        if(num[i]==0)
            q.push(i);
    }
    while(!q.empty())
    {
        int u=q.top();
        q.pop();
        ans[len++]=u;
        for(i=0;i<v[u].size();i++)
        {
            int t=v[u][i];
            num[t]--;
            if(num[t]==0)
                q.push(t);
        }
    }
    cout<<"v"<<ans[0];
    for(i=1;i<len;i++)
        cout<<" v"<<ans[i];
    cout<<endl;
}
int main()
{
    int i;
    cin>>n>>m;
    memset(num,0,sizeof(num));
    for(i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
        num[y]++;
        v[x].push_back(y);
    }
    toposort();
    for(i=0;i<=n;i++)
        v[i].clear();
    return 0;
}