浙大PAT 1034 Head of aGang
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 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
相当于一个简单的联系网,然后找出几个独立的集合,并在满足条件的集合中找到权重最大的那个节点输出即可,dfs求解,其中直接使用stl容器为数据结构。(参考了别人的思路)
#include <cstdio>
#include <string>
#include <map>
#include <vector>
#include <iostream>using namespace std;map<string,vector<string> >adjlist;
map<string,int> weight;
map<string,int> visit;
map<string,int> res;int cnt,total;
string head;void dfs(string ss){++cnt;total += weight[ss];visit[ss] = 1;if(weight[head]<weight[ss])head = ss;vector<string>::iterator it = adjlist[ss].begin();for(;it!=adjlist[ss].end();++it){if(visit[*it]==0){dfs(*it);}}}int main(){int n,k,wei,i;string sa,sb;scanf("%d %d",&n,&k);for(i=0;i<n;++i){cin>>sa>>sb>>wei;adjlist[sa].push_back(sb);adjlist[sb].push_back(sa);weight[sa] += wei;weight[sb] += wei;visit[sa] = 0;visit[sb] = 0;}map<string,int>::iterator it = visit.begin();for(;it!=visit.end();++it){if(it->second==0){head = it->first;total = 0;cnt = 0;dfs(it->first);if(cnt>2 && total/2>k)res[head] = cnt;}}printf("%d\n",res.size());it = res.begin();for(;it!=res.end();++it){cout<<it->first<<" "<<it->second<<endl;}return 0;
}
浙大PAT 1034 Head of aGang相关推荐
- 浙大pat | 浙大pat 牛客网PAT顶级(Top Level)练习题 1001
1001 LUCKY STRING 1872 8254 22% 题目描述 A string s is LUCKY if and only if the number of differentchara ...
- 浙大PAT甲级1040
浙大PAT甲级1040 原题 问题分析 原题 1040 Longest Symmetric String (25 分) Given a string, you are supposed to outp ...
- PAT:1034 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 phon ...
- 浙大PAT甲级1019. General Palindromic Number (20)
1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 浙大 PAT b1040
#include <stdio.h> #include <string.h> int main() {char str[100001];gets(str);int numT = ...
- 浙大PAT 1003题 1003. Emergency
本题用Dfs搜索或者Dijkstra算法都可以,当然也有其它的方法.这题感觉是pat中常见的类型,非常重要. Dfs搜索代码: #include<stdio.h> int road[510 ...
- pat 1034. Head of a Gang (30)
题意:求边权大于k并且点数大于2的联通块,输出其中的重要点,重要点:联通块中与该点相连的边权最大 丢set里乱搞 #include<bits/stdc++.h> using namespa ...
- 浙大PAT甲级1027. Colors in Mars (20)
1027. Colors in Mars (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue People ...
- 浙大PAT甲级1006
1006. Sign In and Sign Out (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
最新文章
- 7_2判断两个单链表是否相交,若相交,求出第一个交点
- BannerDemo【图片轮播图控件】
- The Way to TiDB 3.0 and Beyond (下篇)
- php获取浏览器cookies,简单实现创建以及读取浏览器中cookie的几种方法
- 节能原理 (能量平衡)
- 78oa mysql_78oa系统版本升级方法
- Linux加密框架 crypto算法模板 以及CBC算法模板实例
- 云时代 揭开性能监测战略的隐秘优势
- PyTorch实现自由的数据读取
- pycharm添加python_Pycharm 复制代码出现空格的解决方式
- C# Word控件 dsoframer、office viewer、pageoffice、setparent
- yii php 弹窗,Yii框架弹出框功能示例
- 大牛直播十万个为什么
- nginx的cgi模块
- Linux 下串口编程(C++ 程序设计)
- sqlplus: error while loading shared libraries: libclntsh.so.11.1
- 李开复哥大毕业演讲:工程师的AI银河系漫游指南
- 【OpenGL ES】二维图形绘制
- [Vijos1763]Wormhole (贪心/模拟?)
- TOSHIBA,TC358775XBG,MIPI DSI转LVDS,视频解码器,RK3399点LVDS屏必备