题目地址: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. writeToFile 读写文件问题

    关于 writeToFile 读写文件:当字典中键值对以 Model(例如:studentModel)为值时发现 Dictionary 调用 writeToFile 方法无法生成 plist 文件,经 ...

  2. xrange和range区别

    range和xrange这两个函数基本都是在循环的时候使用的. >>> for x in range(10,21,1): ... print x ... 10 11 12 13 14 ...

  3. Ubuntu之网络配置

    一.配置大概分三类:通过配置文件配置.通过命令配置.通过图形化的网络连接菜单配置. 拨号无线等的没条件实验,不涉及. 主要文件:/etc/network/interfaces,这里是IP.网关.掩码等 ...

  4. [模拟]Codeforces509C Sums of Digits

    题目链接 题意:给n个数a[i], 要求b[i]每位数的和等于a[i], 并且b[i]要严格递增 求最小的b[i] b[0]最小一定是X9999...这样的形式 后面的b[i]位数一定大于等于前一个 ...

  5. HDOJ 4893 Wow! Such Sequence!

    题意是这样的,给定一个n个元素的数组,初始值为0,3种操作: 1 k d将第k个数增加d: 2 l r 询问区间l...r范围内数之和: 3 l r 表示将区间l...r内的数变成离他最近的斐波那契数 ...

  6. Android 圆角Button

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbEAAADrCAYAAADnsqiUAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAA

  7. IPv6 tutorial – Part 6: Site-local addresses and link-local addresses

    https://4sysops.com/archives/ipv6-tutorial-part-6-site-local-addresses-and-link-local-addresses/ In ...

  8. Subline Text默认设置文件Preferences.sublime-settings—Default详解

    Subline Text中,点击Preferences,选择Settings - Default 全部属性解析 // While you can edit this file, it's best t ...

  9. 定制属于自己的自动化安装的linux系统镜像

    使用软件和平台 1.基于平台:                  Vmware workstation 8.0 2.基于系统镜像:               rhel-server-5.8-i386 ...

  10. Master Nginx(4) - Nginx as a Reverse Proxy

    Introduction to reverse proxying the proxy module legacy servers with cookies the upstream module ke ...