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 ...
随机推荐
- Go中使用动态库C/C++库
转自:http://studygolang.com/articles/1441 最近需要做一些在go中使用动态C++库的工作,经常碰到找不到动态库路径这种情况,所以就花点时间,专门做一下实验来了解Go ...
- jQuery对象和DOM对象的互换
Dom 对象:指的是普通的 JavaScript 对象 jQuery对象:是包装 Dom 对象后产生的对象. 一:JQuery 对象和 Dom 对象 在使用 JQuery 过程中,我们一般(也是绝大多 ...
- 【255】◀▶IEW-Unit20
Unit 20 Environment: Tourism I.定语从句及分词在雅思写作中的运用 定语从句: 1. 先行词 2. 关系词:关系代词.关系副词 3. 非限制性定语从句 4. 分词和定语从句 ...
- assert.ifError()
assert.ifError(value) 如果 value 为真,则抛出 value. 可用于测试回调函数的 error 参数(通俗解释ifError方法断定某个表达式是否false,如果该表达式对 ...
- CodeForces 492D Vanya and Computer Game (思维题)
D. Vanya and Computer Game time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- c/c++进制转换练习
1 下列数最大的是( ).括号内为数字,括号外为进制.(360集团) (10010101)2 (227)8------>10010111 (96)16------>10010110 (14 ...
- Java Synchronized的原理
我们先通过反编译下面的代码来看看Synchronized是如何实现对代码块进行同步的: public class SynchronizedDemo{ public void method(){ syn ...
- 使用IDEA编译netty4
本来计划一天一篇博文的,结果周末加班加的不亦乐乎,对整个项目重构添加国际化的功能,不停的使用native2ascii工具 使用IDEA编译原有的netty4要报错,我修改了下pom.xml,编译成功了 ...
- SqlParameter用法
if (id != null) { sql = @"update [User] set Username = @Username, Password = @Password, Type = ...
- 常见的web漏洞及其防范
原文地址:http://blog.csdn.net/u013777676/article/details/52124298 一.SQL注入漏洞 SQL注入攻击(SQL Injection),简称注入攻 ...