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 ...
随机推荐
- QT QcustomPlot的使用(二)
在QcustomPlot中,给横纵坐标添加箭头的方法 //在末尾添加箭头 customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeA ...
- python学习-6 猜拳小游戏
import random # 调用随机数模块 pc = random.randint(1,3) # 产生1-3的随机数 print("来玩个猜拳游戏吧!") a = '石头' b ...
- WPF DataGrid控件中某一列根据另一个文本列的值显示相应的模板控件
之前做项目的时候需要实现这样一个功能.WPF DataGrid有两列,一列为"更新状态”列,一列为"值"列,如果"更新状态"列的值为“固定值更新”,则 ...
- TCP协议探究(三):RTT、滑动窗口和阻塞处理
1 RTT算法 1.1 概述 上一节说了重传机制需要设置一个重传超时值(RTO,Retransmission TimeOut),RTO设长了,重发太慢:设短了,可能导致包没有丢,就重发了,可能导致雪崩 ...
- 设计模式 -- MVC
MVC 在Web中应用是常见的了,成为基础应用模式. 不好的用法是把业务写在C 中,M只是失血模型. 应该要重M 轻C,业务写在M中,但是这样有问题了.View 会引用Model,那么View会看到M ...
- ES6的拓展
1.String的拓展 ① str.includes("abc"); ② str.startWith("abc"); ③ str.endWith("a ...
- 【Git】五、远程仓库
前面4节将的都是本地的git操作,这节开始讲合并到本地分支后,如何与远程仓库做交互 -------------------------------- 提要 //生成本地ssh密钥 $ ssh-keyg ...
- 交换机配置-----monitor session
目录 交换机配置-----monitor 1.前言 2.monitor session的作用 3.配置命令 4.使用 交换机配置-----monitor 1.前言 本文章适用于Dell Network ...
- com.android.ddmlib.adbcommandrejectedexception:未经授权的设备。
出现这种问题的原因是adb被杀死了,根据网上的说法在platform-tools下双击adb.exe 也启动不了. 在命令提示符中执行 adb kill-server adb start-ser ...
- Flutter——Text组件(文字组件)
名称 功能 textAlign 文本对齐方式(center 居中,left 左 对齐,right 右对齐,justfy 两端对齐) textDirection 文本方向(ltr 从左至右,rtl 从右 ...