One way that 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 that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls 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 calls, 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 10Sample Output 1:
2 AAA 3 GGG 3Sample 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 10Sample Output 2:
0
# include <iostream> # include <algorithm> # include <queue> # include <cstring> # include <string> # include <map> # include <stack> using namespace std; const int _size = 2005; struct { map<string,int> str2int; map<int,string> int2str; string operator[] (int n) { return int2str[n]; } int operator[] (string& str) { static int cnt = 0; if (str2int.find(str)==str2int.end()){ str2int[str] = cnt; int2str[cnt] = str; return cnt++; } return str2int[str]; } } trans; struct Disjoint_Set { int father[_size]; int weight[_size]; int weight_sum[_size]; int n; Disjoint_Set(int _n):n(_n){memset(father,-1,sizeof(father));memset(weight,0,sizeof(weight));} int FindRoot(int i){return father[i]<0?i:(father[i] = FindRoot(father[i]));} void AddWeight(int i,int adder) {weight_sum[i] = weight[i] = weight[i] + adder;} void Union(int i,int j) { if ((i = FindRoot(i))!=(j = FindRoot(j))){ if (weight[i]<weight[j]) swap(i,j); father[i] += father[j],father[j] = i; weight_sum[i] += weight_sum[j]; } } int getSum(int i){return -father[i];} }; typedef pair<int,int> line; typedef pair<string,int> gang; int main() { int i,j,temp; int n,k,total = 0; cin >> n >> k; Disjoint_Set disj_s((n<<1) + 5); stack<line> line_stk; for (i=0;i<n;i++) { string a,b; cin >> a >> b >> temp; int na = trans[a]; int nb = trans[b]; disj_s.AddWeight(na,temp); disj_s.AddWeight(nb,temp); line_stk.push(line(na,nb)); total = max(na,nb); } n = total+1; while (!line_stk.empty()) { line& loca = line_stk.top(); disj_s.Union(loca.first,loca.second); line_stk.pop(); } priority_queue<gang,vector<gang>,greater<gang> > pri_que; int *fath = disj_s.father; int *weight_sum = disj_s.weight_sum; for (i=0;i<n;i++) if (fath[i]<-2&&weight_sum[i]/2>k) pri_que.push(gang(trans[i],disj_s.getSum(i))); cout << pri_que.size() << endl; while (!pri_que.empty()) { gang loca = pri_que.top();pri_que.pop(); cout << loca.first << ' ' << loca.second << endl; } return 0; }为了方便复习,以后每份代码都配上代码的运行结果,必要的时候配上牛客网的运行结果