九度OJ 1446 Head of a Gang
2014-02-27 16:28
447 查看
参考:pat 1034 Head of a Gang ,第一次用map,差点被vc6蹦出的无数个warning吓尿。。。。题目描述: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 allthe 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 givena list of phone calls, you are supposed to find the gangs and the heads.输入: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 morethan 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 outputmust be sorted according to the alphabetical order of the names of the heads.样例输入:
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 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样例输出:
2 AAA 3 GGG 3 0
#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
- 九度1446 Head of a gang
- 【九度】题目1446:Head of a Gang
- 九度 题目1446:Head of a Gang
- 九度考研真题 浙大 2012-4浙大1446:Head of a Gang
- 题目1446:Head of a Gang
- [WA]题目1446:Head of a Gang
- 【学习笔记】〖九度OJ〗题目1446:Head of a Gang
- 九度OJ 1446 Head of a Gang -- 并查集
- 九度 oj 题目1446:Head of a Gang
- 九度OJ 1446 Head of a Gang -- 并查集
- Head of a Gang 1446 并查集
- 题目1446:Head of a Gang(并查集操作)
- Zhejaing university----Head of a Gang
- Head of a Gang
- pat 1034 Head of a Gang
- 九度OJ 1446 Head of a Gang -- 并查集
- 九度题目1446:Head of a Gang 2012年浙江大学计算机及软件工程研究生机试真题
- PAT_1034: Head of a Gang
- 1034. Head of a Gang