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 Aand 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 threthold 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 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
Sample Output 1:
2
AAA 3
GGG 3
Sample Input 2:
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
Sample Output 2:
0
分析: 变量有点多。。得有耐心写
/** * Copyright(c) * All rights reserved. * Author : Mered1th * Date : 2019-02-21-21.05.03 * Description : A1034 */ #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<string> #include<unordered_set> #include<map> #include<vector> #include<set> using namespace std; ; },weight[maxn]={}; bool vis[maxn]={false}; map<string,int> stringToInt; map<int,string> intToString; map<string,int> Gang; //Gang的人数 ,numPerson=; int n,th; int change(string str){ if(stringToInt.find(str)!=stringToInt.end()){ return stringToInt[str]; } else{ stringToInt[str]=numPerson; intToString[numPerson]=str; return numPerson++; } } void DFS(int index,int& head,int& numMember,int& totalValue){ numMember++; vis[index]=true; if(weight[index]>weight[head]){ head=index; } ;i<numPerson;i++){ ){ totalValue+=G[index][i]; G[index][i]=G[i][index]=; //遍历过后把该边删除 if(vis[i]==false) DFS(i,head,numMember,totalValue); } } } void DFSTravel(){ ;i<numPerson;i++){ if(vis[i]==false){ ,totalValue=; DFS(i, head, numMember, totalValue); && totalValue > th){ Gang[intToString[head]]=numMember; } } } } int main(){ #ifdef ONLINE_JUDGE #else freopen("1.txt", "r", stdin); #endif int w; string str1,str2; scanf("%d%d",&n,&th); ;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; } DFSTravel(); cout<<Gang.size()<<endl; for(auto it =Gang.begin();it!=Gang.end();it++){ cout<<it->first<<" "<<it->second<<endl; } ; }
1034 Head of a Gang (30 分)的更多相关文章
- PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)
1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's p ...
- 1034 Head of a Gang (30分)(dfs 利用map)
One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...
- 【PAT甲级】1034 Head of a Gang (30 分)
题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...
- pat 甲级 1034. Head of a Gang (30)
1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...
- 1034 Head of a Gang (30)(30 分)
One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
- 1034. Head of a Gang (30) -string离散化 -map应用 -并查集
题目如下: One way that the police finds the head of a gang is to check people's phone calls. If there is ...
- PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]
题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...
- 1034. Head of a Gang (30)
分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...
- PAT甲题题解-1034. Head of a Gang (30)-并查集
给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...
随机推荐
- js方法参数问题
大家都知道java是强类型语言,而js是弱类型语言,其实,它们之间的区别不止这一点,还有方法参数及调用问题,参看下例: js中:这里定义了一个query()方法 function query() { ...
- 新手小白Linux(Centos6.5)部署java web项目(mysql5.7安装及相关操作)
一.安装 参考:https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html # 检测系统中是否安装了mysql y ...
- STM32 Flash 永久用户数据空间
/********************************************************************************* * STM32 Flash 永久用 ...
- Size类型如何调用和定义
cv::Size sz = cv::Size(PROB_W, PROB_H);//Size(srcimage.cols, srcimage.rows) groundtoimage(xylimit, u ...
- 使用struts框架后的404错误
访问jsp界面后出现404错误,我开始以为是因为struts没有配置好,在网上找了很多解决方法, 试了一遍,无效, 最后在参考书上看到“struts2推荐把所有的视图界面存放在WEB-INF目录下,这 ...
- HPU 1166: 阶乘问题(一)
1166: 阶乘问题(一) [数学] 时间限制: 1 Sec 内存限制: 128 MB提交: 58 解决: 24 统计 题目描述 小H对阶乘!很感兴趣.现在他想知道N!N!的位数,由于NN太大了,所以 ...
- 51Nod:活动安排问题之二(贪心)
有若干个活动,第i个开始时间和结束时间是[Si,fi),同一个教室安排的活动之间不能交叠,求要安排所有活动,最少需要几个室? 输入 第一行一个正整数n (n <= 10000)代表活动的个数. ...
- SQL SERVER 2008R2 执行大脚本文件时,管理工具提示“内存不足”的解决方法
项目需求:当我把服务器上的数据库导出为SQL脚本时,在本地新建数据库,再导入执行SQL文件时报错,因为SQL文件过大,导致出现如下报错 如下图所示: ========================= ...
- hdu1080 DP(类最长公共子序列)
题意,有两个字符串,分别由四个字母构成,字母之间有不同的相似度,允许在两个字符串都按原顺序排列的情况下进行字母与字母之间的匹配,也可以让字母与空格匹配,即相当于在字符串中间加空格来一一匹配,每个字母与 ...
- u-boot分析
4.Bootloader:u-boot.2009.08分析与移植4.1:分析u-boot根文件夹下的Makefile,能够看到uboot编译的顺序例如以下,由此可知编译运行的第一个文件是cpu/$(C ...