Topic Links:
Gym Class
Time limit:6000/1000 MS (java/others)
Memory limit:65536/65536 K (java/others)
problem DescriptionAs we all know, the degree bears like all kinds of sports activities.
Today, it finally became the dream of the physical education teacher. For the first time in class, it found an interesting thing. Before class, all the classmates have to be lined up, assuming that at first everyone has a unique ID, from 1 to N, after the queue, each student party finds the minimum ID of all the students in front of him, including himself, as a score for judging the class. Trouble is, some students do not want to some (some) classmates in front of him (her), in the case of satisfying this premise, the new PE teacher-degree bear, hope that the final line results can make all students evaluation scores and maximum.
InputFirst line an integerTSaidT(1≤t≤) Group data.
For each set of data, enter two integers in the first rowNAndm(1≤N≤100000,0≤m≤100000) , representing the total number of people and the preferences of some students respectively.
NextMRows, two integers per lineAAndb(1≤A,b≤N) , which indicates that the ID isThe classmate of a does not want the student ID B to rank before him (her). You can assume that the title guarantee has at least one arrangement that meets all requirements.
OutputFor each set of data, the maximum score is output.
Sample Input 31 02 11 23 13 1
Sample Output 126
Test Instructions:
Ideas:related to the succession problem is the topological sequence, and this topological sequence more than one, and the requirements of the final and maximum, greedy know to make the ID value of large as far as possible in front, so topology sorting with priority queue maintenance;
AC Code:
//#include <bits/stdc++.h>#include<iostream>#include<queue>#include<cmath>#include<map>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;#defineRiep (n) for (int i=1;i<=n;i++)#defineRIOP (n) for (int i=0;i<n;i++)#defineRJEP (n) for (int j=1;j<=n;j++)#defineRJOP (n) for (int j=0;j<n;j++)#defineMST (SS,B) memset (ss,b,sizeof (ss));typedefLong LongLL;//const LL mod=1e9+7;Const DoublePi=acos (-1.0);Const intinf=0x3f3f3f3f;Const intn=1e5+ -;intN,m,ind[n],vis[n],b[n];vector<int>Ve[n];p riority_queue<int>Qu;intMain () {intT; scanf ("%d",&t); while(t--) {scanf ("%d%d",&n,&m); Riep (n) ve[i].clear (), Vis[i]=0; intx, y; Riep (m) {scanf ("%d%d",&x,&y); Ve[x].push_back (y); Ind[y]++; } for(intI=1; i<=n;i++) { if(!Ind[i]) {Qu.push (i); Vis[i]=1; } } intCnt=0; while(!Qu.empty ()) { intFr=Qu.top (); B[cnt++]=fr; Qu.pop (); intlen=ve[fr].size (); for(intI=0; i<len;i++) { inty=Ve[fr][i]; Ind[y]--; if(!ind[y]&&!Vis[y]) {Qu.push (y); Vis[y]=1; }}} LL sum=0; intmmin=2e5; for(intI=0; i<n;i++) { if(mmin>B[i]) {Mmin=B[i]; } Sum=sum+mmin; } printf ("%i64d\n", sum); } return 0;}
hdu-5695 Gym Class (greedy + topological sort)