PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034
此题考查并查集的应用,要熟悉在合并的时候存储信息:
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cstddef>
using namespace std; struct Person
{
int personalTime;
int gangTime;//记录以当前person为根的集合的gang 的总的通话时间
vector<string> members;//记录以当前person为根的集合的gang members
string root;
Person()
{
personalTime=;
gangTime=;
root="-1";
}
}; map<string,Person> tree;//并查集
map<string,int> gangs;//符合条件的gang string findRoot(string index)
{
if(tree[index].root=="-1") return index;
else
{
string tmp=findRoot(tree[index].root);
tree[index].root=tmp;
return tmp;
}
} string findHead(string root)//找出当前gang中具有最大weight的为gang head
{
vector<string> members=tree[root].members;
string gangHead=root;
int maxPersonalTime=tree[root].personalTime;
for(vector<string>::iterator iter=members.begin();iter!=members.end();++iter)
{
if(tree[*iter].personalTime>maxPersonalTime)
{
gangHead=*iter;
maxPersonalTime=tree[*iter].personalTime;
}
}
return gangHead;
} int _tmain(int argc, _TCHAR* argv[])
{
int N,K;
cin>>N>>K;
int i;
string Name1,Name2,root1,root2;
int Time;
for(i=;i<N;++i)
{
cin>>Name1>>Name2>>Time;
if(tree[Name1].members.size()==)
{
tree[Name1].members.push_back(Name1);
}
if(tree[Name2].members.size()==)
{
tree[Name2].members.push_back(Name2);
}
tree[Name1].personalTime+=Time;
tree[Name2].personalTime+=Time;
root1=findRoot(Name1);
root2=findRoot(Name2);
if(root1!=root2)
{
tree[root1].root=root2;
tree[root2].gangTime+=Time;
tree[root2].gangTime+=tree[root1].gangTime;
tree[root2].members.insert(tree[root2].members.end(),tree[root1].members.begin(),tree[root1].members.end());
}
else
{
tree[root2].gangTime+=Time;
}
}
for(map<string,Person>::iterator iter=tree.begin();iter!=tree.end();++iter)
{
if(iter->second.root=="-1"&&iter->second.members.size()>&&iter->second.gangTime>K)
{
string head=findHead(iter->first);
gangs[head]=iter->second.members.size();
}
}
size_t size=gangs.size();
cout<<size<<endl;
if(==size)
{
return ;
}
for(map<string,int>::iterator iter=gangs.begin();iter!=gangs.end();++iter)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
return ;
}
PAT 1034. Head of a Gang (30)的更多相关文章
- 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[难][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 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 (30)-并查集
给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...
- PAT (Advanced Level) 1034. Head of a Gang (30)
简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- 【PAT甲级】1034 Head of a Gang (30 分)
题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...
- 1034. Head of a Gang (30) -string离散化 -map应用 -并查集
题目如下: One way that the police finds the head of a gang is to check people's phone calls. If there is ...
随机推荐
- ExtJS 获取浏览器宽度
JS中代码: Ext.onReady(function() { var width=window.screen.availWidth; var height=window.screen.availHe ...
- ANGULAR 2 FOR REACT DEVELOPERS
Now that Angular 2 is in beta, the time has come for us to drop everything and learn something new, ...
- 网页中插入Flvplayer视频播放器代码
http://blog.csdn.net/china_skag/article/details/7424019 原地址:http://yuweiqiang.blog.163.com/blog/stat ...
- linux下查看文件编码及修改编码
http://blog.csdn.net/jnbbwyth/article/details/6991425 查看文件编码在Linux中查看文件编码可以通过以下几种方式:1.在Vim中可以直接查看文件编 ...
- RxJava开发精要7 – Schedulers-解决Android主线程问题
原文出自<RxJava Essentials> 原文作者 : Ivan Morgillo 译文出自 : 开发技术前线 www.devtf.cn 转载声明: 本译文已授权开发者头条享有独家转 ...
- 关于linq
其实从08年的时候,我就已经知道了linq,开始的时候也并没有注意,我说过很多次,我不是一个有心人,只是在新建立一个工程的时候,程序会自动引入linq这个玩意,怀着好奇的心去找了点资料,有的时候,看一 ...
- mysql select count(filed) 问题(where条件没有数据匹配的话也有数据返回)。
问题: SELECT count(*),user_id FROM tb_rp_logintrace WHERE id=-1 返回结果: count(*), user_id 0 ...
- Android进阶篇-时间滑动控件
仿Iphone时间选择滑动控件: WheelView.java: /** * @author Administrator * * 时间滑动滚轮 */ public class WheelView ex ...
- c#浅谈反射内存的处理
这段时间由于公司的项目的要求,我利用c#的反射的机制做了一个客户端框架.客户端里的所有的模块都是以一定形式进行提供,例如:FORM,UserControl. 在做的过程中很简单与愉快.具体的过程如下: ...
- JAVA与.NET的相互调用——通过Web服务实现相互调用
JAVA与.NET是现今世界竞争激烈的两大开发媒体,两者语言有很多相似的地方.而在很多大型的开发项目里面,往往需要使用两种语言进行集成开发.而很多的开发人员都会偏向于其中一种语言,在使用集成开发的时候 ...