Garlic Guest | Topological sorting | Distribution on the HW Mountain

Source: Internet
Author: User

Every year, when the mountain carving will give the brothers a silver, before the silver, the mountain Eagle allows the people to express their views, because if they can not meet the views of everyone, who will make any big news. But everyone can only say in their opinion: "I think a cent of the silver should be more than B!" ”。 The mountain Eagle decided to find a distribution plan that would satisfy everyone's opinion, while making the total amount of silver for everyone to be the least, and the minimum amount of silver for each person was two.

Input format

First row of two integers n,m(0<n≤10000,0< M≤20000), indicating the total number of persons and total opinions;

FollowingMRows, two integers per lineA,B, separated by a space, indicating that a certain opinion that the silver of the younger brother should be more than the number B , all the younger brother starting from 1.

Output format

If a legitimate scheme cannot be found, the output Unhappy! (without quotation marks), or the output of a number represents the minimum total silver number.

Sample input
3 21 22 3
Sample output
303


1#include <bits/stdc++.h>2 using namespacestd;3 Const intMax_num =100005;4 /*5 IP: The first few sides6 indeg: Indicates the degree of penetration7 SEG:8 */9 intHead[max_num],ip,indegree[max_num];Ten intN,m,seq[max_num]; One  A structnote{ -     intV,next; - }edge[max_num]; the  - voidInit () - { -memset (head,-1,sizeof(head)); +memset (Indegree,0,sizeof(Indegree));  -IP =0;  + } A  at voidAddedge (intUintV//increase The edge, U is the starting point, V is the end point - { -EDGE[IP].V = V,edge[ip].next = Head[u],head[u] = ip++; - } -  - inttopo () in { -     intAns =0; toqueue<int>Q; +queue<int>money;//add a queue that stores silver based on the template -     intIndeg[max_num]; the      for(inti =1; I <= N; i++) *     { $Indeg[i] =Indegree[i];Panax Notoginseng         if(Indeg[i] = =0) -         { the Q.push (i); +Money.push ( -);//For a person with an entry level of 0, the base silver is A         }     the              +           -     }     $     intK =0; $     BOOLres =false; -      while(!q.empty ()) -     { the         if(Q.size ()! =1) -res =true;Wuyi         intU =Q.front (); the         inttemp =Money.front (); -Ans + =temp; Wu Q.pop (); - Money.pop (); Aboutk++; $          for(inti = Head[u]; I! =-1; i =edge[i].next) -         { -             intv =edge[i].v; -indeg[v]--; A             if(Indeg[v] = =0)//at this point, this man doesn't need more money than anyone else. +             { the Q.push (v); -Money.push (temp+1);//according to test instructions, the total amount of silver $             } the         } the     } the //if (k < n) return-1;//There are forward loops, in short, topological ordering is not possible the //if (res) return 0;//can be topological sorted, and only one way, the SEQ array is a well-ordered sequence - //return 1;////topological ordering is possible, and in many cases, the SEQ array is one of the sequences in     if(k < N) printf ("unhappy!\n"); the     Elseprintf"%d\n", ans); the      About     return 0; the } the intMain () the { +     intb; -      while(~SCANF ("%d%d",&n,&m)) the     {Bayi init (); the          for(inti =1; i<= m; i++) the         { -scanf"%d%d",&a,&b); - Addedge (b,a); theindegree[a]++;//the more you enter, the more money you have to divide. the         } the topo ();  the     } -     return 0; the}

Garlic Guest | Topological sorting | Distribution on the HW Mountain

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.