1034. Head of a Gang (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

  1. PAT1034;Head of a Gang

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

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

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

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

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

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

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

  8. 1034. Head of a Gang (30)

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

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

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

随机推荐

  1. linux日常管理-vmstat命令

    系统负载用w查看.是什么原因造成了系统负载.查看系统负载状态 命令:vmstat vmstat就查看一次 vmstat 1 每秒钟更新一次.按ctrl+c取消. vmstat 1 5 每秒钟更新一次, ...

  2. python的gzip库使用方法

    解压gzip文件示例: import gzip f = gzip.open('file.txt.gz', 'rb') file_content = f.read() f.close() 创建gzip文 ...

  3. 卡特兰数&错排&一个一直记不住的公式

    卡特兰数 公式:f(x)=f(2)*f(x-1)+f(3)*f(x-2)+......+f(x-1)*f(2) #include<iostream>#include<cstdlib& ...

  4. Entity Framework Code-First(10):Fluent API

    Fluent API in Code-First: We have seen different DataAnnotations attributes in the previous sections ...

  5. 在虚拟机环境(CentOS7系统)下将kubernetes中部署服务成功,但在虚拟机外部无法访问到服务

    在CentOS7环境下,kubernetes单机版环境,成功部署一个服务,在虚拟机中访问服务没问题,下面这样: curl http://172.27.73.26:8888/eureka-server/ ...

  6. C#----接口与多继承

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 接口 { ...

  7. 【关于msyql5.6创建存储过程的一些记录】

    -- 秒杀执行存储过程DELETE $$ -- console的结束符号由;转换成 $$-- in输入参数:out:输出参数-- ROW_COUNT():返回上条dml影响的条数: 小于0:sql语句 ...

  8. Equals 和 == 的区别--转

    在比较Equals 和 ==的区别前.我们先来了解下相关的知识 C#数据类型 1.值类型 值类型有: 值类型包括:简单类型.结构类型.枚举类型. byte(1).sbyte(1).short(2).u ...

  9. 【转】List<T>和ILIst<T>的区别

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List ...

  10. 洛谷P3819 松江1843路

    P3819 松江1843路 题目描述 涞坊路是一条长L米的道路,道路上的坐标范围从0到L,路上有N座房子,第i座房子建在坐标为x[i]的地方,其中住了r[i]人. 松江1843路公交车要在这条路上建一 ...