1034. Head of a Gang (30) [discretization + parallel query set + SEARCH] -- PAT (Advanced Level) Practise

Source: Internet
Author: User

1034. Head of a Gang (30) [discretization + parallel query set + SEARCH] -- PAT (Advanced Level) Practise
Question Information

1034. Head of a Gang (30)

Time limit 100 MS
The memory limit is 65536 kB.
Code length limit: 16000 B

One way that the police finds the head of a gang is to check people's phone CILS. if there is a phone call between A and B, we say that A and B is related. the weight of a relation is defined to be the total time length of all the phone CILS made between the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. in each gang, the one with maximum total weight is the head. now given a list of phone CILS, you are supposed to find the gangs and the heads.

Input Specification:

Each input file contains one test case. for each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. then N lines follow, each in the following format:

Name1 Name2 Time

Where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.

Output Specification:

For each test case, first print in a line the total number of gangs. then for each gang, print in a line the name of the head and the total number of the members. it is guaranteed that the head is unique for each gang. the output must be sorted according to the alphabetical order of the names of the heads.

Sample Input 1:
8 59
Aaa bbb 10
Bbb aaa 20
Aaa ccc 40
Ddd eee 5
Eee ddd 70
Fff ggg 30
Ggg hhh 20
Hhh fff 10
Sample Output 1:
2
AAA 3
GGG 3
Sample Input 2:
8 70
Aaa bbb 10
Bbb aaa 20
Aaa ccc 40
Ddd eee 5
Eee ddd 70
Fff ggg 30
Ggg hhh 20
Hhh fff 10
Sample Output 2:
0

Solutions

First, the name is discretization, and then the set is created and queried based on the input call relationship to find the final set information.

AC code
# Include
  
   
# Include
   # Include
    
     
# Include
     
      
Using namespace std; struct NODE {char a [10], B [10]; int v;} node [1011]; struct RES {string maxName; int maxV, totalV, member; RES (): maxV (0), totalV (0), member (0) {}}; map
      
        Weight, lst; // weight records the weight of each person, and lst is used to discretization map
       
         Res; // result vector
        
          Index (2020); // discrete mark int getfa (int a) {return index [a] = (index [a] = )? A: getfa (index [a]);} void tomerge (int a, int B) {index [getfa (a)] = index [getfa (B)];} int main () {int n, k, v; char namea [10], nameb [10]; scanf ("% d", & n, & k ); for (int I = 0; I <n; ++ I) {scanf ("% s % d", node [I]. a, node [I]. b, & node [I]. v); weight [node [I]. a] + = node [I]. v; weight [node [I]. b] + = node [I]. v;} int cnt = 0; for (map
         
           : Iterator it = weight. begin (); it! = Weight. end (); ++ it) {index [cnt] = cnt; // initialize and query the set lst [it-> first] = cnt ++; // discrete number} for (int I = 0; I <n; ++ I) {// merge the set tomerge (lst [node [I]. a], lst [node [I]. b]);} for (map
          
            : Iterator it = weight. begin (); it! = Weight. end (); ++ it) {int fa = getfa (lst [it-> first]); // obtain the Group Identifier (and query the ancestor node value of the Set) if (res [fa]. maxV <it-> second) {res [fa]. maxV = it-> second; res [fa]. maxName = it-> first;} res [fa]. totalV + = it-> second; // note that the value of each pair is added twice. Therefore, divide the value below by 2 res [fa]. member ++; // number of member groups plus 1} map
           
             Rs; // result set for (map
            
              : Iterator it = res. begin (); it! = Res. end (); ++ it) {if (it-> second. member> 2 & it-> second. totalV/2> k) {rs [it-> second. maxName] = it-> second. member;} printf ("% d \ n", rs. size (); for (map
             
               : Iterator it = rs. begin (); it! = Rs. end (); ++ it) {printf ("% s % d \ n", it-> first. c_str (), it-> second);} return 0 ;}
             
            
           
          
         
        
       
      
     
    
  

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.