HDU4875 (reverse topological sort)

Problem Description
Something bad happened, and now everyone is running for their lives. But the escape channel is very narrow, and everyone can only line up.

There are now n people, numbered from 1 to n. There are also some strange constraints, each of the form: a must come before b.
At the same time, society is unequal, some of these people are poor and some are rich. No. 1 is the richest, No. 2 is the second richest, and so on. Rich people bribe those in charge, so they have some benefits.

The person in charge can now arrange the order of everyone's queuing. Because of the benefits, he wants to put No. 1 in the front as much as possible. If there are still many situations at this time, let No. 2 go ahead as much as possible. If there are still many situations, Let No. 3 move forward as far as possible, and so on.

Then you have to arrange the order of everyone. We guarantee there will be a solution.
 

Input
The first line contains an integer T (1 <= T <= 5), which represents the number of test data.
Then for each test data, the first line has two integers n (1 <= n <= 30000) and m (1 <= m <= 100000), representing the number of people and the number of constraints, respectively.

Then there are m lines, each with two integers a and b, indicating that there is a constraint that the number a must precede the number b. a and b must be different.
 

Output
For each test data, output a line of queued order, separated by spaces.
 

Sample Input
 
  
1 5 10 3 5 1 4 2 5 1 2 3 4 1 4 2 3 1 5 3 5 1 2
 

Sample Output
 
  
1 2 3 4

After learning topological sorting, I found that this question is really cool. In addition to the constraints, there is a rule that the small number means that the rich businessman must go first, so it is cool to build a topological sorting map, such as this set of examples:

3 1

3 1

If it is 2 3 1, we need to build the map in reverse, then use a large root heap for the priority queue, put the larger number in the front, and finally output the result in reverse order.

#include<stdio.h>
#include<queue>
#include<set>
#include<functional>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
struct node
{
    int v;
    int nextt;
}e[100005];
priority_queue<int>q;
int r,k;
int first[30005],in[30005],z[30005];
int n,m;
void add(int u,int v)
{
    e [r] .v = v;
    e[r].nextt=first[u];
    first[u]=r++;
}
void tp()
{
    while(!q.empty())
        q.pop();
    for(int i=1;i<=n;i++)
        if(in[i]==0)
          q.push(i);
    k=0;
    while(!q.empty())
    {
        int u=q.top();
        q.pop();
        in[u]--;
        z[k++]=u;
        printf("%d\n",u);
        for(int i=first[u];i!=-1;i=e[i].nextt)
        {
            int v = e [i] .v;
            and in]--;
            if(in[v]==0)
            q.push(v);
        }
    }
}
intmain()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        int a,b;
        r=0;
        memset(first,-1,sizeof(first));
        memset(in,0,sizeof(in));
        memset(z,0,sizeof(z));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            add(b,a);
            in[a]++;
        }
        tp();
        printf("%d",z[k-1]);
        for(int i=k-2;i>=0;i--)
            printf(" %d",z[i]);
        printf("\n");
    }
}

Guess you like

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