Hdoj 4857 Escape (Reverse topology sort + priority queue)

Source: Internet
Author: User

Escape Click to open link
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2161 Accepted Submission (s): 608


Problem description Bad things happen, and now everyone is busy running for their lives. But the channel of escape is very narrow, we can only line up.

There are now n individuals, numbered from 1 to N. At the same time there are some strange constraints, each of which is shaped like: a must precede B.
At the same time, society is unequal, and some of these people are rich and poor. Number 1th is richest, number 2nd is second richest, and so on. The rich bribe the responsible, so they have some good.

The responsible person can now arrange the order of everyone in line, due to the benefits, so he wants to make 1th as far as possible, if there are a variety of circumstances, let 2nd number as far as possible, if there are many situations, let 3rd as far as possible, and so on.

Then you'll have to arrange the order of everyone. We guarantee there must be a solution.
Input first line an integer t (1 <= T <= 5), indicating 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), respectively, representing the number of people and constraints.

Then m lines, two integers a and b per line, indicate that a constraint a must precede the B number. A and B are necessarily different.
Output the order in which each test data is queued, separated by a space.
Sample Input
15 103 51 42 51 23 41 42 31 53 51 2

Sample Output
1 2 3) 4 5

The problem is to sort the topology, but not the minimum output according to the dictionary order, but to make the smaller number first.

The wrong idea at first: to determine a priority for each point (the smallest point the point can reach), and then use the topology sort + precedence to the column forward processing, forward output. This is wrong, as in the following example:

1

5 4

5 2

4 3

2 1

3 1

Correct solution: is to reverse the construction of the edge, point large priority, with topological sorting + priority queue, reverse output sequence can be.

The topology sequence can be determined based on each pair of restrictions, but there may be multiple topological sequences at this time (there are no points in the order of the relationships). The sequence is determined by the smaller points that are required to precede them.

(1) If point A and point B have a direct and simple topological relationship, then the order of A and B can be determined by topological ordering.

(2) If point A and point B do not have a direct and simple topological relationship, then the order of A and B is determined by the points that can be reached by a and B.

Such as:

1

3 2

3 1

3 1

Should output a result of 3 1 2

Point 3 and point 2 do not have a direct topological relationship, but 3 reaches the minimum point of 1, and 2 reaches the minimum point of 2.

Synthesis (1) and (2) are subject to reverse processing.

PS: The path output of Euler circuit is also reverse output.

Note that vector STL is convenient for use here

<span style= "FONT-SIZE:18PX;" > #include <cstdio> #include <queue> #include <vector> #include <cstring> #include < Cstdlib> #include <algorithm>using namespace std; #define MAX 100005priority_queue<int,vector<int> Less<int > >q;//here should be from large to small vector<int> map[max];int indegree[max];int que[max];//store void topsort (int n) {    int I;for (i=1;i<=n;i++) if (indegree[i]==0) Q.push (i);  int j=0;  memset (que,0,sizeof (que));  while (!q.empty ()) {int v=q.top ();  Q.pop ();  Que[j++]=v;   for (int k=0;k<map[v].size (); k++) {indegree[map[v][k]]--;  if (indegree[map[v][k]]==0) Q.push (Map[v][k]);}   } for (i=j-1;i>0;i--) printf ("%d", que[i]); printf ("%d\n", Que[0]);} int main () {int t;scanf ("%d", &t), while (t--) {int n,m;scanf ("%d%d", &n,&m); Memset (Indegree,0,sizeof (       Indegree)); for (int. i=1;i<=n;i++) Map[i].clear ();</span> while (m--) {int A, a, b;        scanf ("%d%d", &a,&b); Map[b].push_back (a);//Place A Inb behind, establish the opposite side of the relationship </span> indegree[a]++; } topsort (n);} return 0;} </span>
At first I thought I'd do it. WA
<span style= "FONT-SIZE:18PX;" > #include <cstdio> #include <cstring> #include <queue> #include <cstdlib> #include < algorithm>using namespace std; #define MAX 100005int head[max];int indegree[max];int que[max];struct node{int to;int N ext;} Edge[max];p riority_queue<int,vector<int>,greater<int> >q;//queue<int>q;  void Topsort (int n) {int i;for (i=1;i<=n;i++) if (indegree[i]==0) Q.push (i);  int j=0;  memset (que,0,sizeof (que));  while (!q.empty ()) {int v=q.top ();  int V=q.front ();    Q.pop ();   Que[j++]=v;   for (int k=head[v];k!=-1;k=edge[k].next) {indegree[edge[k].to]--; if (indegree[edge[k].to]==0) Q.push (edge[k].to);    }} for (i=j-1;i>0;i--) printf ("%d", que[i]); printf ("%d\n", Que[0]);} int main () {int t;scanf ("%d", &t), while (t--) {int n,m,i;scanf ("%d%d", &n,&m); Memset (Head,-1,sizeof (head)) , Memset (indegree,0,sizeof (Indegree)), memset (Edge), for (edge,0,sizeof) {int i=1;i<=m;i++ ("a,b;scanf", &AMP;A,&AMP;B);//edge[i].to=a;//edge[i].next=head[b];//head[b]=i;//indegree[a]++;edge[i].to=b;edge[i].next=head[a];head[a]=i ; indegree[b]++;} Topsort (n);}} </span>



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj 4857 Escape (Reverse topology sort + priority queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.