PAT 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
本期主要的思想是并查集
由于题目中的姓名格式十分严格,我们可以利用一个hash函数来获得一个唯一的index。然后我们在GangRecord中记录每个姓名所对应其相应的对应的组别和总时间,我们将所有的人分成好几个cluster。其中在添加原有两个都已添加过的成员时需要注意将两个不同的组别进行合并(类似于并查集中的union操作)。

  1 #include <iostream>
  2 #include <string>
  3 #include <vector>
  4 #include <algorithm>
  5 
  6 using namespace std;
  7 
  8 struct Gang
  9 {
 10     int clusterNum = 0;
 11     int time=0;
 12 };
 13 
 14 int HashIndex(string name)
 15 {
 16     return (name[0] - 'A') * 26 * 26 + \
 17         (name[1] - 'A') * 26 + \
 18         (name[2] - 'A');
 19 }
 20 
 21 string ReverseHash(int index)
 22 {
 23     int digit1, digit2, digit3;
 24     digit3 = index % 26;
 25     digit2 = (index / 26) % 26;
 26     digit1 = (index / 26 / 26) % 26;
 27     return string(1, digit1 + 'A') + \
 28         string(1, digit2 + 'A') + \
 29         string(1, digit3 + 'A');
 30 }
 31 
 32 Gang GangRecord[26 * 26 * 26];
 33 vector<string> vec[26 * 26 * 26];
 34 int totaltime[26 * 26 * 26];
 35 int maxtime[26 * 26 * 26];
 36 string maxname[26 * 26 * 26];
 37 
 38 int main()
 39 {
 40     int RecordNum, threshold;
 41     cin >> RecordNum >> threshold;
 42     int ClusterNum = 0;
 43 
 44     for (int i = 0; i < RecordNum; i++)
 45     {
 46         string name1, name2;
 47         int time;
 48         cin >> name1 >> name2 >> time;
 49         int index1 = HashIndex(name1);
 50         int index2 = HashIndex(name2);
 51         if (GangRecord[index1].clusterNum == 0 && GangRecord[index2].clusterNum == 0)
 52         {
 53             ClusterNum++;
 54             vec[ClusterNum].push_back(name1);
 55             vec[ClusterNum].push_back(name2);
 56             totaltime[ClusterNum] = time;
 57             GangRecord[index1].clusterNum = ClusterNum;
 58             GangRecord[index1].time = time;
 59             GangRecord[index2].clusterNum = ClusterNum;
 60             GangRecord[index2].time = time;
 61         }
 62         else if (GangRecord[index1].clusterNum != 0 && GangRecord[index2].clusterNum != 0)
 63         {
 64             GangRecord[index1].time += time;
 65             GangRecord[index2].time += time;
 66             totaltime[GangRecord[index1].clusterNum] += time;
 67             if (GangRecord[index1].clusterNum != GangRecord[index2].clusterNum)
 68             {
 69                 int clusterNum1 = GangRecord[index1].clusterNum;
 70                 int clusterNum2 = GangRecord[index2].clusterNum;
 71                 for (int i = 0; i < vec[clusterNum2].size(); i++)
 72                 {
 73                     string name = vec[clusterNum2][i];
 74                     GangRecord[HashIndex(name)].clusterNum = GangRecord[index1].clusterNum;
 75                 }
 76                 vec[clusterNum1].insert(vec[clusterNum1].end(), vec[clusterNum2].begin(), vec[clusterNum2].end());
 77                 vec[clusterNum2].clear();
 78                 totaltime[clusterNum1] += totaltime[clusterNum2];
 79                 totaltime[clusterNum2] = 0;
 80             }
 81         }
 82         else if (GangRecord[index1].clusterNum != 0)
 83         {
 84             vec[GangRecord[index1].clusterNum].push_back(name2);
 85             totaltime[GangRecord[index1].clusterNum] += time;
 86             GangRecord[index1].time += time;
 87             GangRecord[index2].clusterNum = GangRecord[index1].clusterNum;
 88             GangRecord[index2].time = time;
 89         }
 90         else
 91         {
 92             vec[GangRecord[index2].clusterNum].push_back(name1);
 93             totaltime[GangRecord[index2].clusterNum] += time;
 94             GangRecord[index2].time += time;
 95             GangRecord[index1].clusterNum = GangRecord[index2].clusterNum;
 96             GangRecord[index1].time = time;
 97         }
 98     }
 99 
100     for (int i = 0; i < 26 * 26 * 26; i++)
101     {
102         int clusterNum = GangRecord[i].clusterNum;
103         if (clusterNum != 0)
104         {
105             if (GangRecord[i].time > maxtime[clusterNum])
106             {
107                 maxtime[clusterNum] = GangRecord[i].time;
108                 maxname[clusterNum] = ReverseHash(i);
109             }
110         }
111     }
112     int GangNum = 0;
113     for (int index = 1; index <= ClusterNum; index++)
114     {
115         if (vec[index].size() > 2 && totaltime[index] > threshold)
116             GangNum++;
117     }
118     cout << GangNum << endl;
119     
120     sort(maxname+1, maxname+ClusterNum+1);
121     for (int i = 1; i <= ClusterNum; i++)
122     {
123         string name = maxname[i];
124         if (name.empty())
125             continue;
126         int index = GangRecord[HashIndex(name)].clusterNum;
127 
128         if (vec[index].size() > 2 && totaltime[index] > threshold)
129             cout << name << " " << vec[index].size() << endl;
130     }
131 }

 

 

posted @ 2015-08-12 10:47  JackWang822  阅读(219)  评论(0编辑  收藏  举报