PAT甲级练习1034. Head of a Gang (30)
来源:互联网 发布:混沌摆钢铁侠淘宝 编辑:程序博客网 时间:2024/05/22 09:47
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
这题基本参考了http://blog.csdn.net/jtjy568805874/article/details/50837585,用到了并查集的概念,一开始将各个点赋予独立的组号,之后根据输入的连接信息将相连的点赋予权值最大的点的组号,最后统计组号信息以及当中的最大值。。。并查集的相关文献http://blog.csdn.net/dm_vincent/article/details/7655764
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <map>#include <set>#include <string>#include <string.h>using namespace std;const int MAX=2e3+1;map<string,int> m;//name和序号的对应string s[MAX];//序号int cnt=0;int x[MAX], y[MAX], w[MAX], l[MAX], sum[MAX], num[MAX];vector<string> head;int get(int p){//得到对应序号的组号return p == l[p] ? p : l[p] = get(l[p]);}int main(){int n, threshold, weight, tmpx;string n1, n2;scanf("%d %d", &n, &threshold);for(int i=1; i<=2*n; i++) l[i] = i;//组号初始化和序号一样for(int i=1; i<=n; i++){cin>>n1>>n2>>weight;if(!m[n1]) s[++cnt] = n1, m[n1] = cnt;if(!m[n2]) s[++cnt] = n2, m[n2] = cnt;x[i] = m[n1]; y[i] = m[n2];w[x[i]] += weight; w[y[i]] += weight;}for(int i=1; i<=n; i++){int tx=get(x[i]), ty=get(y[i]);if(tx==ty) continue;if(w[tx]>w[ty]) l[ty] = tx;//权值更大的就是headelse l[tx] = ty;}for(int i=1; i<=cnt; i++) l[i]=get(i), sum[l[i]] += w[i], num[l[i]]++;for(int i=1; i<=cnt; i++) if(num[i]>2 && sum[i]>2*threshold) head.push_back(s[get(i)]);sort(head.begin(), head.end());printf("%d\n", head.size());if(!head.empty()){for(int i=0; i<head.size(); i++) cout<<head[i]<<" "<<num[m[head[i]]]<<endl;}cin>>n;return 0;}
- PAT甲级练习1034. Head of a Gang (30)
- 【PAT甲级】1034. Head of a Gang (30)
- 1034. Head of a Gang (30) PAT 甲级
- PAT甲级1034. Head of a Gang (30)
- PAT 甲级 1034. Head of a Gang (30)
- PAT甲级 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)-PAT甲级真题
- 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)
- 1034. Head of a Gang (30)-PAT甲级真题(图的遍历dfs)
- 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
- 优化MySchool 第八章 酒店管理系统
- 使用java.util.zip包压缩和解压缩文件
- Python基础语法——循环(五)
- java-面向对象编程-接口
- javaweb学习总结——HttpServletResponse对象(一)
- PAT甲级练习1034. Head of a Gang (30)
- 将List<T>导出到CSV
- 389. Find the Difference
- MFC工程中使用printf输出到控制台
- poj1797
- 从C#到TypeScrip
- 蓝桥杯 报时助手
- 05 租房网
- 搭建多模块企业级项目