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 ...
随机推荐
- PHP和js判断访问设备是否是微信浏览器实例
PHP和js判断访问设备是否是微信浏览器实例,代码非常精简,适合新手学习. js判断是否是微信浏览器: 1 function is_weixin() { 2 var ua = window.navig ...
- 【规律】Growing Rectangular Spiral
Growing Rectangular Spiral 题目描述 A growing rectangular spiral is a connected sequence of straightline ...
- ReLeQ:一种自动强化学习的神经网络深度量化方法
ReLeQ:一种自动强化学习的神经网络深度量化方法 ReLeQ:一种自动强化学习的神经网络深度量化方法ReLeQ: An Automatic Reinforcement Learning Ap ...
- hdu 2767 强连通缩点处理加边问题
#include <cstring> #include <cstdlib> #include <cstdio> 缩点的好处就是可以将乱七八糟的有向图 转化为无环的有 ...
- 搞懂String、StringBuffer、StringBuilder的区别
String.StringBuffer.StringBuilder有什么区别呢? 1.String: 首先String是不可变的这是家喻户晓的,它的底层是用一个final修饰的char数组来保存数据的 ...
- 搭建nginx环境
1.安装nginx 下载地址:http://nginx.org/en/download.html 博主选择的是nginx1.8.1,点击下载 下载完成后是一个压缩包, 解压后双击nginx.exe 这 ...
- kali linux 安装 qq (deepin-wine)
添加deeepin-wine 依赖 /etc/apt/sources.list: # Generated by deepin-installer deb http://mirrors.aliyun.c ...
- CentOS如何安装MySQL8.0、创建用户并授权的详细步骤
# 安装相关软件 yum install -y gcc gcc-c++ openssl openssl-devel ncurses ncurses-devel make cmake # 获取MySQL ...
- HashMap闭环(死循环)的详细原因(转)
为何出现死循环简要说明 HashMap是非线程安全的,在并发场景中如果不保持足够的同步,就有可能在执行HashMap.get时进入死循环,将CPU的消耗到100%. HashMap采用链表解决Hash ...
- JAVA语言程序设计课后习题----第六单元解析(仅供参考)
1 本题就是基本函数的用法 import java.util.Scanner; public class Poone { public static void main(String[] args) ...