分析:

  考察并查集,注意中间合并时的时间的合并和人数的合并。

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cctype>
#include <map> using namespace std; const int Max_Int = 0x7fffffff;
const int Max_required = ; struct Node //记录gang的信息
{
int total_person; //gang中总人数
int time_length; //gang中总时间
int tree; //并查集用处
}gang[]; struct Name
{
char ch[];
}; map<string, int> Map_Name2int; //名字转下标之用
map<int, string> Map_int2Name;
int _index; int perp[]; //记录每个人花的时间 void Init()
{
Map_Name2int.clear();
_index = ;
for (int i = ; i <= ; i++)
{
gang[i].total_person = ;
gang[i].tree = i;
gang[i].time_length = ;
perp[i] = ;
}
} int string2index(string name)
{
map<string, int>::iterator iter = Map_Name2int.find(name);
if (iter != Map_Name2int.end())
return iter->second;
Map_Name2int.insert(make_pair(name, _index));
Map_int2Name.insert(make_pair(_index, name));
_index++; return _index - ;
} int get_root(int r)
{
while (r != gang[r].tree)
r = gang[r].tree;
return r;
} //-------------------------------------------------
void merge(int fir, int sec, int time) //合并操作。
{
fir = get_root(fir);
sec = get_root(sec); if (fir != sec) //如果两个拥有不同的根
{
gang[sec].tree = fir;
gang[fir].time_length += time + gang[sec].time_length;
gang[fir].total_person += gang[sec].total_person;
}
else
gang[fir].time_length += time;
//cout << gang[fir].total_person << " " << gang[fir].time_length << endl;
} struct Gang //以root为首的群的head,存储答案
{
int head;
int root;
string name;
}gang_head[];
int total_gang; void find_head(int k)
{
total_gang = ;
for (int i = ; i < _index; i++)
{
int r = get_root(i); //cout << r << " " << Map_int_Name.find(i)->second << endl; if (gang[r].total_person > && gang[r].time_length > k)
{
bool flag = ;
for (int j = ; j < total_gang; j++)
{
if (gang_head[j].root == r)
{
flag = ;
if (perp[gang_head[j].head] < perp[i])
gang_head[j].head = i;
break;
}
} if (flag == )
{
gang_head[total_gang].head = i;
gang_head[total_gang].root = r;
total_gang++;
}
}
}
} int cmp(const Gang &a, const Gang &b)
{
return a.name < b.name;
} void print_info()
{
for (int i = ; i < total_gang; i++)
gang_head[i].name = Map_int2Name.find(gang_head[i].head)->second; sort(gang_head, gang_head + total_gang, cmp); printf("%d\n", total_gang);
for (int i = ; i < total_gang; i++)
{
cout << gang_head[i].name << " " << gang[gang_head[i].root].total_person << endl;
} } int main()
{
int n, k; while (scanf("%d%d", &n, &k) != EOF)
{ Init(); //初始化 string name1, name2;
int time;
while (n--)
{
cin >> name1 >> name2 >> time;
int fir = string2index(name1);
int sec = string2index(name2); perp[fir] += time;
perp[sec] += time;
merge(fir, sec, time);
} find_head(k); print_info();
}
return ;
}

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 (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...

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

  5. 1034 Head of a Gang (30)(30 分)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

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

  7. 1034 Head of a Gang (30分)(dfs 利用map)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  8. PAT甲题题解-1034. Head of a Gang (30)-并查集

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

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

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

随机推荐

  1. Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)

    今天遇到一个项目中用到Android 6.0的系统,但是有个文件是从Assert目录下写到SDCard中,但是AndroidMinifest.xml中已经加了文件的读权限和写权限,异常日志是在创建文件 ...

  2. EF 示例

    EF有三种数据库访问方式,这里只介绍Code First. 1.DB First,类似Linq to sql中拖拽一个DB到方案中 2.Model First,没试过,不能自动生成数据库只能生成表 3 ...

  3. 云存储性能测试工具--COSBench安装

    COSBench安装 Cosbench是Intel的开源云存储性能测试软件,COSBench目前已经广泛使用与云存储测试,并作为云存储的基准测试工具使用 1 环境 1.1 操作系统 COSBench可 ...

  4. zedboard如何从PL端控制DDR读写(七)

    前面已经详细介绍了从PL端如何用AXI总线去控制DDR的读写,并且从AXI_BRESP的返回值来看,我们也是成功写入了的.但是没有通过别的方式来验证写入的数据的话,总是感觉差了点什么. 今天试了一把从 ...

  5. WinServer2008r2 机器时间格式修改

    windows2008 这么高级的系统不可能改个系统的日期时间显示格式还要进注册表啊.于是有baidu,google了下终于发现了,原来还有不需要注册表的更简便方法.windows2008默认时间格式 ...

  6. DEBUG STACK TRACE for PoolBackedDataSource.close().

    我使用generator生成的代码,xml里面的内容没有覆盖重写,而是在下面直接追加,所以需要把以前的全部删除,然后在用generator生成.如果这个不能解决问题,就查查别人的问题.

  7. CSS3基础01

    一.选择器: 分为关系选择器 ,属性选择器 ,伪类选择器 1.1关系选择器  后代选择器   ul li  选择所有的后代元素 子代选择器   ul > li 选择ul的儿子 紧邻选择器  .b ...

  8. ajax请求后根据条件进行页面跳转

    $.ajx({ url: "@Url.Action("DetectCorporationCompetencyCreated", "DataBase") ...

  9. 《C++编程规范:101条规则、准则与最佳实践》学习笔记

    转载:http://dsqiu.iteye.com/blog/1688217 组织和策略问题 0. 不要为小事斤斤计较.(或者说是:知道什么东西不需要标准化) 无需在多个项目或者整个公司范围内强制实施 ...

  10. C++/MFC如何启动另一个应用程序并获取其进程 ID

    ShellExecute( hWnd: HWND; {指定父窗口句柄} Operation: PChar; {指定动作, 譬如: open.runas.print.edit.explore.find[ ...