Hpuoj 1706: niub [Forward topology] [diagram]

Source: Internet
Author: User
1706: niub time limit: 1 sec memory limit: 128 MB
Submission: 22 solution: 6
[Submit] [Status] [discussion version] Description

A group of Japanese terrorists carrying AK47, rockets, tanks and planes forcibly ranked a bank in Japan. But we all know that the Japanese are fraudulent and brutal, but the terrorists are even more cruel. They have been burning and looting the Japanese and finally successfully snatched all the money from the bank. But because the Japanese are greedy, these terrorists always want to grab all the money exclusively. Of course, this has aroused dissatisfaction among others. For this reason, they have to kill each other in order to monopolize all the money. This group of terrorists is known to have n (n <= 200) and M (M <= 1000) two people in each line of the relationship indicate that the previous person can kill the latter. Now, please judge whether there is a unique order. If there is an output, this ranking will be output from the best B to the weakest B. if the name does not exist or there is a conflict with the data (in this case, Japanese a kills Japanese B and Japanese B is resurrected to kill Japanese A (Japanese are so cruel) and so on) Output "Baga ".

Input

Input the first two integers n and M, N represents N (2 = <n <= 200) Japanese terrorists, M group relationship (1 = <m <1000) the next two strings in each line represent the Japanese name. The first Japanese can kill the Second Japanese. Input data to ensure that N people's names have exceeded and their names cannot exceed 20 characters.

Output output: if there is a group of unique rankings from ox B to weak B, the Japanese names in this ranking are output in sequence from ox B to weak B. If there is no output or a conflict exists, "Baga" is output "; sample Input
4 3anbeijinsan gangcunningcigangcunningci songjingshigensongjingshigen dongtiaoyingji4 3anbeijinsan xiaoquanyilanggancunningc shanben56anbeijinsan shanben562 2anbeijinsan gangcunningcigangcunningci anbeijinsan
Sample output
Anbeijinsan gangcunningci songjingshigen dongtiaoyingjibagabaga
This question is not difficult to emphasize on creating a diagram:
Note: 1. the sequence to be output must be unique, that is, the rest of the people except the best person must be able to be killed by another person, for example, if there are four ABCD members, a kills B, B kills C, and C kills D)
So how can we determine if it is unique? It is very simple, that is, each queue must have only one element
2. If the Ring cannot be formed, this is easy. You only need to judge whether the number of people involved in the sorting is equal to the total number after the sorting is completed.
# Include <stdio. h> # include <string. h ># include <queue> using namespace STD; int n, m; char a [50], B [50]; char STR [210] [50]; int map [210] [210], vis [210]; int s [210] [210]; int d [210]; void Qing () {memset (MAP, 0, sizeof (MAP); memset (VIS, 0, sizeof (VIS); memset (D, 0, sizeof (d); memset (S, 0, sizeof (s); memset (A, '\ 0', sizeof (a); memset (B,' \ 0', sizeof (B); memset (STR, '\ 0', sizeof (STR);} void getmap () {int Q, P, I, j, sum = 1; Qing (); for (I = 1; I <= m; I ++) {q = P =-1; scanf ("% S % s", a, B); For (j = 1; j <sum; j ++) {If (strcmp (STR [J], a) = 0) {q = J; Map [I] [1] = J ;} if (strcmp (STR [J], B) = 0) {P = J; Map [I] [2] = J;} If (Q! =-1 & P! =-1) break;} If (q =-1) {q = sum ++; Map [I] [1] = Q; strcpy (STR [Q], a) ;}if (P =-1) {P = sum ++; Map [I] [2] = P; strcpy (STR [p], b) ;}}for (I = 1; I <= N; I ++) {If (! S [map [I] [1] [map [I] [2]) {s [map [I] [1] [map [I] [2] = 1; vis [map [I] [2] ++ ;}}} void tuopu () {int I, j, OK = 0; int ant = 0; queue <int> q; while (! Q. empty () Q. pop (); for (I = 1; I <= N; I ++) if (vis [I] = 0) Q. push (I); int U, ANS = 0; while (! Q. empty () {If (Q. size ()> 1) // determine if it is unique {OK = 1; break;} u = Q. front (); q. pop (); D [ans ++] = u; for (I = 1; I <= N; I ++) {If (s [u] [I]) {vis [I] --; If (! Vis [I]) Q. Push (I) ;}} if (ANS! = N) // determine whether the ring is OK = 1; if (OK) printf ("Baga \ n"); else {for (I = 0; I <ans-1; I ++) printf ("% s", STR [d [I]); printf ("% s \ n", STR [d [ans-1]);} int main () {int I, j; while (scanf ("% d", & N, & M )! = EOF) {getmap (); tuopu ();} return 0 ;}

  

  

Hpuoj 1706: niub [Forward topology] [diagram]

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.