HDU 1285 determining the tournament position (Topology sort template)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1285

The main topic: There are N teams (1<=n<=500), numbered in turn, .... , 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.

Problem-solving ideas: topological sort of bare topic, but there are requirements for side-by-side point ordinal small row in front. began to write a DFS version of the reverse of how to write is wrong, later found that there is no way to do (maybe I am too food) .... Then rewrite a point by each lookup into the 0, and then delete the version of the side, a bit on AC.

First of all, call it the method of reducing the degree, this method can determine whether the graph is a single chain, that is, there is no branch, if a point to find the degree of 0 points there are two there is a branch.

Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5 using namespacestd;6 Const intn=5e2+5;7 8 intn,m;9 intDegree[n];Ten BOOLG[n][n]; Onequeue<int>Q; A  - voidToposort () { -      for(intI=1; i<=n;i++){ the         //looking for a point with a degree of 0 -         intj=1; -          while(degree[j]!=0) J + +; -degree[j]--; + Q.push (j); -         //reduce the degree of the associated point by 1, that is, remove the edges associated with the node +          for(intk=1; k<=n;k++){ A             if(G[j][k]) atdegree[k]--; -         } -     } - } -  - intMain () { in      while(~SCANF ("%d%d",&n,&m)) { -memset (G,false,sizeof(G)); tomemset (Degree,0,sizeof(degree)); +          for(intI=1; i<=m;i++){ -             intb; thescanf"%d%d",&a,&b); *             if(!G[a][b]) { $g[a][b]=true;Panax Notoginsengdegree[b]++; -             }         the         } + Toposort (); A          while(!Q.empty ()) { the             if(q.size () = =1) +printf"%d\n", Q.front ()); -             Else $printf"%d", Q.front ()); $ Q.pop (); -         } -     } the     return 0; -}

Then the DFS version, although can not write this problem, but still as a template to put it, DFS law can be in O (n^2) time to determine whether there is a ring, and Floyd need O (n^3).

Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <stack>5 using namespacestd;6 Const intn=5e2+5;7 8 intn,m;9 intG[n][n],vis[n];//vis[i]=0,-1,1 means no access, access, access, and recursive access to all descendantsTenstack<int>Res; One  A  - BOOLDfsintu) { -vis[u]=-1; the      for(intI=1; i<=n;i++){ -         if(G[u][i]) { -             if(vis[i]<0)return false; -             Else if(!vis[i]&&!DFS (i)) +                 return false; -         } +     } Avis[u]=1; at res.push (u); -     return true; - } -  - BOOLToposort () { -memset (Vis,0,sizeof(Vis)); in      for(intI=1; i<=n;i++){ -         if(!Vis[i]) { to             if(!dfs (i))return false; +         } -     } the     return true; * } $ Panax Notoginseng intMain () { -      while(~SCANF ("%d%d",&n,&m)) { thememset (G,0,sizeof(G)); +          for(intI=1; i<=m;i++){ A             intb; thescanf"%d%d",&a,&b); +g[a][b]=1; -         } $ Toposort (); $          while(!Res.empty ()) { -             if(res.size () = =1) -printf"%d\n", Res.top ()); the             Else -printf"%d", Res.top ());Wuyi Res.pop (); the         } -     } Wu     return 0; -}

HDU 1285 determining the tournament position (Topology sort template)

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.