Escape HDU 4857 (FIG built reverse topological sorting +)

escape

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11873    Accepted Submission(s): 3278

Problem Description
Bad happen to you, and now everyone is busy with their lives. But the channel is very narrow escape, we can only line up.

There are n individual numeral from 1 to n. While some strange constraints, each of the form: b a necessary before.
At the same time, social inequality, the poor, some rich, some of these people. The richest 1, No. 2 second rich, and so on. The rich to bribe the person in charge, so they have some benefits.

Leaders can now arrange the order we line up due to receive the benefits, so he let the No. 1 ranking as much as possible, if at this time there are a variety of situations, you let No. 2 as far forward, if there are a variety of situations, let No.3 far forward, and so on.

Then you have arranged for them to order. We guarantee solvability.
 

Input

A first line integer T (1 <= T <= 5), indicates the number of test data.
Then for each of the test data, a first line of two integers n (1 <= n <= 30000) and m (1 <= m <= 100000), respectively, and the number represents the number of constraints.

Then m lines of two integers a and b, represent the number b must be a number before a constraint. a and b must be different.
 

Output

, Separated for each test data output line sequentially with queuing space.
 

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 5
 
Topic analysis
The problem initially thought directly on a priority queue topological sorting on the line, but WA, read explanations of others came to understand different this title and dictionary order, requirement is to meet the premise of m relations, so that No. 1 far forward, then this allows No. 2 .... and so far forward as possible on the basis of topological sorting if direct, priority queue with a minimum value, then the pop-up, No. 1 can not be guaranteed as far forward, for example, 6> 3 -> 1 and 5-> 4-> 2, the output of this method is 542,631, it is evident 631 532 is correct
So our strategy should be to find the point 1, should be put ahead of him are output, then find 2 points, and should be ranked ahead of him are output, and so on, that is sorted by the tail, so, to use a reverse map building
In order to ensure that the front point 1 2, the priority queue may be used to eject a large point
Finally reverse output
#include<bits/stdc++.h>

using namespace std;
priority_queue<int>q;
vector<int>mp[30005];
int n,m,t,i,now,a,b,into[30005],anss[30005],cnt;
int main()
{
    ios::sync_with_stdio(false);  //不写会TLE
    cin>>t;
    while(t--)
    {
        memset(into,0,sizeof(into));
        memset(mp,0,sizeof(mp));
        memset(anss,0,sizeof(anss));
        cnt=0;
        cin>>n>>m;
        while(m--)
        {
            cin>>a>>b;
            mp[b].push_back(a);
            into[a]++;
        }
        for(i=1;i<=n;i++)
        {
            if(into[i]==0)
            {
                q.push(i);
            }
        }
        int flag=1;
        while(!q.empty())
        {
            now=q.top();
            anss[cnt++]=now;
            q.pop();
            for(i=0;i<mp[now].size();i++)
            {
                into[mp[now][i]]--;
                if(into[mp[now][i]]==0)
                {
                    q.push(mp[now][i]);
                }
            }
        }
        for(i=cnt-1;i>=1;i--)
        {
            cout<<anss[i]<<' ';
        }
        cout<<anss[0]<<endl;
    }
}

 

Guess you like

Origin www.cnblogs.com/dyhaohaoxuexi/p/12544390.html