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:

  1. 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:

  1. 8 59
  2. AAA BBB 10
  3. BBB AAA 20
  4. AAA CCC 40
  5. DDD EEE 5
  6. EEE DDD 70
  7. FFF GGG 30
  8. GGG HHH 20
  9. HHH FFF 10

Sample Output 1:

  1. 2
  2. AAA 3
  3. GGG 3

Sample Input 2:

  1. 8 70
  2. AAA BBB 10
  3. BBB AAA 20
  4. AAA CCC 40
  5. DDD EEE 5
  6. EEE DDD 70
  7. FFF GGG 30
  8. GGG HHH 20
  9. HHH FFF 10

Sample Output 2:

  1. 0
  2. 分析: 变量有点多。。得有耐心写
  1. /**
  2. * Copyright(c)
  3. * All rights reserved.
  4. * Author : Mered1th
  5. * Date : 2019-02-21-21.05.03
  6. * Description : A1034
  7. */
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<iostream>
  11. #include<cmath>
  12. #include<algorithm>
  13. #include<string>
  14. #include<unordered_set>
  15. #include<map>
  16. #include<vector>
  17. #include<set>
  18. using namespace std;
  19. ;
  20. },weight[maxn]={};
  21. bool vis[maxn]={false};
  22. map<string,int> stringToInt;
  23. map<int,string> intToString;
  24. map<string,int> Gang; //Gang的人数
  25. ,numPerson=;
  26. int n,th;
  27. int change(string str){
  28. if(stringToInt.find(str)!=stringToInt.end()){
  29. return stringToInt[str];
  30. }
  31. else{
  32. stringToInt[str]=numPerson;
  33. intToString[numPerson]=str;
  34. return numPerson++;
  35. }
  36. }
  37.  
  38. void DFS(int index,int& head,int& numMember,int& totalValue){
  39. numMember++;
  40. vis[index]=true;
  41. if(weight[index]>weight[head]){
  42. head=index;
  43. }
  44. ;i<numPerson;i++){
  45. ){
  46. totalValue+=G[index][i];
  47. G[index][i]=G[i][index]=; //遍历过后把该边删除
  48. if(vis[i]==false) DFS(i,head,numMember,totalValue);
  49. }
  50. }
  51. }
  52.  
  53. void DFSTravel(){
  54. ;i<numPerson;i++){
  55. if(vis[i]==false){
  56. ,totalValue=;
  57. DFS(i, head, numMember, totalValue);
  58. && totalValue > th){
  59. Gang[intToString[head]]=numMember;
  60. }
  61. }
  62. }
  63. }
  64.  
  65. int main(){
  66. #ifdef ONLINE_JUDGE
  67. #else
  68. freopen("1.txt", "r", stdin);
  69. #endif
  70. int w;
  71. string str1,str2;
  72. scanf("%d%d",&n,&th);
  73. ;i<n;i++){
  74. cin>>str1>>str2>>w;
  75. int id1=change(str1);
  76. int id2=change(str2);
  77. weight[id1]+=w;
  78. weight[id2]+=w;
  79. G[id1][id2]+=w;
  80. G[id2][id1]+=w;
  81. }
  82. DFSTravel();
  83. cout<<Gang.size()<<endl;
  84. for(auto it =Gang.begin();it!=Gang.end();it++){
  85. cout<<it->first<<" "<<it->second<<endl;
  86. }
  87. ;
  88. }
  1.  
  1.  

1034 Head of a Gang (30 分)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 【PAT甲级】1034 Head of a Gang (30 分)

    题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...

  4. pat 甲级 1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

  5. 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 ...

  6. PAT 1034. Head of a Gang (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...

  7. 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 ...

  8. 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 ...

  9. 1034. Head of a Gang (30)

    分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...

  10. PAT甲题题解-1034. Head of a Gang (30)-并查集

    给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...

随机推荐

  1. Cross-Site Script

    Cross-Site Script(跨站脚本)XSS 整理于<浅析XSS(Cross Site Script)漏洞原理>   了解XSS的触发条件就先得从HTML(超文本标记语言)开始,我 ...

  2. Python实现对Android截图

    背景: 测试过程中,总是需要对Android设备进行截图,然后在截图中标注问题描述: 手动方式: 1.使用adb scrrencap /sdcard/screen.png 命令对Android设备进行 ...

  3. LARC Caffe笔记(二) 训练自己的img

    继看完 贺完结!CS231n官方笔记 上一次已经成功跑起caffe自带的例程,mnist和cifar10 但是终归用的是里面写好的脚本,于是打算训练自己的img 〇.目标 准备好food图片3类(出于 ...

  4. 玩转TypeScript(引言&文章目录) --初看TypeScript.

    JavaScript过去一直被当作一种玩具语言存在,直到2005年以后,这门语言又开始活跃并可以说是火爆,而且随着浏览器版本的不断升级和完善,各种DOM之间的兼容性已经渐渐的被各种技术解决了,比如经典 ...

  5. 【linux基础】区块选择VisualBlock

    前言 有时候使用linux需要选择某一块区域进行处理,比如对某些列的某些行,类似于Ultraedit的列模式,其实vim中就有相关的功能,此时可以使用vim的区块选择进行处理. 区块选择 1.在一般模 ...

  6. 蓝桥杯 ALGO-1:区间k大数查询

      算法训练 区间k大数查询   时间限制:1.0s   内存限制:256.0MB        问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个 ...

  7. Mac无法上网

    今天mac突然无法上网了, 家里的大部分设备, 都出现了重启后无法上网的问题, 猜测可能是dns有问题了. 于是乎, 在mac中添加了如下DNS 114.114.114.114 8.8.8.8 1.1 ...

  8. hdu1238 Substrings 扩展KMP

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  9. web页面的数据从excel中读取

    # -*- coding: utf-8 -*- import xdrlib ,sysimport xlrdimport datetimeimport jsonimport conf,reimport ...

  10. SharedImageCollection

    https://documentation.devexpress.com/#WindowsForms/clsDevExpressUtilsSharedImageCollectiontopic The ...