PAT-A-1034. Head of a Gang (30)
来源:互联网 发布:机械力学分析软件 编辑:程序博客网 时间:2024/05/22 00:10
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 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10Sample Output 1:
2AAA 3GGG 3Sample Input 2:
8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10Sample Output 2:
0
#include<iostream>#include<map>#include<cstdio>#include<string>using namespace std;const int maxn = 2000;map<string, int> strtoint;map<int ,string> inttostr;map<string, int> gang;bool vis[maxn] = { false };int weight[maxn] = { 0 };int g[maxn][maxn] = { 0 };int n, k, numperson = 0;int change(string s){ if (strtoint.find(s) != strtoint.end()) { return strtoint[s]; } else { strtoint[s] = numperson; inttostr[numperson] = s; return numperson++; }}void DFS(int root, int &head, int &member, int &tot){ member++; vis[root] = true; if (weight[root] > weight[head]) head = root; for (int i = 0; i < numperson; i++) { if (g[root][i]>0) { tot += g[root][i]; g[root][i] = g[i][root] = 0; if (vis[i]==false) DFS(i, head, member, tot); } }}void DFStrave(){ for (int i = 0; i < numperson; i++) { if (vis[i] == false) { int head = i, member = 0, tot = 0; DFS(i, head, member, tot); if (member>2 && tot > k) { string s=inttostr[head]; gang[s] = member; } } }}int main(){ cin >> n >> k; string str1, str2; int w; for (int i = 0; i < n; i++) { cin >> str1 >> str2 >> w; int id1 = change(str1); int id2 = change(str2); weight[id1] += w; weight[id2] += w; g[id1][id2] += w; g[id2][id1] += w; } DFStrave(); cout << gang.size() << endl; map<string, int> ::iterator it; for (it = gang.begin(); it != gang.end(); it++) { cout << it->first << " " << it->second << endl; } system("pause"); return 0;}
- PAT 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- pat 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- 【PAT】1034. Head of a Gang (30)
- PAT A 1034. Head of a Gang (30)
- PAT-A-1034. Head of a Gang (30)
- PAT 1034. Head of a Gang
- PAT 1034. Head of a Gang
- PAT 1034. Head of a Gang
- PAT 1034. Head of a Gang
- 【PAT】1034. Head of a Gang
- PAT A1034. Head of a Gang (30)
- pat-a1034. Head of a Gang (30)
- PAT Head of a Gang
- PAT (Advanced) 1034. Head of a Gang (30)
- 【PAT甲级】1034. Head of a Gang (30)
- vs工程 "生成事件" "自定义生成步骤"
- Caffe模型测试之分类错误分析
- log4j2使用详解
- Freeline
- Android四大组件与进程启动关系总结
- PAT-A-1034. Head of a Gang (30)
- html,css,js加载顺序
- 长牌游戏开发--搭建系统环境
- 内存对齐分配策略(含位域模式)
- SpringBoot 在启动时运行代码
- JavaScript学习笔记(第一天)
- jQuery
- iptables
- vs2010下MFC按钮打开文件夹并遍历下面所有文件