经典指数          
原因
1874
浏览数
0
收藏数
 

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. 输入描述: 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 Timewhere 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. 输出描述: 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. 输入例子: 8 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10 输出例子: 2AAA 3GGG 3

     举报   纠错  
 
切换
1 个答案
贴一个用并查集来做的方法 总体思路:         1.写两个类,Person类和Group类来记录每个人之间的信息。         2.每个人用并查集来联系是否属于同一个组         3.把符合要求(人数大于2,总权值大于阈值)的分组放入set中去,实现自动排序         4.遍历输出符合要求的分组 代码如下: #include #include #include #include #include using namespace std; struct Person { string name; int weight, father; Person(){} Person(string _name, int _weight, int _father) { name = _name; weight = _weight; father = _father; } }; struct Group { string head; int numOfPerson; int maxWeight, sumWeight; Group() { numOfPerson = 0; maxWeight = 0; sumWeight = 0; } bool operator<(const Group& that) const{ return (head < that.head); } }; vector persons; // 并查集的两个操作 int getFather(int x) { if(x != persons[x].father) { persons[x].father = getFather(persons[x].father); } return persons[x].father; } void unionSet(int x, int y) { x = getFather(x); y = getFather(y); if(x!=y) { persons[x].father = y; } } int main() { ios::sync_with_stdio(false); // 读入数据 int N, K; cin >> N >> K; map link; vector groups; string first, second; int weight, idx1, idx2; while(N--) { cin >> first >> second >> weight; // 按照first和second是否已经在分组里分成四个情况 if(link.count(first) && link.count(second)) { idx1 = link[first]; idx2 = link[second]; persons[idx1].weight += weight; persons[idx2].weight += weight; } else if(link.count(first) && !link.count(second)) { idx1 = link[first]; idx2 = persons.size(); link[second] = idx2; persons.push_back(Person(second,weight,idx2)); groups.push_back(Group()); persons[idx1].weight += weight; } else if(!link.count(first) && link.count(second)) { idx1 = persons.size(); idx2 = link[second]; link[first] = idx1; persons.push_back(Person(first,weight,idx1)); groups.push_back(Group()); persons[idx2].weight += weight; } else { idx1 = persons.size(); link[first] = idx1; idx2 = idx1+1; link[second] = idx2; persons.push_back(Person(first,weight,idx1)); persons.push_back(Person(second,weight,idx2)); groups.push_back(Group()); groups.push_back(Group()); } unionSet(idx1, idx2); groups[idx1].sumWeight += weight; } // 处理数据:分组并得到结果 int tf; int num = persons.size(); set result; // 计算各分组的信息 for(int i=0; i K && groups[i].numOfPerson > 2) { result.insert(groups[i]); } } // 输出结果 cout << result.size() << endl; set::iterator iter = result.begin(); for(;iter != result.end(); iter++) { cout << (*iter).head << " " << (*iter).numOfPerson << endl; } return 0; }
 
切换
撰写答案
扫描后移动端查看本题