PAT甲级 1034. Head of a Gang (30)

来源:互联网 发布:云计算价值 编辑:程序博客网 时间:2024/05/20 18:18

题目:

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 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
Sample Output 1:
2AAA 3GGG 3
Sample Input 2:
8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
Sample Output 2:
0
思路:

利用并查集将各个成员分组,根据根root和各成员的总时间排序,再利用gang的判断条件,判断出gang中的head,最后排序输出。

需要注意的是,并查集这里最后还要再查一下,因为可能是一个环。


代码:

#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;struct member{int id;string name;int time;int root;};struct head{string name;int num;bool operator < (const head &h1) const{return this->name < h1.name;}};//判断是否已存在成员int isexist(string &m,vector<member> &team){int flag=-1;int i;if (team.size() == 0)  //没有现有成员{i = flag;}else{for (i = 0; i < team.size(); ++i){if (m == team[i].name){break;}}if (i == team.size())        i = flag;}return i;}//创建成员int  addmem(vector<member> &team, string s,int t){int lable = isexist(s,team);int id;member m;if (lable<0){//不存在该点,创建该点m.name = s;m.time = t;team.push_back(m);id = team.size() - 1;team[id].id = id;team[id].root = id;}else{team[lable].time += t;id = lable;}return id;}//寻根函数int findroot(vector<member> &team, int id){int x = team[id].root;while (x != team[x].root){x = team[x].root;}team[id].root = x;return x;}void unitmember(vector<member> &team, int n1, int n2){int x = findroot(team,n1);int y = findroot(team, n2);team[x].root = y;}//排序函数bool comp(const member &m1, const member &m2){if (m1.root != m2.root){return m1.root < m2.root;}else{return m1.time > m2.time;}}int main(){//inputint N,K;cin >> N >> K;int i = 0;vector<member> all;string s1, s2;int time;member m;int lable,n1,n2;for (; i < N; ++i){cin >> s1 >> s2 >> time;if (time > 1000)time = 1000;n1=addmem(all,s1,time);n2=addmem(all, s2, time);unitmember(all,n1,n2);}//这里用了并查集,但可能会造成一个环,所以需要再处理一下for (i = 0; i < N; ++i){if (i != all[i].root){all[i].root = findroot(all,i);}}sort(all.begin(), all.end(), comp);vector<head> h;head hh;int j;i = j = 0;while (i < all.size()){j = i;while ((j<all.size())&&(all[j].root == all[i].root)){j++;}if ((j - i) <= 2){//人数不符要求i = j;}else{//人数符合要求int total_k = 0;int k = i;for (; k < j; ++k){total_k += all[k].time;}if (total_k <= 2 * K){//不符条件i = j;}else{//符合条件hh.name = all[i].name;hh.num = j - i;h.push_back(hh);i = j;}}}cout << h.size()  << endl;sort(h.begin(),h.end());for (i = 0; i < h.size(); ++i){cout << h[i].name << " " << h[i].num << endl;}system("pause");return 0;}