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 
#include 
#include 
#include 
#include 
using namespace std;
mapm;
mapm2;
int n, k, weight[2005], cnt = 0, temp, father[2005];
vectorrelation, num(2005,0), sum(2005, 0),ans;
vectorname(2005);
int cmp(int a, int b){
    return name[a] < name[b];
}
int findfather(int x){
    if(x == father[x])
        return x;
    int temp = findfather(father[x]);
    father[x] = temp;
    return temp;
}
void Union(int a, int b){
    int faa = findfather(a), fab = findfather(b);
    if(weight[faa] > weight[fab])
        father[fab] = faa;
    else
        father[faa] = fab;
}
int main(){
    scanf("%d %d", &n, &k);
    k = k << 1;
    for(int i = 0; i < 2005; ++ i)
        father[i] = i;
    for(int i = 0; i < n; ++ i){
        string a, b;
        cin >> a >> b >>temp;
        if(m.find(a) == m.end()){
            name[cnt] = a;
            m[a] = cnt ++;
        }   
        if(m.find(b) == m.end()){
            name[cnt] = b;
            m[b] = cnt ++; 
        }
        weight[m[a]] += temp;
        weight[m[b]] += temp;
        relation.push_back(m[a]);
        relation.push_back(m[b]);
    }
    for(int i = 0; i < relation.size(); i += 2)
        Union(relation[i], relation[i + 1]);
    for(int i = 0; i < cnt; ++ i){
        int fa = findfather(i);
        sum[fa] += weight[i];
        num[fa] ++;
    }
    for(int i = 0; i < cnt; ++ i)
        if(num[i] > 2 && sum[i] > k)
            ans.push_back(i);
    sort(ans.begin(), ans.end(), cmp);
    printf("%d", ans.size());
    for(int i = 0; i < ans.size(); ++ i)
        printf("\n%s %d", name[ans[i]].c_str(), num[ans[i]]);
} 

 

 

你可能感兴趣的:(PAT甲级)