hdu4857 (topological sorting, FIG reverse construction)

escape

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


 

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

Author

CLJ

Source

BestCoder Round #1

Recommend

We have carefully selected several similar problems for you:  6460 6459 6458 6457 6456 

 Mind: if there will be the normal priority queue topological sort problem, such as an edge 5-> 2 and an isolated point 3, less than 5 because of topological sorting after 3 becomes 2 in front of the 3,5,2,3. If we are to build a reverse side view of a 2-> 5 and a solitary point 3, we maintain a large heap get is top 3,2,5 flipped it is the answer.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>e[30050];
int in[30050];
struct  cmp1{
	bool operator()(int &a,int &b)
	{
		return a>b;
	}
};
priority_queue<int> q;
int main()
{
	int n;
	int t,m;
	cin>>t;
	while(t--)
	{
        memset(in,0,sizeof(in));
        scanf("%d%d",&n,&m);
		for(int i = 1; i <= n; i++)
			e[i].clear();
		for(int i=0; i<m; i++)
		{
			int x,y;
            scanf("%d%d",&x,&y);
			in[x]++;
			e[y].push_back(x);
		}
		for(int i=1; i<=n; i++)
		{
			if(in[i]==0)
			{
				q.push(i);
			}
		}
		vector<int> ans;
		while(!q.empty())
		{
			int a=q.top();
			q.pop();
			ans.push_back(a);
			for(int i=0; i<e[a].size(); i++)
			{
				int b=e[a][i];
				in[b]--;
				if(in[b]==0)
					q.push(b);
			}
		}
        for(int i = ans.size()-1;i > -1;i--) {
            if(i != 0) printf("%d ",ans[i]);
            else printf("%d\n",ans[i]);
        }
	}
	return 0;
}

 

Published 155 original articles · won praise 32 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_37632935/article/details/87900434