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 分)的更多相关文章

  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. 解释 Hello1.java

    hello1.java 代码 package javaeetutorial.hello1; import javax.enterprise.context.RequestScoped; import ...

  2. 将PS/2接口鼠标改造成USB接口鼠标

    改造接线图 不是所有PS/2鼠标都可以改为USB鼠标的,可以改的PS/2鼠标的特征: A.早期PS/2鼠标电路板一般带有两块集成电路,(一块光电感应,一块按键或USB协议转换,和一只24M的晶体振荡器 ...

  3. BadUSB测试记录

    0x00 前言 不是很新的东西,其他作者已对此做过研究测试,本文仅用来记录操作过程,保存日志,说明细节. 0x01参考资料 https://github.com/adamcaudill/Psychso ...

  4. Emacs矩形操作

    原始矩形块模式 emacs以C-x r开头的命令来进行矩形操作.先用C-space或者C-@设一个mark,移动光标到另一点,用以下命令进行列操作: C-x r r 复制一个矩形区域到寄存器 C-x ...

  5. 用virtualbox虚拟机无法上网的解决方法

    用virtualbox虚拟机无法上网的解决方法   首先保证你的本机是可以正常上网的   启动虚拟机系统前,选择安装好的虚拟PC,点击"设置"按钮,然后切到"网络&quo ...

  6. Python 之 numpy 和 tensorflow 中的各种乘法(点乘和矩阵乘)

    点乘和矩阵乘的区别: 1)点乘(即“ * ”) ---- 各个矩阵对应元素做乘法 若 w 为 m* 的矩阵,x 为 m*n 的矩阵,那么通过点乘结果就会得到一个 m*n 的矩阵. 若 w 为 m*n ...

  7. 12.2 linux下的线程

    什么是线程: 在一个程序里的一个执行路线就叫做线程(thread),更准确的定义是:线程是“一个进程内部的控制序列” 一切进程至少都有一个执行线程 进程与线程: 进程是资源竞争的基本单位 线程是程序执 ...

  8. 通过JS动态创建和删除HTML元素

    <script type="text/javascript" language="Javascript"> function InputOnBlur ...

  9. Dlib与OpenCV图片转换

    re: 1. https://zhuanlan.zhihu.com/p/36489663 2. https://stackoverflow.com/questions/38180410/convert ...

  10. liunx网络基本命令

    1.ifconfig 查看本机的ip或者网关 更改本机的ip地址 2.sudo reboot    重启 跟 sudo shutdown -r new  是一样的意思