pat1034. Head of a Gang (30)
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 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
#include<cstdio>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
map<string,vector<string> > edge;
map<string,int> pv;
map<string,bool> vis;
map<string,int> output;
void DFS(string s,string &maxper,int &pernum,int &total){
vis[s]=true;
pernum++;
total+=pv[s];
if(pv[s]>pv[maxper]){
maxper=s;
}
vector<string>::iterator it;
for(it=edge[s].begin();it!=edge[s].end();it++){
if(!vis[*it]){
DFS(*it,maxper,pernum,total);
}
}
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,k;
scanf("%d %d",&n,&k);
int i,v;
string name1,name2;
for(i=;i<n;i++){
cin>>name1>>name2;
scanf("%d",&v);
edge[name1].push_back(name2);
pv[name1]+=v;
vis[name1]=false;
edge[name2].push_back(name1);
pv[name2]+=v;
vis[name2]=false;
}
map<string,vector<string> >::iterator it;
int count=;
string maxper;
int pernum,total;
for(it=edge.begin();it!=edge.end();it++){
if(!vis[it->first]){
maxper=it->first;
pernum=;
total=;
DFS(it->first,maxper,pernum,total);
if(pernum>&&total*1.0/>k){
output[maxper]=pernum;
count++;
}
}
}
map<string,int>::iterator itt;
if(count){
printf("%d\n",count);
for(itt=output.begin();itt!=output.end();itt++){
cout<<itt->first;
printf(" %d\n",itt->second);
}
}
else{
printf("0\n");
}
return ;
}
pat1034. Head of a Gang (30)的更多相关文章
- PAT1034;Head of a Gang
1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...
- pat 甲级 1034. Head of a Gang (30)
1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...
- 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) -string离散化 -map应用 -并查集
题目如下: One way that the police finds the head of a gang is to check people's phone calls. If there is ...
- 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 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分)(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 ...
- 1034. Head of a Gang (30)
分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
随机推荐
- makefile 基础知识
$@ 目标文件名 $< 第一个依赖文件名 $^ 规则所有依赖文件列表 如果不想让执行语句被打印出来,就在语句前面加上@符号 模式规则 %.o:%.c 后缀规则 .c.o 生成单进程的Mak ...
- k8s 基础 k8s架构和组件
k8s 的总架构图
- 用C语言实现一个公用库函数void * memmove(void *dest,const void *src,size_t n)
用C语言实现一个公用库函数void * memmove(void *dest,const void *src,size_t n). 该函数的功能是拷贝src所指的内存内容前n个字节到dest所指的地址 ...
- 6.6 chmod的使用
从公司拷贝了白天整理的笔记,拿回家整理,结果发现有锁,无法对其解压.解决方案如上: ll 命令,查看其权限. sudo chmod 777 Picture.tar-1修改权限. 然后,可以正常打开Pc ...
- 连接mysql时报:message from server: "Host '192.168.76.89' is not allowed to connect to this MySQL server 处理方案
1.先用localhost方式连接到MySQL数据库,然后使用MySQL自带的数据库mysql; use mysql: 2.执行:select host from user where user = ...
- InnoDB信息说明
InnoDB是MySQL数据库发展至今一款至关重要的数据库存储引擎,其不仅支持事务特性,并且具有丰富的统计信息,便于数据库管理人员了解最近InnoDB存储引擎的运行状态. 早期版本的InnoDB存储引 ...
- cf780E(dfs)
题目链接: http://codeforces.com/problemset/problem/780/E 题意: 给出一个 n 个点 m 条边的图, 有 k 个人, 初始位置可以为任意位置, 每个人最 ...
- Kotlin VS Java:基本语法差异
Kotlin比Java更年轻,但它是一个非常有前途的编程语言,它的社区不断增长. 每个人都在谈论它,并说它很酷. 但为什么这么特别? 我们准备了一系列文章,分享我们在Kotlin开发Android应用 ...
- CTS/APIO后文化课游记
根据ghj1222的尿性,干什么事都要写一个游记划水记啥的...然后就写嘛... 现在是5.30微机课,先开个坑,学校6.5放假,我将于6.5后开始更新本文 APIO回来后发生的事真的特别多...有的 ...
- 开源库SRT编译指南
SRT(Secure,Reliable,Transport)是Haivision公司开发的一套开源媒体传输协议,用于在不稳定的网络环境下,优化媒体数据的传输性能. SRT的码流加密基于开源库open ...