分析:

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

 #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. 便捷的php操作mysql库MysqliDb

    github 地址:https://github.com/joshcam/PHP-MySQLi-Database-Class MysqliDb -- Simple MySQLi wrapper and ...

  2. Intel汇编语言程序设计学习笔记1

    第一章 汇编器链接器:汇编器将汇编语言翻译成机器语言,链接器将单个文件合并为可执行文件 intel 80X86系列处理器的汇编语言与VAX或者motorala 68x00等系统的汇编是否相同?不相同, ...

  3. js实现一套代码来控制所有的运动,图片的淡入淡出,winth,height的变宽

    介绍了那么多运动,這次一套代码实现所有运动 1.html代码和css代码,只是定义一个div <style> div{ width:200px; height:200px; margin: ...

  4. struts2 中 Actionsupport 的作用

    struts2 中 Actionsupport 的作用 Action 跟 Actionsupport 的区别     当我们在写action的时候,可以实现Action接口,也可以继承Actionsu ...

  5. oracle中的timestamp字段的值乱码问题修改

    我的解决方案: 直接新增一个系统变量: key值为:NLS_TIMESTAMP_FORMATvalue的值为:YYYY-MM-DD HH24:MI:SSFF6 其它解决方案: 在登录PLSQL之后,查 ...

  6. Corba概念(GIOP、IIOP、IOR、ORB、IDL)

    CORBA公用对象请求代理(调度)程序体系结构(Common Object Request Broker Architecture),缩写为 CORBA,是对象管理组织(Object Manageme ...

  7. iOS 调用拍照、选择本地相册、上传功能---未完善。

    1.新建viewController 拖入一个Button,添加点击事件,使用代理方法 <UIActionSheetDelegate,UIImagePickerControllerDelegat ...

  8. ERROR SparkUncaughtExceptionHandler: Uncaught exception in thread

    ERROR SparkUncaughtExceptionHandler: Uncaught exception in thread Thread[appclient-registration-retr ...

  9. 正确停止kafka的方法

    kill -15 pid 即: kill SIGNTERM pid 不要使用kill -9. kill -15会触发调用shutdownHook的run方法,从而可以执行关闭服务器的时候一些必要代码. ...

  10. codevs 1163 访问艺术馆

    1163 访问艺术馆  时间限制: 1 s  空间限制: 128000 KB       题目描述 Description 皮尔是一个出了名的盗画者,他经过数月的精心准备,打算到艺术馆盗画.艺术馆的结 ...