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

  1. PAT甲级1034. Head of a Gang

    PAT甲级1034. Head of a Gang 题意: 警方找到一个帮派的头的一种方式是检查人民的电话.如果A和B之间有电话,我们说A和B是相关的.关系的权重被定义为两人之间所有电话的总时间长度. ...

  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. PAT甲级1034 Head of a Gang【bfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 题意: 给定n条记录(注意不是n个人的 ...

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

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

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

  8. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

随机推荐

  1. C++ Primer练习题day2

    /* 1.7略 1.8 /* 指出不合法的语句: std::cout<<"/"; std::cout<<"*/ "; std::cout ...

  2. 第9章:Python自动化管理

    1.使用SSH协议访问远程服务器 SSH协议 OpenSSH协议 使用密钥登陆远程服务器 使用ssh-agent管理私钥 2.使用Polysh批量管理服务器 Polysh requires pytho ...

  3. 【第一季】CH08_FPGA_Button 按钮去抖动实验

    [第一季]CH08_FPGA_Button 按钮去抖动实验 按键的消抖,是指按键在闭合或松开的瞬间伴随着一连串的抖动,这样的抖动将直接影响设计系统的稳定性,降低响应灵敏度.因此,必须对抖动进行处理,即 ...

  4. 【第一季】CH06_FPGA设计Verilog基础(三)

    [第一季]CH06_FPGA设计Verilog基础(三) 一个完整的设计,除了好的功能描述代码,对于程序的仿真验证是必不可少的.学会如何去验证自己所写的程序,即如何调试自己的程序是一件非常重要的事情. ...

  5. Codeforces 1247F. Tree Factory

    传送门 正难则反,把链操作成树不好想,那么考虑一下如何把树变成链 每次操作相当于把一个兄弟变成儿子(我把你当兄弟你竟然想把我当儿子.jpg) 注意到每次操作最多只能使树的深度增加 $1$ 因为链的深度 ...

  6. MyBatis 源码篇-插件模块

    本章主要描述 MyBatis 插件模块的原理,从以下两点出发: MyBatis 是如何加载插件配置的? MyBatis 是如何实现用户使用自定义拦截器对 SQL 语句执行过程中的某一点进行拦截的? 示 ...

  7. 怎样理解ECMAScript 和 JavaScript的关系

    JavaScript可以分为三大部分: 1. 核心语法 2. DOM 3. BOM 而核心语法实际上就是指的ECMAScript, 而JS又是不断在发展的, 而这个发展实际上最主要的就是ECMAScr ...

  8. Django rest-framework框架-认证组件的简单实例

    第一版 : 自己写函数实现用户认证 #models from django.db import models #用户表 class UserInfo(models.Model): user_type_ ...

  9. Django数据查询中对字段进行排序

    Django数据查询中对字段进行排序   第一种方法:使用order_by进行排序 Articlelist = Article.objects.filter(**kwargs).order_by('n ...

  10. 使ffmpeg支持HDR10bit 环境为ubuntu16.04

    1. 编译X265,生成静态库, 安装到默认目录 修改CMakeLists.txt 使   HIGH_BIT_DEPTH  设置为ON cmake -G "Unix Makefiles&qu ...