pat 甲级 1034 ( Head of a Gang )
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
#include<bits/stdc++.h>
using namespace std;
int n;
int par[2005];int Rank[2005];
void init()
{
for(int i=0;i<2005;i++)par[i]=i,Rank[i]=0;
}
int find(int x)
{
if(par[x]==x)return x;
else return par[x]=find(par[x]);
}
bool same(int x,int y)
{
return find(x)==find(y);
}
void unite(int x,int y)
{
if(same(x,y))return;
x=find(x);y=find(y);
if(Rank[x]<Rank[y])par[x]=y;
else {
par[y]=x;
if(Rank[y]==Rank[x])Rank[x]++;
}
}
int m;int k;
int cnt=0;
map<string,int>mp;
string name[2005];
int e[2005][2005];
int tot[2005];
vector<string>gg;
map<string,int>mp2;
int main()
{
//freopen("in.txt","r",stdin);
init();
cin>>m>>k;
memset(e,0,sizeof(e));
string a,b;int c;
while(m--)
{
cin>>a>>b>>c;
if(mp.find(a)==mp.end()){name[cnt]=a;mp[a]=cnt++;}
if(mp.find(b)==mp.end()){name[cnt]=b;mp[b]=cnt++;}
int x=mp[a];int y=mp[b];
if(e[x][y]==-1)e[x][y]=e[y][x]=c;
else e[x][y]+=c,e[y][x]+=c;
tot[x]+=c;tot[y]+=c;
if(!same(x,y))
unite(x,y);
}
set<int>st;st.clear();
for(int i=0;i<cnt;i++)st.insert(find(i));
int ans=0;
set<int>::iterator it=st.begin();
while(it!=st.end())
{
vector<int>vec;vec.clear();
int p=*it;
for(int i=0;i<cnt;i++)
{
if(find(i)==p)vec.push_back(i);
}
int sum=0;int id=0;
for(int i=0;i<vec.size();i++)
{
for(int j=i+1;j<vec.size();j++)
{
int x=vec[i];int y=vec[j];
sum+=e[x][y];
}
}
if(sum>k&&vec.size()>2){
for(int i=0;i<vec.size();i++)
{
if(tot[vec[i]]>tot[vec[id]])id=i;
}
gg.push_back(name[vec[id]]);
mp2[name[vec[id]]]=vec.size();
ans++;
}
it++;
}
cout<<ans<<endl;
sort(gg.begin(),gg.end());
for(int i=0;i<ans;i++)
{
cout<<gg[i]<<" "<<mp2[gg[i]]<<endl;
}
return 0;
}
pat 甲级 1034 ( Head of a Gang )的更多相关文章
- PAT甲级1034. Head of a Gang
PAT甲级1034. Head of a Gang 题意: 警方找到一个帮派的头的一种方式是检查人民的电话.如果A和B之间有电话,我们说A和B是相关的.关系的权重被定义为两人之间所有电话的总时间长度. ...
- 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 ...
- PAT甲级1034 Head of a Gang【bfs】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 题意: 给定n条记录(注意不是n个人的 ...
- 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 ...
- PAT 1034 Head of a Gang[难][dfs]
1034 Head of a Gang (30)(30 分) One way that the police finds the head of a gang is to check people's ...
- PAT 1034. Head of a Gang
1034. Head of a Gang (30) One way that the police finds the head of a gang is to check people's phon ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
随机推荐
- hdu 1029 求出现次数过半的数
题目传送门//res tp hdu 已知必定存在一个元素出现次数超过一半,考虑用栈 若当前元素等于栈顶元素,入栈,反之出栈,并将当前元素入栈 最终的栈顶元素即是所求 #include<iostr ...
- java web开发环境设置
Mapped Statements collection does not contain value for后面是什么类什么方法之类的问题: 除了"https://changbl.itey ...
- 迁移WordPress
一.迁移目的 WordPress是一款能让您建立出色网站.博客或应用的开源软件.官网:https://cn.wordpress.org/download/,最开始是将WordPress部署在本地虚拟机 ...
- redis键空间通知(keyspace notification)
一.需求 在redis中,设置好key和生存时间之后,希望key过期被删除时能够及时的发送一个通知告诉我key,以便我做后续的一些操作. 二.环境 系统:windows10 php:7.1 redis ...
- 路由器WAN口IP显示为10、100、172开头,网络被电信联通等运营商做了NAT转发
摘要:路由器WAN口IP显示为10.100.172开头,网络被电信联通等运营商做了NAT转发 ... 路由器WAN口IP显示为10.100.172开头的解决方法方法一:找电信(10000号)或者联通( ...
- 使用NPOI导出Excel文件
使用NPOI导出Excel文件,本实例使用了ASP.NET MVC. 1.使用NPOI导出Excel文件 实例:导出商品列表. 要求:1.通过NPOI导出导出商品列表信息: 2.使用Excel函数计算 ...
- ELinq学习一
ELinq安装:在Nuget控制台中输入:install-package ELinq一.ELinq与DLinq和EF的功能差异 二.数据库对照表 三.CRUD操作1.插入(Insert)(1)简单形式 ...
- 二、LinkList及其源码解析
1.链表介绍 链表是一种物理单元上非连续,非顺序的存储结构.链表由一系列的姐点组成,结点可以在运行时动态生成.每个结点包含两个部分,一个是存储数据元素的数据域,一个是存储下一个结点的指针域 双链表是链 ...
- nhandled rejection Error: EPERM: operation not permitted, open 'C:\Program Files\nodejs\node_cache npm ERR! cb() never called!
安装全局包时报错,之前已经遇到过,结果第二次又忘记解决方法,果然还是要记下来,好记性不如烂笔头哇 $ npm i electron -gUnhandled rejection Error: EPERM ...
- 如何上传HTML5应用到SAP云平台的Cloud Foundry环境下
先使用WebIDE创建一个HTML5应用.New->Project from Template: 从可选模板里选择SAPUI5 Application: 创建一个HTML5 Module,取名为 ...