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.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...
随机推荐
- 201621123010《Java程序设计》第10周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 结合题集题目7-1回答 1.1 自己以前编写的代码中经常出现 ...
- 一个TED演讲背后的文化论
0. 前言 写这个前言让我很难受,当然不是心情难受哈,此时的状态是很High的哦,大中午觉都省了, 说难受是我觉得我这语言文字太渣了,相比今天的主题确实很没“文化”.但我也很庆幸,能 看到这么个人认为 ...
- Python 水果统计
f = open("水果.txt", mode="r", encoding="utf-8") lst = [] for line in f: ...
- Ubuntu终端点击确定按钮的方法
Ubuntu终端里出现需要点击 确定 按钮的时候,直接鼠标点击 确定 是不生效的,这个时候需要利用tab键选中这个 确定 按钮,然后回车键就可以了.
- 【微信小程序推广营销】教你微信小程序SEO优化,让你的小程序快人一步抢占先机
今年一月份上线的小程序,经过近一年的沉淀发酵,现在也进入了快速发展期. 在未来肯定会有越来越多的小程序诞生,小程序多了就需要搜索,那么如何让自己的小程序在众多的小程序中脱颖而出,这就需要小程序SEO优 ...
- Global.asax 详解
在网上找了N多相关的东西总说的不够细,现在终于找到了.可以了解web.cofig和Global.asax之间的关系以及执行的顺序. 在Global.asax.cs文件中 protected void ...
- js中准确判断数据类型的方法
一 通用的typeof 方法 typeof ture 输出 Boolean typeof 123 输出 number ..... 但是 typeof 无法判断 nu ...
- Fire! 又是图 bfs
Joe works in a maze. Unfortunately, portions of the maze havecaught on re, and the owner of the ma ...
- 各大OJ题目分类
http://www.pythontip.com/acm/problemCategory
- mysql 常用linux命令
★ 数据导出命令 D:\Program Files\MySQL\MySQL Server 5.6.39\bin 导出: mysqldump -u root -p cela_sub > D:/d ...