PAT甲级1034 Head of a Gang (并查集)

1034 Head of a Gang (30 分)

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 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

思路

题目的大致意思是在一堆通话记录中寻找可能的帮派头目,如果直接或间接通话的人数超过两个,并且总通话时长超过就认为是一个帮派。直接套并查集模版就可以了,如果两个人通过话就把他们并起来,并更新每个人的通话时长,最后再统计每个帮派的通话时长,需要注意的是,题目中的是指通话的记录一共有条,所以总人数可能达到,如果数组不开到2000的话测试点三会WA掉。另一种方法就是使用dfs或者bfs计算连通分量的数目,下面我只给出并查集的代码。

#include 
#include 
#include 
#include 
#include 
#define MAXN 2005
using namespace std;
struct herd{
    string name;
    int num = 0, time = 0, maxtime = 0;
}herds[MAXN];
unordered_mapump;
unordered_mapIdName;
vectorres;
int fa[MAXN], times[MAXN]; //times表示每个人的通话时长
bool cmp(herd a, herd b){return a.name < b.name;}
int find(int x){
    int a = x;
    while(x != fa[x])x = fa[x];
    while(a != fa[a]){
        int tep = a;
        a = fa[a];
        fa[tep] = x;
    }
    return x;
}
void Union(int a, int b){
    int faA = find(a), faB = find(b);
    if(faA != faB)fa[faA] = faB;
}
int main(){
    int n, k, time, idx = 0;
    string name1, name2;
    scanf("%d %d", &n, &k);
    for(int i = 0; i < n * 2; i++)fa[i] = i;
    for(int i = 0; i < n; i++){
        cin >> name1 >> name2 >> time;
        if(ump.count(name1) == 0){
            ump[name1] = idx;
            IdName[idx++] = name1;
        }
        if(ump.count(name2) == 0){
            ump[name2] = idx;
            IdName[idx++] = name2;
        }
        Union(ump[name1], ump[name2]);
        times[ump[name1]] += time;
        times[ump[name2]] += time;
    }
    for(int i = 0; i < idx; i++){
        int fai = find(i);
        herds[fai].num++;
        herds[fai].time += times[i];
        if(times[i] > herds[fai].maxtime){
            herds[fai].maxtime = times[i];
            herds[fai].name = IdName[i];
        }
    }
    for(int i = 0; i < n; i++)if(herds[i].num > 2 && (herds[i].time / 2) > k)res.push_back(herds[i]);
    if(res.size() == 0){printf("%d\n", 0); return 0;}
    else{
        sort(res.begin(), res.end(), cmp);
        printf("%d\n", res.size());
        for(int i = 0; i < res.size(); i++){
            cout << res[i].name << " " << res[i].num << endl;
        }
    }
    return 0;
}

你可能感兴趣的:(PAT甲级1034 Head of a Gang (并查集))