hdu 1285 to determine the competition

  Topological sorting of a Directed Acyclic Graph (DAG) G is to arrange all vertices in G into a linear sequence, so that any pair of vertices u and v in the graph, if the edge (u, v) ∈ E(G), then u appears before v in the linear sequence. Usually, such a linear sequence is called a sequence satisfying a topological order, or a topological sequence for short. Simply put, a total order on a set is obtained from a partial order on a set, and this operation is called topological sorting.

Problem Description
There are N teams (1<=N<=500), numbered 1, 2, 3, in order. . . . , N to play the game. After the game, the referee committee will rank all participating teams from front to back, but now the referee committee cannot directly obtain the game results of each team, only know the result of each game, that is, P1 wins P2, using P1, P2 means that P1 is ahead of P2 when ranking. Now please program to determine the ranking.
 
Input
There are several groups of input, and the first row in each group is two numbers N (1<=N<=500), M; where N represents the number of teams, and M represents the input data followed by M rows. In the next M lines of data, each line also has two integers P1, and P2 means that the P1 team wins the P2 team.
 
Output
Give a ranking that meets the requirements. There are spaces between the team numbers when outputting, and there is no space after the last.
Other instructions: The ranking that meets the conditions may not be unique. In this case, the team with the smaller number is required to be first in the output; the input data is guaranteed to be correct, that is, the input data ensures that there must be a ranking that meets the requirements.
 
Sample Input
4 3
1 2
2 3
4 3
Sample Output
1 2 4 3
 topological sort
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
vector<int>v[506];
int x,y,degree[506];
int vis[506],n,m;
void toposort()
{
    priority_queue<int,vector<int>,greater<int> >q;
    for(int i=1;i<=n;i++)
    {
        if(!degree[i])
        {
            q.push(i);
            show [i] = 1 ;
        }
    }
    bool flag=0;
    while(!q.empty())
    {
        int u=q.top();
        q.pop();
        if(!flag){printf("%d",u);flag^=1;}
        else printf(" %d",u);
        for(int i=0;i<v[u].size();i++)
        {
            degree[v[u][i]]--;
            if(!degree[v[u][i]]) q.push(v[u][i]);
        }
    }
    printf("\n");
}
intmain ()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<=n;i++)
            v[i].clear();
        memset(degree,0,sizeof(degree));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            v[x].push_back(y);
            degree[y] ++ ;
        }
        toposort();
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325356546&siteId=291194637