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
using namespace std;
#define range 2003
map cnt;
map sti;
map its;
map final;
int fa[range],n,k;
int IsRoot[range];
vector 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;imaxn){
                    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<::iterator it=final.begin();it!=final.end();it++){
        printf("%s %d\n",it->first.c_str(),it->second);
    }
    return 0;
}

 

 

 

 

你可能感兴趣的:(PTA)