程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 1034. Head of a Gang (30)[離散化+並查集+搜索]——PAT (Advanced Level) Practise

1034. Head of a Gang (30)[離散化+並查集+搜索]——PAT (Advanced Level) Practise

編輯:C++入門知識

1034. Head of a Gang (30)[離散化+並查集+搜索]——PAT (Advanced Level) Practise


題目信息

1034. Head of a Gang (30)

時間限制100 ms
內存限制65536 kB
代碼長度限制16000 B

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

解題思路

先把名稱離散化,然後按輸入的打電話關系建立並查集,查找最終集合信息即可

AC代碼

#include 
#include
#include 
#include 
using namespace std;
struct NODE{
    char a[10], b[10];
    int v;
}node[1011];
struct RES{
    string maxName;
    int maxV, totalV, member;
    RES():maxV(0), totalV(0), member(0){}
};
map weight, lst; //weight記錄每人權重,lst用於離散化
map res; //結果
vector index(2020); //離散標記
int getfa(int a){
    return index[a] = (index[a] == a) ? a : getfa(index[a]);
}
void tomerge(int a, int b){
    index[getfa(a)] = index[getfa(b)];
}
int main()
{
    int n, k, v;
    char namea[10], nameb[10];
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; ++i){
        scanf("%s%s%d", node[i].a, node[i].b, &node[i].v);
        weight[node[i].a] += node[i].v;
        weight[node[i].b] += node[i].v;
    }
    int cnt = 0;
    for (map::iterator it = weight.begin(); it != weight.end(); ++it){
        index[cnt] = cnt; //初始化並查集
        lst[it->first] = cnt++; //離散編號
    }
    for (int i = 0; i < n; ++i){ //合並集合
        tomerge(lst[node[i].a], lst[node[i].b]);
    }
    for (map::iterator it = weight.begin(); it != weight.end(); ++it){
        int fa = getfa(lst[it->first]); //獲得團伙標識符(並查集的祖宗節點值)
        if (res[fa].maxV < it->second){
            res[fa].maxV = it->second;
            res[fa].maxName = it->first;
        }
        res[fa].totalV += it->second; //注意此處每對的值會加兩次,所以下方要除以2
        res[fa].member++; //團伙成員數加 1
    }
    map rs; //結果集
    for (map::iterator it = res.begin(); it != res.end(); ++it){
        if (it->second.member > 2 && it->second.totalV / 2  > k){
            rs[it->second.maxName] = it->second.member;
        }
    }
    printf("%d\n", rs.size());

    for (map::iterator it = rs.begin(); it != rs.end(); ++it){
        printf("%s %d\n", it->first.c_str(), it->second);
    }
    return 0;   
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved