1034 Head of a Gang (并查集)

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.


测试点3:由于最大可能有1000条边,所以最多有2000个节点,数组要开大一点
测试点4和5,可能是你写并查集的时候,判断的权值不是找的父亲(本人就是..)

详情见代码:
#include<iostream>
#include<algorithm>
#include<unordered_set>
#include<vector>
#include<string>
#include<unordered_map>
using namespace std;
unordered_map<string,int> mp;//将名字转化为下标
unordered_set<int> q;//记录可能的gang head
string toindex[2005];//记录对应下标的名字
int father[2005];//记得要开大一点
int weight[2005]={0};
int n,k;
struct node{
    string name1;
    string name2;
    int time;
};
typedef struct ts{
    string name;
    int num;
}ts;

node record[2005];
bool cmp(ts t1,ts t2){
    return t1.name < t2.name;
}
int findfather(int x){
    int temp=x;
    while(x!=father[x]){
        x=father[x];
    }
    //路径压缩
    while(temp != father[x]){
        int t=father[temp];
        father[temp]=father[x];
        temp=t;
    }
    return x;
}
void union1(int u,int v){
    int fatheru=findfather(u);
    int fatherv=findfather(v);
    if(fatheru != fatherv){
        father[fatherv]=fatheru;
    }
}

int main(){
    
    scanf("%d %d",&n,&k);
    string name1,name2;
    int time;
    int count=1;
    //father数组初始化
    for(int i=1;i<=2005;++i){
        father[i]=i;
    }
    for(int i=1;i<=n;i++){
        cin>>name1>>name2>>time;
        record[i].name1=name1;
        record[i].name2=name2;
        record[i].time=time;    
        //首次出现进行下标映射
        if(mp[name1] ==0 ){
            mp[name1]=count;
            toindex[count]=name1;
            count++;
        }
        if(mp[name2] == 0){
            mp[name2]=count; 
            toindex[count]=name2;
            count++;
        }
        //记录权值和
        weight[mp[name1]]+=time;
        weight[mp[name2]]+=time;
    }
    for(int i=1;i<=n;i++){
        int f1=findfather(mp[record[i].name1]);
        int f2=findfather(mp[record[i].name2]);
        if( f1 != f2 ){
            //比较俩者父亲的权值大小,进行和并,始终保证大的当小的父亲
            if(weight[f1] < weight[f2]){
                union1(mp[record[i].name2],mp[record[i].name1]);
            }else{
                union1(mp[record[i].name1],mp[record[i].name2]);
            }
        }
    }
    //记录所有可能的gang·head
    for(int i=1;i<count;i++){
        q.insert(findfather(i));
    }
    vector<ts> res;
    for(auto x:q){
        int sum=0,we=0;
        for(int i=1;i<count;i++){
            if(findfather(i) == x){
                sum++;
                we+=weight[i];
            }
        }
        //我们在加权值的时候,并查集中的一个集合的每个边都加了俩次,每一边的俩个节点都算了一次,所有实际的要除以2和门槛值来进行比较
        if(sum > 2 && we>k*2){
            ts t;
            t.name=toindex[x];
            t.num=sum;
            res.push_back(t);
        }
    }
    sort(res.begin(),res.end(),cmp);
    cout<<res.size()<<endl;
    for(auto x:res){
        cout<<x.name<<" "<<x.num<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值