hdu1285+hdu2467 (topological sort)

Source: Internet
Author: User

determine the position of the matchTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 10604 Accepted Submission (s): 4150


problem DescriptionThere are N Match teams (1<=n<=500). The numbering is 1 in turn. 2, 3 ...

。。

, N to race, after the game is over. The Referee Committee will rank all the participating teams from the point of travel, but now the referee Committee cannot get the results of each team directly. Just know the results of each game, that is, P1 win P2, with P1. P2 said P1 before P2 in the rankings. Now ask you to compile the program to determine the rankings.

Input the input has several groups, the first behavior in each group is two number n (1<=n<=500), M, where n represents the number of troops, and M is followed by the input data for M rows. In the next M-row data, each row also has two integer P1. P2 said that the P1 team won the P2 team.



Output give 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 this time 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
 
AuthorSmallbeer (CML) 
SourceHangzhou Electric ACM Training Team Training Tournament (VII) 
recommendLCY | We have carefully selected several similar problems for you:2647 3342

pid=1811 ">1811 1548

pid=1532 ">1532

Exercises
The topic is a relatively bare topological sorting problem. Pay attention to the heavy edges. There is also the point that qualifying rankings may not be unique, at this time the requirement to output the number of small teams in. The next is the limit of the output format!



#include <iostream> #include <cstring>using namespace Std;const int Maxn=500+10;int Indev[maxn];bool Visited[maxn];bool g[maxn][maxn];int n,m;void init () {memset (g,0,sizeof (g)); Memset (visited,0,sizeof (visited)); memset (indev,0,sizeof (InDev));} void input () {int a,b;for (int i=0;i<m;i++) {scanf ("%d%d", &a,&b), if (!g[a][b]) {g[a][b]=true;++indev[b];}}} void Topsort ()  {      int i,tag=0;      while (Tag<n)      {for  (i=1;i<=n;i++) if (!visited[i]&&0==indev[i]) break;if (tag) printf (""); + + tag;printf ("%d", I), visited[i]=true;for (int j=1;j<=n;j++) if (G[i][j])--indev[j];    }  printf ("\ n");}  int main () {while (scanf ("%d%d", &n,&m)!=eof) {init (); input (); Topsort ();} return 0;}


Reward Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3580 Accepted Submission (s): 1085


problem DescriptionDandelion ' s uncle is a boss of a factory. As the Spring Festival is coming, he wants to distribute rewards to his workers. Now he had a trouble about what to distribute the rewards.
The workers would compare their rewards, and some one may has demands of the distributing of rewards, just like a ' s reward Should more than B ' S.dandelion's unclue wants to fulfill all the demands, of course, he wants to use the least money. Every work ' s reward'll be at least 888, because it's a lucky number. 
InputOne line with a integers n and m, stands for the number of works and the number of demands. (n<=10000,m<=20000)
Then M. Lines, each line contains the integers a and B, stands for a ' s reward should is more than B ' s. 
Outputfor every case, print the least money dandelion ' s uncle needs to distribute. If it ' s impossible to fulfill all the works ' demands, print-1. 
Sample Input
2 11 22 21 22 1
 
Sample Output
1777-1
 
AuthorDandelion 
Sourceit was jinghong.Exercises
In the same way is easier to see the topological sorting problem, different building topology diagram is should reverse see diagram. This will ensure that the former is larger than the latter.


#include <iostream> #include <vector> #include <cstring> #include <queue>using namespace std; const int Maxn=10000+10;vector<int>g[maxn];int indev[maxn];int money[maxn];int n,m;void init () {for (int i=0;i <maxn;i++) {g[i].clear (); money[i]=888;} memset (indev,0,sizeof (InDev));} void input () {int a,b;for (int i=0;i<m;i++) {scanf ("%d%d", &a,&b); G[b].push_back (a); ++indev[a];}} void Topsort () {queue<int>q;int tag=0,ans=0;for (int i=1;i<=n;i++) {if (indev[i]==0) Q.push (i);} while (!q.empty ()) {++tag;int Now=q.front (), Ans+=money[now];q.pop (); for (int i=0;i<g[now].size (); i++) {int next=g[ Now][i];if (--indev[next]==0) {Q.push (next); money[next]=money[now]+1;}}} if (tag!=n) ans=-1;printf ("%d\n", ans);} int main () {while (scanf ("%d%d", &n,&m)!=eof) {init (); input (); Topsort ();} return 0;}


246

hdu1285+hdu2467 (topological 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.