Nyoj 496 [tournament-topology sort]

Source: Internet
Author: User

Link: click here

Test instructions

Tournament time limit: +Ms | Memory Limit:65535KB Difficulty:3
description
the
World Boxing Association (WBA) is the oldest boxing organization in the world, and it breeds a multitude of champions, especially heavyweight, that almost all of us are familiar with the great champion. Ali, Fraser, foreman known as the "heavyweight boxing of the 70 's", is a well-deserved champion, their gold belts are engraved with the WBA words. To celebrate the 50 anniversary of the founding of the World Boxing Association, WBA President Mendoza invited n boxer to carry on the M-field tour, each game can be divided, the organizing committee to order the N players after the game, for each fighter, must meet the fighter's opponents all ranked after the rank to be satisfied with the ranking.
now give the winning and losing relationship of M field, please help the organizing committee decide whether to be able to determine the ranking, so that all the boxers are satisfied, if the only certainty will be output final ranking.
input
The first row gives the group number of test data T (0<T<30), for each set of test data, first give N (1<=n<=26), M (0& lt;=m<=1000) The number of fighters and the number of matches, the boxer's name is the first N capital letters from A, followed by M-line gives the result of each match, each line consists of two uppercase letters, a space between the two. A
such as "A B" indicates a victory over B in a match.
output
for each set of tests, if there is no unique ranking sequence, the single-line output "No Answer", if present, the name of the fighter by ranking from high to low.
Sample Input
34 4 a BA CB CC D4 4 a BA CB DC D3 3 a BB AA C 
Sample Output
abcdno answerno Answer 
Source
"Problem-solving ideas":

DFS implementation topology sequencing

Code:

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>using namespace std;const int MAXN =30;int t,n,m,v,i,j;int g[maxn],topo[maxn],g[maxn][maxn];bool DFS (int u)//df                             S implements topological ordering {G[u] =-1;    Start accessing the vertex for (int v=0; v<v; v++) if (G[u][v]) {if (g[v]<0) return false;  The C[V]=-1 representative is accessing the fixed point (that is, recursive calls to DFS (U) are in the frame stack, not yet returned) else if (G[v]==0&&dfs (v) ==0) return false;//(c[v]==0 &&                               DFS (v) ==false that is, the current vertex is not a vertex,} g[u]=1;    Visit End Topo[--t]=u; return true;}    BOOL Aov_toposort () {t=v;    memset (G,0,sizeof (g)); for (int u = 0; u < V; u++) if (!g[u]) if (!    DFS (U)) return false; return true;}    int main () {//freopen ("1.txt", "R", stdin);    Freopen ("2.txt", "w", stdout);    int nca,i,m;    Char a[2],b[2];    cin>>nca;        while (nca--) {memset (g,0,sizeof (G)); Cin>>v>>m;            for (i = 0; i < m; i++) {cin>>a>>b;        g[(a[0]-' a ') [(b[0]-' a ')] = 1;            } if (Aov_toposort ()) {bool flag= true; for (i=0; i<v-1; i++) if (!                    G[topo[i]][topo[i+1]]) {flag=false;                Break                } if (flag) {for (i=0; i<v; i++) printf ("%c", topo[i]+ ' A ');            Puts ("");        } else puts ("No Answer");    } else puts ("No Answer"); } return 0;}


Nyoj 496 [tournament-topology 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.