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 ...
随机推荐
- Mybatis+Mysql 返回主键的值
需求:使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值. 方法:在mapper中指定keyProperty属性,示例如下: <insert id="i ...
- ios判断是否为iphone6或iphone6plus代码
转自:http://blog.csdn.net/lvxiangan/article/details/45288505 #define IS_IPAD (UI_USER_INTERFACE_IDIOM( ...
- C# 利用委托和事件 传入一个参数进行进行计算并返回结果
一.委托定义 1: public class TestData 2: { 3: //定义委托 4: public delegate void Get_TestDataEventHandler(Get_ ...
- delphi 线程教学第二节:在线程时空中操作界面(UI)
第二节:在线程时空中操作界面(UI) 1.为什么要用 TThread ? TThread 基于操作系统的线程函数封装,隐藏了诸多繁琐的细节. 适合于大部分情况多线程任务的实现.这个理由足够了吧 ...
- Java数组操作的10大方法
转载自码农网 译文链接:http://www.codeceo.com/article/10-java-array-method.html 英文原文:Top 10 Methods for Java Ar ...
- IE和W3c盒模型
盒子模型是css中一个重要的概念,理解了盒子模型才能更好的排版.其实盒子模型有两种,分别是 ie 盒子模型和标准 w3c 盒子模型.他们对盒子模型的解释各不相同,先来看看我们熟知的标准盒子模型: 从上 ...
- Unusual Sequences
题意: 求解合为 y 的总体 gcd 为 x 的正整数非空序列个数. 解法: 特判一下后,原问题等价于合为 s = y/x 的整体gcd为1的正整数序列个数. 1.$ans = \sum_{\sum{ ...
- mongodb切换到admin
127.0.0.1:后面是端口,/admin是切换到管理员权限 cd C:\Program Files\MongoDB\Server\3.0\bin mongo.exe 127.0.0.1:27017 ...
- 杭电acm 1022题
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- PHP获取原生POST数据
To get the Raw Post Data: <?php $postdata = file_get_contents("php://input"); ?> 参考官 ...