【PAT甲级】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 threshold, 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.

ample 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

题意:警方通过核对人们的通话记录查找团伙的头目。如果两个人有一条通话记录,那么称这两个人有关系。两个人关系的权重用他们之间通话记录的总长度表示。如果一个群体超过2个人,相互有联系,而且群体总权重超过阈值K,那么把这个群体叫做gang。在gang中,权重最高的人叫做head。现在给定N条通话记录,以及阈值K,找出所有gang的head,以及gang中团体的个数。

思路:参考1034. Head of a Gang (30)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <cmath>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define mem(a,n) memset(a,n,sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
const ll INF=0x3f3f3f3f;
const int N = 2e3+5;

int n,k;
vector<int>par(N);
vector<int>weight(N);
map<string, int>name;
map<int,string>rname;
struct Result {
    int nodes;///节点总数
    int weight;///总权重
    int head;///首领
    Result() {}
    Result(int a,int b,int c):nodes(a),weight(b),head(c) {}
    bool operator < (const Result &m)const {
        return rname[head]<rname[m.head];
    }
} ;
int Find(int x) { ///找根并压缩路径
    return par[x]==x?x:(par[x]=Find(par[x]));
}
void Unite(int x,int y) { ///合并有关系的两个人
    int pax=Find(x);
    int pay=Find(y);
    if(pax>pay) {
        par[pax]=pay;
    }
    if(pax<pay) {
        par[pay]=pax;
    }
}
void init() {
    rep(i,0,N) par[i]=i;
}
map<int, Result>res;
vector<Result>ans;
int main() {
    scanf("%d%d",&n,&k);
    init();
    int cnt=0;///节点总数
    rep(i,0,n) {
        string s1,s2;
        int w;
        cin>>s1>>s2>>w;
        if(!name.count(s1)) {
            name[s1]=cnt++;
            rname[name[s1]]=s1;
        }
        if(!name.count(s2)) {
            name[s2]=cnt++;
            rname[name[s2]]=s2;
        }
        weight[name[s1]]+=w;
        weight[name[s2]]+=w;
        Unite(name[s1],name[s2]);
    }
    rep(i,0,cnt) {
        int pai=Find(i);///找节点i的根
        if(!res.count(pai)) {
            Result r=Result{1,weight[i],i};
            res[pai]=r;
        } else {
            res[pai].nodes++;
            res[pai].weight+=weight[i];
            if(weight[i]>weight[res[pai].head]) { ///当首领的权重小时
                res[pai].head=i;
            }
        }
    }
    for(map<int,Result>::iterator it=res.begin(); it!=res.end(); it++) {
        if(it->second.nodes > 2 && (it->second.weight)/2 > k) {
            ans.push_back(it->second);
        }
    }
    if(ans.empty()) {
        printf("0");
    } else {
        sort(ans.begin(),ans.end());
        printf("%d\n",ans.size());
        rep(i,0,ans.size()) {
            printf("%s %d\n",rname[ans[i].head].c_str(),ans[i].nodes);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/feng_zhiyu/article/details/81293169