Hangzhou Electric 4857--Escape (topological sort)

Source: Internet
Author: User

Escape

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2257 Accepted Submission (s): 637


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 Input15 103 51 42 51 23 41 42 31 53 51 2

Sample OUTPUT1 2 3 4 5

Authorclj

Sourcebestcoder Round #1

Recommendwe has carefully selected several similar problems for you:5390 5389 5388 5387 5386 RE: Reverse topology + priority queue + reverse output.
1#include <queue>2#include <cstdio>3#include <cstring>4#include <iostream>5 using namespacestd;6 inthead[100010], indegree[100010], result[100010];7 intN, M;8 9 structrode{Ten     intto, next; One} map[100010]; A voidTsort (intN) - { -priority_queue<int> q;intK =1, U; the      for(inti =1; I <= N; i++) -         if(!Indegree[i]) - Q.push (i); -      while(!q.empty ()) +     { -result[k++] = U =q.top (); + Q.pop (); A          for(inti = Head[u]; I! =-1; i =map[i].next) at         { -             if(!--indegree[map[i].to]) - Q.push (map[i].to); -         } -     }     -      for(inti = n; I >=1; i--) in     { -         if(I! =1) toprintf"%d", Result[i]); +         Else -printf"%d\n", Result[i]); the          *     } $ }Panax Notoginseng intMain () - { the     intT; +scanf"%d", &t); A      while(t--) the     { +memset (Indegree,0,sizeof(Indegree)); -Memset (Head,-1,sizeof(head)); $scanf"%d%d", &n, &m); $          for(inti =0; I < m; i++) -         { -             intA, B; thescanf"%d%d", &a, &b); -Map[i].to =A;WuyiMap[i].next =Head[b]; theHEAD[B] =i; -++Indegree[a]; Wu         } - tsort (n); About     }  $     return 0; -}

Hangzhou Electric 4857--Escape (topological sort)

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.