题目地址: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)的更多相关文章

  1. pat 甲级 1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

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

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

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

  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 (30)-并查集

    给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...

  7. PAT (Advanced Level) 1034. Head of a Gang (30)

    简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  8. 【PAT甲级】1034 Head of a Gang (30 分)

    题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...

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

随机推荐

  1. Oracle DB优化

    http://www.jb51.net/article/77876.htm http://www.jb51.net/article/56881.htm http://danni505.blog.51c ...

  2. UVA 1160 X-Plosives

    题意是一次装入物品,物品由两种元素组成,当遇到即将装入的物品与已经装入的物品形成k个物品,k种元素,跳过该物品的装入.可以将每种元素看成顶点,物品看成一条边.这样问题就转化为利用并查集求环的情况. 算 ...

  3. Java集合类之栈Stack

    package com.test; import java.util.*; public class Demo7_3 { public static void main(String[] args) ...

  4. ActionBar官方教程(4)给ActionBar添加操作项及它们的事件处理

    Adding Action Items The action bar provides users access to the most important action items relating ...

  5. XSD标准架构-----<xsd:element> 元素详解

    声明一个元素.     <element abstract = Boolean : false block = (#all | List of (extension | restriction ...

  6. 【HDOJ】1857 Word Puzzle

    trie树.以puzzle做trie树内存不够,从puzzle中直接找串应该会TLE.其实可以将查询组成trie树,离线做.扫描puzzle时注意仅三个方向即可. /* 1857 */ #includ ...

  7. BGP详解

    相信各位站长在托管服务器或者选择虚拟主机的时候,提供商都会说他们的机房是双线机房,保证你的站点访问速度,那么这里所谓的双线机房到底是何意思,它又为何能提升站点的访问速度呢? 一遍小型机房的所谓双线路其 ...

  8. 转自 z55250825 的几篇关于FFT的博文(一)

        关于FFT,咱们都会迫不及待地 @  .....(大雾)(貌似被玩坏了...)    .....0.0学习FFT前先orz FFT君.         首先先是更详细的链接(手写版题解点赞0v ...

  9. iOS8新增加的frameworks, 在目前基于7以上开发的情况下, 使用下列sdk要注意设置成optional

    Added frameworks: AVKitCloudKitCoreAudioKitCoreAuthenticationHealthKitHomeKitLocalAuthenticationMeta ...

  10. 教你如何通过ICCID找回丢失的的iPhone

    22日晚买了FACETIME,在某宝上买的.价格不贵,可以查到偷手机的人注册FT的号码,还可以查询手机被刷机和被维修的日期(这个很关键) 27日手机被刷机,遂买了某宝查询ICCID的服务,找到一串IC ...