1034. Head of a Gang (30) time limit MS Memory limit 65536 KB code length limit 16000 B procedure StandardAuthor Chen, Yue
One of the police finds the head of a gang is to check people ' s phone calls. If There is a phone call between A and B, we say A and B are related. The weight of a relation is defined to being the total time length of all the phone calls made between the. A "Gang" is a cluster of the more than 2 persons The WHO be related to the other and total relation weight being greater than a Given threshold K. In each gang, the one and maximum total weight are the head. Now given a list of phones calls, you is supposed to find the gangs and the heads.
Input Specification:
Each input file contains the one test case. The first line contains positive numbers N and K (both less than or equal to), the number of the 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 "the" the "Call" and "time is the length of the". A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which are no more than minutes.
Output Specification:
For each test case, first print with a line the total number of gangs. Then for each gang, print in a line the name of the "the" and the "total" number of the members. It is guaranteed, the head is a unique for each gang. The output must is sorted according to the alphabetical order of the names of the heads.
Sample Input 1:
8 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
Sample Output 1:
2AAA 3GGG 3
Sample Input 2:
8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
Sample Output 2:
0
Submit Code
1#include <cstdio>2#include <stack>3#include <algorithm>4#include <iostream>5#include <stack>6#include <Set>7#include <map>8#include <vector>9 using namespacestd;Tenmap<string,vector<string> >Edge; Onemap<string,int>PV; Amap<string,BOOL>Vis; -map<string,int>output; - voidDFS (stringSstring&maxper,int&pernum,int&Total ) { thevis[s]=true; -pernum++; -total+=Pv[s]; - if(pv[s]>Pv[maxper]) { +Maxper=s; - } +vector<string>:: iterator it; A for(It=edge[s].begin (); It!=edge[s].end (); it++){ at if(!vis[*it]) { -DFS (*it,maxper,pernum,total); - } - } - } - intMain () { in //freopen ("D:\\input.txt", "R", stdin); - intn,k; toscanf"%d%d",&n,&k); + inti,v; - stringname1,name2; the for(i=0; i<n;i++){ *Cin>>name1>>name2; $scanf"%d",&v);Panax Notoginseng Edge[name1].push_back (name2); -pv[name1]+=v; thevis[name1]=false; + Edge[name2].push_back (name1); Apv[name2]+=v; thevis[name2]=false; + } -map<string,vector<string> >:: iterator it; $ intCount=0; $ stringMaxper; - intpernum,total; - for(It=edge.begin (); It!=edge.end (); it++){ the if(!vis[it->First ]) { -Maxper=it->First ;Wuyipernum=0; theTotal=0; -DFS (it->first,maxper,pernum,total); Wu if(pernum>2&&total*1.0/2>k) { -output[maxper]=Pernum; Aboutcount++; $ } - } - } -map<string,int>:: iterator ITT; A if(count) { +printf"%d\n", count); the for(Itt=output.begin (); Itt!=output.end (); itt++){ -Cout<<itt->First ; $printf"%d\n",itt->second); the } the } the Else{ theprintf"0\n"); - } in return 0; the}
pat1034. Head of a Gang (30)