PAT (Advanced) 1034. Head of a Gang (30)
2014-08-29 11:37
344 查看
#include <iostream> #include <string> #include <vector> #include <map> #include <algorithm> using namespace std; map<string, vector<string> > calls; map<string, bool> visit; map<string, int> weight; map<string, int> result; int cnt, totalweight; string head; void dfs(string str) { visit[str] = true; cnt++; totalweight += weight[str]; if (weight[str] > weight[head] || weight[str] == weight[head] && str < head) head = str; for (auto it = calls[str].begin(); it != calls[str].end(); ++it) { if (visit[*it] == false) dfs(*it); } } int main() { int n, k, time; string name1, name2; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> name1 >> name2 >> time; weight[name1] += time; weight[name2] += time; calls[name1].push_back(name2); calls[name2].push_back(name1); visit[name1] = false; visit[name2] = false; } for (auto it = visit.begin(); it != visit.end(); ++it) { if (visit[it->first] == false) { cnt = 0; totalweight = 0; head = it->first; dfs(it->first); if (cnt > 2 && totalweight / 2 > k) result[head] = cnt; } } cout << result.size() << endl; for (auto it = result.begin(); it != result.end(); ++it) cout << it->first << " " << it->second << endl; }参考:http://blog.csdn.net/matrix5467/article/details/8641186
相关文章推荐
- PAT (Advanced Level) 1034. Head of a Gang (30)
- PAT (Advanced Level) 1034. Head of a Gang (30) 黑帮头目,并查集
- PAT (Advanced Level)1034. Head of a Gang (30) map的灵活运用
- PAT (Advanced) 1034. Head of a Gang (30)
- 【PAT】【Advanced Level】1034. Head of a Gang (30)
- 1034. Head of a Gang (30)【离散化+并查集+搜索】——PAT (Advanced Level) Practise
- PAT 1034. Head of a Gang (30)
- 【PAT】1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- 浙大2012上机 PAT1034. Head of a Gang (30)
- pat 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)-PAT甲级真题
- Python实现Pat 1034. Head of a Gang (30)
- PAT甲题题解-1034. Head of a Gang (30)-并查集
- PAT (Advanced Level) Practise 1034 Head of a Gang (30)
- PAT_A 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 (Advanced Level) Practise 1034 Head of a Gang (30)