1034 Head of a Gang(30 分)(C++)

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.

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

题目大意:两个用户若是通过电话,则与他们其中任意一个通过电话的都可以算作为一个群体之中的,现在要判断给出的通话可以将人们分为几个群体,群体的要求是:人数大于等于3,总通话时间超过k。

解题思路:这是计算连通分量的个数,我们可以考虑用DFS进行遍历,但同时本题中并没有对各节点之间关系给出要求,只有一个head与其他不同,我们运用并查集解决这类问题。

并查集模板:

int findfather(int x){
    int a=x;
    while(x!=father[x])
        x=father[x];
    //路径压缩
    while(a!=father[a]){
        int z=a;
        a=father[a];
        father[z]=x;
    }
    return x;
}//找某节点所属的gang的head
void Union(int a,int b){
    int faa=findfather(a);
    int fab=findfather(b);
    if(faa!=fab){
        if(weight[faa]>weight[fab])
            father[fab]=faa;
        else 
            father[faa]=fab;
    }
}//将a和b连接在一起

本题完整代码如下:

注意一下,因为我们对每次通话记录计算了两次,要对k*2。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
int father[2005],isRoot[2005],weight[2005],isgang[2005];
map<string,int>m;
map<int,string>m2;
std::vector<int>result;
int cmp(int a,int b){
    return m2[a]<m2[b];
}
int findfather(int x){
    int a=x;
    while(x!=father[x])
        x=father[x];
    while(a!=father[a]){
        int z=a;
        a=father[a];
        father[z]=x;
    }
    return x;
}
void Union(int a,int b){
    int faa=findfather(a);
    int fab=findfather(b);
    if(faa!=fab){
        if(weight[faa]>weight[fab])
            father[fab]=faa;
        else 
            father[faa]=fab;
    }
}
int main(){
    int n,k,cnt=0;
	  vector<int>v;
    scanf("%d %d",&n,&k);
	  k*=2;
    for(int i=0;i<2005;i++){
        father[i]=i;weight[i]=0;isgang[i]=0;isRoot[i]=0;
    }
    for(int i=0;i<n;i++){
        string a,b;
        cin>>a>>b;
        int temp;
        scanf("%d",&temp);
        if(m.count(a)==0){
            m[a]=cnt;m2[cnt]=a;
            cnt++;
        }
        if(m.count(b)==0){
            m[b]=cnt;m2[cnt]=b;
            cnt++;
        }
        weight[m[a]]+=temp;
        weight[m[b]]+=temp;
		v.push_back(m[a]);v.push_back(m[b]);
	}
	  for(int i=0;i<v.size();i=i+2)
		  Union(v[i],v[i+1]);
    for(int i=0;i<cnt;i++){
        isRoot[findfather(i)]++;
        isgang[findfather(i)]+=weight[i];
    }
    for(int i=0;i<cnt;i++){
        if(isRoot[i]>2&&isgang[i]>k)
            result.push_back(i);
    }
    sort(result.begin(), result.end(),cmp);
    printf("%d\n",result.size());
    for(int i=0;i<result.size();i++){
        cout<<m2[result[i]];
        printf(" %d\n",isRoot[result[i]]);
    }
	system("pause");
}

猜你喜欢

转载自blog.csdn.net/qq_41562704/article/details/82055517