题目描述
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 threthold 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.
输入描述:
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.
输出描述:
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.
示例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 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
输出
复制
2 AAA 3 GGG 3 0
感觉这个题的数据结构比较复杂,理清楚数据结构就成功了一半。
和普通的并查集一样,也需要father、height数组,但是在Union的时候,要比较的是每个节点的权重值,然后结点的father就是权重较高的结点。
我做的时候有点混了(混子的混),尤其是Union的时候,感觉效果不是很好,,,然后我就多Union了一遍,,这个问题就解决了,但显然并不是一个好的解决办法
/***************************************************
如何保证并查集中的所有节点都被遍历一遍?:map的iterator
***************************************************/
#include<iostream>
#include<map>
#include<stdlib.h>
#include<stack>
using namespace std;
struct point{
int node;
int weight;
};
struct pair{ //存储输入,方便在输入完成后Union
string n1;
string n2;
}pairs[1005];
map<string,string> father;
map<string,int> height;
map<string,point> result;
string Find(string i){
if(i!=father[i]){
father[i]=Find(father[i]);
}
return father[i];
}
void Union(string x,string y){ //这一步的更新保证father中的值始终都是最新的 即father[i]和Find(i)是一样的
x=Find(x);
y=Find(y);
if(height[x]>height[y]){
father[y]=x;
}
else {
father[x]=y;
}
}
string in2s(int x){
stack<int> res;
while(x){
res.push(x%10);
x/=10;
}
string r="";
while(!res.empty()){
r+=res.top()+'0';
res.pop();
}
return r;
}
int main(){
int n,k,count=0;
while(scanf("%d%d",&n,&k)!=EOF){
string name1,name2;
int len;
while(n--){
cin>>name1>>name2>>len;
if(father.find(name1)==father.end()){ //如果还没有出现过,初始化一下
father[name1]=name1;
height[name1]=0;
}
if(father.find(name2)==father.end()){
father[name2]=name2;
height[name2]=0;
}
height[name1]+=len;
height[name2]+=len;
pairs[count].n1=name1;
pairs[count++].n2=name2;
// cout<<name1<<":"<<height[name1]<<endl;
// cout<<name2<<":"<<height[name2]<<endl;
Union(name1,name2); //不能在此处Union
}
//这时每个点上的权重已经加完了,可以Union所有的输入
for(int i=0;i<count;i++){
Union(pairs[i].n1,pairs[i].n2);
}
for(int i=0;i<count;i++){
Union(pairs[i].n1,pairs[i].n2);
}
//将每一个都加到他的根节点上去
for(map<string,string>::iterator it=father.begin();it!=father.end();it++){
result[it->second].weight+=height[it->first];
result[it->second].node++;
// cout<< it->first<<" father:"<<it->second<<":"<<result[it->second].weight<<endl;
}
int res=0;
string resstr="";
for(map<string,point>::iterator it=result.begin();it!=result.end();it++){
if((it->second).node>2 && (it->second).weight > 2*k){ //节点多于两个且权重大于k
res++;
string temp=it->first;
temp+=" ";
char str1[10];
temp+=in2s((it->second).node);
// temp+=itoa((it->second).node,str1,10);
// cout<<(it->second).node<<endl;
temp+="\n";
resstr+=temp;
}
}
cout<<res<<endl;
cout<<resstr;
father.clear();
height.clear();
result.clear();
}
return 0;
}