PAT a1034题解
来源:互联网 发布:尔康制药 知乎 编辑:程序博客网 时间:2024/05/21 10:45
#include <iostream>#include <string>#include <map>using namespace std;const int maxn = 2010;map<int, string> intToString;map<string, int> stringToInt;map<string, int> Gang;int G[maxn][maxn] = {0}, weight[maxn] = {0};int n, k, numPerson = 0;bool vis[maxn] = {false};void DFS(int nowVisit, int& head, int&numMember, int& totalValue){numMember++;vis[nowVisit] = true;if(weight[nowVisit] > weight[head]){head = nowVisit;}for(int i = 0; i < numPerson; i++){if(G[nowVisit][i] > 0){totalValue += G[nowVisit][i];G[nowVisit][i] = G[i][nowVisit] = 0;if(vis[i] == false){DFS(i, head, numMember, totalValue);}}}} void DFSTrave(){for(int i = 0; i < numPerson; i++){if(vis[i] == false){int head = i, numMember = 0, totalValue = 0;DFS(i, head, numMember, totalValue);if(numMember > 2 && totalValue > k){Gang[intToString[head]] = numMember;}}}} int change(string str){if(stringToInt.find(str) != stringToInt.end()){return stringToInt[str];} else{stringToInt[str] = numPerson;intToString[numPerson] = str;return numPerson++; }}int main(){int w;string str1, str2;cin >> n >> k;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;}return 0;}
阅读全文
1 0
- PAT a1034题解
- PAT-A1034
- PAT A1034. Head of a Gang (30)
- pat-a1034. Head of a Gang (30)
- PAT [A1034]-Head of a Gang
- PAT 1001-1010 题解
- PAT 1011-1020 题解
- PAT 1021-1030 题解
- PAT 1031-1040 题解
- PAT 1041-1050 题解
- PAT 1051-1060 题解
- PAT(basic level)题解
- PAT乙级题解
- PAT题解目录
- PAT甲级题解目录
- PAT乙级题解目录
- PAT甲级1001题解
- PAT甲级1002题解
- 浅谈UITableview 的优化
- 离散化--uva12171 Sculpture
- hibernate模糊查询criteria封装参数
- 修改cmd的默认路径
- 《华为机试在线训练》之字符个数统计
- PAT a1034题解
- es6 函数形参的默认值
- PAT a1035题解
- [Head.First.PHP.&.MySQL]pdf
- closed-form solution(闭合解),analytical solution(解析解),numerical solution(数值解)
- 第二题 单例模式
- maven 项目对象模型
- verilog系统复位后拉高某个信号
- 深度学习实例1-----利用深度学习keras工具包实现销量预测