2016 "Baidu Star"-Preliminary (Astar round2a) 1006 Gym Class topology sorting

Source: Internet
Author: User

Gym ClassTime limit:6000/1000 MS (java/others)Memory limit:65536/65536 K (java/others)Problem Description

As 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 to be lined up, assuming that at first everyone has a unique ID, from 1 to nN, after the row, each student will find the minimum ID of all the students in front of them, including themselves, as a score of their own evaluation of 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

The first line is an integer tT, which represents t (1 \leq t \leq)T(1≤t≤30) group data.

For each set of data, enter two integers in the first rowNN and m (1 \leq n \leq 100000, 0 \leq m \leq 100000)M(1≤N≤10000< C18>0,0≤M≤100000), representing the total number of people and the preferences of some students respectively.

NextMm rows, two integers per lineAA andB (1 \leq A, b \leq N)b(1≤a,b≤N), indicating that the student with ID a A does not want the student ID b b toprecede him (her). You can assume that the title guarantee has at least one arrangement that meets all requirements.

Output

For each set of data, the maximum score is output.

Sample Input
31 02 11 23 13 1
Sample Output
126
Idea: Each time take the largest number put in front, topsort;
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<map>#include<vector>using namespacestd;#definell __int64#defineINF 99999999intn,m;intdu[100010];intflag[100010];intans[100010];voidinit () {memset (Du,0,sizeof(flag)); memset (Flag,0,sizeof(flag));}intMain () {intx,y,z,i,t; intT; scanf ("%d",&T);  while(t--) {init (); Vector<int>v[100010]; Priority_queue<int>Q; scanf ("%d%d",&n,&m);  for(i=0; i<m;i++)        {            intU,VV; scanf ("%d%d",&u,&vv);            V[u].push_back (VV); DU[VV]++; }        intli{0;  for(i=1; i<=n;i++)        if(du[i]==0) Q.push (i), Flag[i]=1;  while(!Q.empty ()) {            intgg=Q.top ();            Q.pop (); Ans[ji++]=GG;  for(i=0; I<v[gg].size (); i++)            {                intvv=V[gg][i]; DU[VV]--; if(du[vv]==0&&!FLAG[VV]) Q.push (VV), FLAG[VV]=1; }        }        intminn=inf; ll Getans=0;  for(i=0; i<ji;i++) Minn=min (Minn,ans[i]), getans+=Minn; printf ("%i64d\n", Getans); }    return 0;}

2016 "Baidu Star"-Preliminary (Astar round2a) 1006 Gym Class topology sorting

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.