NYOJ 496 tour Topology Sorting

Source: Internet
Author: User

Tour
Time Limit: 1000 MS | memory limit: 65535 KB
Difficulty: 3
Description
The World Boxing Association (WBA) is the world's oldest boxing organization. It has given birth to many world champions, especially the heavyweight, and has created almost all the great boxing kings familiar to everyone. Alibaba, Fraser, And Forman are known as the "1970s heavyweight boxing Titans" and are well-deserved champion. Their gold belts are all engraved with WBA. To celebrate the 50 th anniversary of the establishment of the World Boxing Association, WBA Chairman mendos invited N boxers to carry out M-game tours, each of which can be divided and won. After the competition, the Organizing Committee will sort N contestants, for each boxer, the ranking must be satisfied only when all the opponents defeated by the Boxer are placed behind them.
We now provide the relationship between winning and losing M games. Please help the Organizing Committee decide whether such a ranking can be uniquely determined to satisfy all the boxers. If it can be uniquely determined, the final ranking will be output.
Input
The first line shows the number of test data groups T (0 <T <30). For each group of test data, N (1 <= N <= 26) is given in sequence ), M (0 <= M <= 1000) indicates the number of boxers and the number of competitions respectively. The name of the boxers is the first N uppercase letters starting from, next, the M line shows the results of each game. Each line consists of two uppercase letters with a space between them.
For example, "a B" indicates that A defeated B in A game.
Output
For each group of tests, if there is No unique ranking sequence, the "No Answer" is output in a single row. If there is a ranking sequence, the name of the Boxer is output from high to low.
Sample Input
3
4
A B
A C
B C
C D
4
A B
A C
B D
C D
3 3
A B
B
A C
Sample output
ABCD
No Answer
No Answer solution: first find a point with an inbound degree of 0, and start from this point to find the next point with an inbound degree of 0 until all the points are found; in the process of searching, if there are multiple or zero vertices with an inbound degree of 0 (except the last vertex) at the same time, there is no topological sorting.

 

# Include <stdio. h> # include <string. h> int map [30] [30], B [50], n, m, in [50]; char c [100]; void toposort () {int incnt = 0, flag = 1, front = 1, rear = 1, I, j, k; for (I = 1; I <= n; I ++) if (! In [I]) {incnt ++; B [rear ++] = I;/* find the point where the first Inbound degree is 0 */} if (incnt! = 1) flag = 0; j = 0;/* number of points saved in the record */while (front <rear) {incnt = 0; k = B [front ++]; c [j ++] = k + 'a'-1;/* Save the sorted vertex */for (I = 1; I <= n; I ++)/* Note that there are n points. Do not write them as m */{if (map [k] [I]) {in [I] --; if (! In [I]) {incnt ++; B [rear ++] = I;/* if the entry level is 0, add to team end */} if (incnt> 1) /* if there are more than one vertex with an inbound value of 0, there is no topological sorting. Note that this cannot be written as incnt! = 1, because from the last vertex, the entry degree of 0 must be 0, and the flag will change to 0 */{flag = 0; break ;}} if (j <n |! Flag) printf ("No Answer \ n"); else {for (I = 0; I <j; I ++) printf ("% c ", c [I]); printf ("\ n") ;}} int main () {int t, I; scanf ("% d", & t ); while (t --) {char A, B; int a, B; memset (map, 0, sizeof (map); memset (in, 0, sizeof (in )); memset (c, 0, sizeof (c); scanf ("% d", & n, & m); getchar (); for (I = 1; I <= m; I ++) {scanf ("% c", & A, & B); a = A-'A' + 1; B = B-'A' + 1; map [A] [B] = 1; in [B] ++; getchar () ;}toposort ();} return 0 ;}

 

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.