HDU 1258 Links: Click here
HDU 3342 Links: Click here
Test instructions
Determine the position of the match
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 14142 Accepted Submission (s): 5667
Problem description has N teams (1<=n<=500), numbered three-in-one, .... , N to play, after the game, the Referee Committee will be all the teams from the arrival of the ranking, but now the referee committee can not directly get each team's performance, only know the results of each game, that is, P1 win P2, with P1,p2 said, ranked P1 before P2. Now ask you to compile the program to determine the rankings.
Input inputs have several groups, the first behavior in each group is two n (1<=n<=500), M, where n represents the number of troops, and m represents the input data for the M row. In the next M-row data, there are also two integers per line p1,p2 means that the P1 team won the P2 team.
Output gives a ranking that meets the requirements. There is a space between the queue numbers at the time of the output, and no space after the last.
Other Notes: Qualifying rankings may not be unique, at which point the output is required to be numbered in front of the team; the input data is guaranteed to be correct, i.e. the input data ensures that there must be a qualifying ranking.
Sample Input
4 31 22) 34 3
Sample Output
1 2 4 3
"Problem-solving ideas":
Basic topology Sequencing application:
/* Topology sorting method is as follows:
(1) Select a vertex with no precursor (that is, 0) from the graph and output it.
(2) Delete the vertex from the net and delete all the forward edges emitted from the vertex.
(3) Repeat these two steps until there are no more vertices in the remaining nets that do not have a previous trend.
*/
Code:
HDU 1285 determine tournament rankings # include <stdio.h> #include <string.h> #include <iostream> #include <algorithm >using namespace Std;const int maxn =550;int g[maxn][maxn];int indegree[maxn];int v,m;bool g[maxn];void AOV_topsort () {int flag = 1; for (int i=1; i<v+1; i++) {for (int j=1; j<v+1; j + +) {if (indegree[j] = = 0) {if (i!=v) printf ("%d", j); else printf ("%d\n", j); indegree[j]--; for (int k=1; k<v+1; k++) {if (g[j][k] = = 1) { indegree[k]--; }} break; }}}}int Main () {while (cin>>v>>m) {memset (g, 0, sizeof (g)); memset (G,false,sizeof (g)); memset (Indegree, 0,sizeof (Indegree)); int A, B; for (int i=0; i<m; i++) {SCANF ("%d%d", &a,&b); if (g[a][b]==0) {g[a][b]=1; indegree[b]++; }} aov_topsort (); } return 0;}
Legal or not
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 5041 Accepted Submission (s): 2311
Problem Descriptionacm-diy is a large QQ group where many excellent acmers get together. It's so harmonious this just like a big family. Every Day,many "holy cows" like HH, hh, AC, ZT, LCC, BF, Qinz and so on chats on-line to exchange their ideas. When someone had questions, many warm-hearted cows like Lost would come to help. Then the one being helped would call Lost "master", and Lost would have a nice "Prentice". By and by, there is many pairs of "Master and Prentice". But then problem Occurs:there was too many masters and too many prentices, how can we know whether it's legal or not?
We all know a master can has many prentices and a Prentice may has a lot of masters too, it ' s legal. Nevertheless,some cows is not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH was 3xian ' s master and, at the same time, 3xian is HH's Master,which is quite illegal! To avoid this,please help us to judge whether their relationship are legal or not.
Please note that the ' Master and Prentice ' relation is transitive. It means if A is B's master ans B is C's master, then A is C ' s master. Inputthe input consists of several test cases. For each case, the first line contains-integers, N (members to is tested) and M (relationships to be tested) (2 <= N , M <= 100). Then M. lines follow, each contains a pair of (x, y) which means x are Y ' s master and y is X ' s Prentice. The input is terminated by N = 0.
To make IT simple, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
Outputfor each test case, print on one line the judgement of the messy relationship.
If It is legal, output "YES", otherwise "NO". Sample Input
3 20 11 22 20 11 00 0
Sample Output
YESNO
"Problem-solving ideas"
Follow the same idea, pay attention to the weight
Code:
/* The topology is sorted as follows: (1) Select a vertex without a precursor (that is, 0) from the graph and output it. (2) Delete the vertex from the net and delete all the forward edges emitted from the vertex. (3) Repeat these two steps until there are no more vertices in the remaining nets. *///hdu 3342 Legal or not#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>using namespace std;const int maxn =550;int g[maxn][maxn];int INDEGREE[MAXN] ; int V,m;bool g[maxn];void aov_topsort () {int flag = 1; for (int i=1; i<v+1; i++) {for (int j=0; j<v; j + +) {if (indegree[j] = = 0) {if (g[j]==0) {g[j]=1; indegree[j]--; for (int k=0; k<v; k++) {if (g[j][k] = = 1) { indegree[k]--; }}}}} for (int i=0; i<v; i++) if (INDEGREE[I]&G T 0) {flag = 0; Break } if (flag==0) puts ("NO"); Else puts ("YES");} int main () {while (cin>>v>>m&&v&&m) {memset (g, 0, sizeof (g)); memset (G,false,sizeof (g)); memset (Indegree, 0,sizeof (Indegree)); int A, B; for (int i=0; i<m; i++) {cin>>a>>b; if (g[a][b]==0) {g[a][b]=1; indegree[b]++; }} int flag = 0; for (int i=0; i<v; i++) {if (indegree[i]==0) {flag = 1; Break }} if (flag==0) puts ("NO"); else Aov_topsort (); } return 0;}
HDU 1258 determining the tournament position &&hdu 3342 Legal or not "pro-table + topology ordering"