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
并查集。
使用map哈希表进行string和int的相互映射,将姓名转换为从0开始的数字。
对各个帮派进行并查集合并后,修正每个结点的root,root不一样表示帮派不一样。
进行排序,root相同的排在一起,而且第一个总是临边总权值最大的,该结点为boss。
对排好序的数组进行遍历,若发现该帮派的人数大于两个,且总权值大于2*K(因为每条边都被用了两次),则压入结果数组。
<pre name="code" class="cpp">/*2015.7.24cyq*/ #include <iostream> #include <vector> #include <string> #include <map> #include <algorithm> #include <fstream> using namespace std; //ifstream fin("case3.txt"); //#define cin fin struct node{ int num; //结点序号,用于排序后还能找到该结点 int root; //结点所在集合的根,帮派代表 int weight; //结点相邻边的总权重 }; vector<node> nodes(2000);//结点集合 map<string,int> str2id;//姓名转id map<int,string> id2str;//id转姓名 int findroot(int x){ if(nodes[x].root!=x) nodes[x].root=findroot(nodes[x].root); return nodes[x].root; }; void unionSet(int ida,int idb){ int root1=findroot(ida); int root2=findroot(idb); if(root1!=root2) nodes[root1].root=root2; } bool cmpByRoot(const node &a,const node &b){//按帮派排序,每个帮派boss排最前 if(a.root<b.root) return true; else if(a.root==b.root){ if(a.weight>b.weight) return true; } return false; } bool comByName(const pair<string,int> &boss1,const pair<string,int> &boss2){ return boss1.first<boss2.first; } int main(){ int N,K; cin>>N>>K; for(int i=0;i<2*N;i++){//最多有2N个结点 nodes[i].num=i; nodes[i].root=i;//每个结点的根都是自身 } //根据输入进行并查集的合并 string a,b; int w,newid=0; for(int i=0;i<N;i++){ cin>>a>>b>>w; if(str2id.find(a)==str2id.end()){ str2id[a]=newid; id2str[newid]=a; newid++; } if(str2id.find(b)==str2id.end()){ str2id[b]=newid; id2str[newid]=b; newid++; } int ida=str2id[a]; int idb=str2id[b]; nodes[ida].weight+=w; nodes[idb].weight+=w; unionSet(ida,idb); } nodes.resize(newid); for(int i=0;i<newid;i++)//修正每个结点的root findroot(i); sort(nodes.begin(),nodes.end(),cmpByRoot);//按帮派排序 pair<string,int> boss;//boss姓名和帮派人数 vector<pair<string,int> > result; int pre=-1; int totalWeight=0; for(int i=0;i<newid;i++){ if(nodes[i].root!=pre){//新的帮派 if(i!=0&&boss.second>2&&totalWeight>2*K) result.push_back(boss);//压入前一个帮派的boss pre=nodes[i].root; boss.first=id2str[nodes[i].num]; boss.second=1; totalWeight=nodes[i].weight; }else{ boss.second++; totalWeight+=nodes[i].weight; } } if(boss.second>2&&totalWeight>2*K) result.push_back(boss); sort(result.begin(),result.end(),comByName); int n=result.size(); if(n==0){ cout<<0; return 0; } cout<<n<<endl; for(int i=0;i<n;i++) cout<<result[i].first<<" "<<result[i].second<<endl; return 0; }