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

 

 

 

 

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