九度OJ 1446 Head of a Gang
来源:互联网 发布:灵族女捏脸数据图 编辑:程序博客网 时间:2024/05/21 15:47
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 threthold 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.
参考:pat 1034 Head of a Gang ,第一次用map,差点被vc6蹦出的无数个warning吓尿。。。。
题目描述:
- 输入:
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.
- 输出:
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 108 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
- 样例输出:
2AAA 3GGG 30
#include <iostream>#include <algorithm>#include <string>#include <vector>#include <map>using namespace std;map<string, int> weight;map<string, int> visit;map<string, vector<string> > Gang;map<string, int> head_and_num;int total_num, total_weight;string head;void DFS(string a){visit[a] = 1;total_num++;total_weight += weight[a];if(weight[a] > weight[head])head = a;for(vector<string>::iterator it=Gang[a].begin(); it!=Gang[a].end(); it++){if(visit[*it] == 0)DFS(*it);}}int main(){freopen("Test.txt","r",stdin);int N, K;while(cin >> N >> K){weight.erase(weight.begin(),weight.end());visit.erase(visit.begin(),visit.end());Gang.erase(Gang.begin(),Gang.end());head_and_num.erase(head_and_num.begin(),head_and_num.end());string a, b;int t;int i;for(i = 0; i < N; i++){cin >> a >> b >> t;Gang[a].push_back(b);Gang[b].push_back(a);weight[a] += t;weight[b] += t;visit[a] = 0;visit[b] = 0;}//for(map<string, int>::iterator iter = weight.begin(); iter != weight.end(); iter++)//cout<<iter->first<<" "<<iter->second<<endl;int gang_num = 0;for(map<string, int>::iterator It = visit.begin(); It != visit.end(); It++){if(visit[It->first] == 0){total_num = 0;total_weight = 0;head = It->first;DFS(It->first);if(total_num > 2 && total_weight/2 > K){head_and_num[head] = total_num;gang_num++;}}}cout << gang_num << endl;for(map<string, int>::iterator map_it = head_and_num.begin(); map_it != head_and_num.end(); map_it++){cout<<map_it->first<<" "<<map_it->second<<endl;}}return 0;}
- 九度OJ 1446 Head of a Gang
- 九度OJ 题目1446:Head of a Gang
- 九度 oj 题目1446:Head of a Gang
- 题目1446:Head of a Gang 九度OJ
- 九度OJ——1446Head of a Gang
- 九度1446 Head of a gang
- 九度OJ 1446 Head of a Gang -- 并查集
- 【学习笔记】〖九度OJ〗题目1446:Head of a Gang
- 九度OJ 1446 Head of a Gang -- 并查集
- 【九度】题目1446:Head of a Gang
- 九度 题目1446:Head of a Gang
- 九度考研真题 浙大 2012-4浙大1446:Head of a Gang
- Head of a Gang
- [WA]题目1446:Head of a Gang
- 题目1446:Head of a Gang
- 1034. Head of a Gang
- PAT_1034: Head of a Gang
- 1034. Head of a Gang
- Android测试原理(三)——使用Eclipse的ADT进行测试
- Winform-根据当前IP获取所在城市名称
- 实现圆角imageview
- Mybatis分页插件
- 网站项目必备——12款经典的白富美型 jQuery 图片轮播插件
- 九度OJ 1446 Head of a Gang
- 解决IE8弹出开发人员工具的方法
- UIScrollView实现循环滚动---快捷方式
- MapReduce实现大矩阵乘法及代码实现
- hdu 1159 最长公共子序列问题
- 10098 - Generating Fast
- 高级Oracle之死锁
- Javascript入门之改变图片
- ASP.NET批量删除数据