1034 Head of a Gang (30分) 并查集

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

 

警察发现一个犯罪团伙的头目的方法之一是:检查人们的电话记录。如果A和B之间有一个电话记录,我们称A和B是相联系的。此联系的权重被定义成在这两人之间所有电话记录的总时长。一个"Gang"的定义如下:成员人数超过两人,且总的联系权重大于一个给定的权重下限K。对于每一个犯罪团伙,有着最大总权重的那个人就是团伙的头目。现在提供一个电话记录的列表,你需要寻找犯罪团伙及其头目。
 

 

用并查集 或者DFS都行

我这用并查集,有一个测试点3是数据规模的问题,1000条通话记录可以有2000人。

#include<bits/stdc++.h>
using namespace std;
#define range 2003
map<string,int> cnt;
map<string,int> sti;
map<int,string> its;
map<string,int> final;
int fa[range],n,k;
int IsRoot[range];
vector<int> s[range];
int find(int t){      // 查找 
    if(t==fa[t]){
        return t;
    }
    else{
        int f=find(fa[t]);    // 这三行是路径压缩
        fa[t]=f;
        return f;
    }
}
void Union(int a,int b){
    int Fa=find(a);
    int Fb=find(b);
    if(Fa!=Fb){
        fa[Fb]=Fa;     // 这个我测试过,如果写成这样fa[Fa]=Fb  会有错误
    }
}
void init(){
    for(int i=1;i<=n;i++){
        fa[i]=i;
        IsRoot[i]=0;
    }
}
int main(){
    int i,j,d,t=0,d1,d2;
    string t1,t2;
    scanf("%d %d",&n,&k);
    init();
    t1.resize(3);
    t2.resize(3);
    for(i=0;i<n;i++){
        scanf("%s %s %d",&t1[0],&t2[0],&d);
        if(cnt.count(t1)==0)
        {
            t++;
            sti.insert(make_pair(t1,t));
            its.insert(make_pair(t,t1));
            cnt.insert(make_pair(t1,d));
        }
        else
        {
            cnt[t1]+=d;
        }
        if(cnt.count(t2)==0)
        {
            t++;
            sti.insert(make_pair(t2,t));
            its.insert(make_pair(t,t2));
            cnt.insert(make_pair(t2,d));
        }
        else
        {
            cnt[t2]+=d;
        }
        d1=sti[t1];
        d2=sti[t2];
        Union(d1,d2);
    }
    n=t;                     // 记录实际的人数 
    for(i=1;i<=n;i++){       // 让同一派的人的fa指向同一人 
        find(i);
    }

    for(i=1;i<=n;i++){       // 统计每一派的人数
        IsRoot[fa[i]]++;
    }
    for(i=1;i<=n;i++){       // 将每一派的人放进一个vector里
        s[fa[i]].push_back(i);
    }
    for(i=1;i<=n;i++){
        if(IsRoot[i]!=0){
            int len=s[i].size();
            int maxn=-1,w,f=-1,sum=0;
            for(j=0;j<len;j++){              // 选出每一派的老大
                    w=cnt[its[s[i][j]]];

                if(w>maxn){
                    maxn=w;
                    f=s[i][j];
                }
                sum+=w;
            }
            if(s[i].size()>2&&sum/2>k){
                final.insert(make_pair(its[f],s[i].size()));
            }
        }
    }
    cout<<final.size()<<endl;
    for(map<string,int>::iterator it=final.begin();it!=final.end();it++){
        printf("%s %d\n",it->first.c_str(),it->second);
    }
    return 0;
}

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章