Gym Class (topological sort)

Source: Internet
Author: User
Tags unique id

Gym Class

Time limit:6000/1000 MS (java/others) Memory limit:65536/65536 K (java/others) total submission (s): 628 Accepted S Ubmission (s): 244

Problem description is well known, 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.

Input first 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.

Output the maximum score for each set of data.

Sample Input31 02 11 23 13 1

Sample Output126

Source2016 "Baidu Star"-Preliminary (Astar round2a)

Code:

#include <stdio.h>#include<queue>#include<vector>using namespacestd; #include<string.h>Const intmaxn=100010;structnode{intTo,next;}; Node EDG[MAXN*Ten];intHEAD[MAXN],ANS[MAXN],TOP,N,QUE[MAXN];p riority_queue<int>dl;typedef __int64 LL;voidTopu () {inttemp=0;  for(intI=1; i<=n;i++){        if(!Que[i]) Dl.push (i); }     while(!Dl.empty ()) {        intk=Dl.top ();        Dl.pop (); //temp++;ans[top++]=K;  for(intj=head[k];j!=-1; j=Edg[j].next) {Que[edg[j].to]--; if(!que[edg[j].to]) Dl.push (edg[j].to); }    }    intx = ans[0]; LL sum=0;  for(inti =0; I < top;i++){    //printf ("%d", ans[i]);        if(Ans[i] <x) {x=Ans[i]; } Sum+=x; }//puts ("");printf"%i64d\n", sum);}voidInitial () {memset (head,-1,sizeof(head)); memset (que,0,sizeof(que));  while(!dl.empty ()) Dl.pop (); Top=0;}intMain () {intt,m,a,b; scanf ("%d",&T);  while(t--) {initial (); scanf ("%d%d",&n,&M);  for(intI=0; i<m;i++) {scanf ("%d%d",&b,&a); Edg[i].to=A; Edg[i].next=Head[b]; HEAD[B]=i; Que[a]++;    } topu (); }    return 0;}

Gym Class (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.