分析:

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

 #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. NeHe OpenGL教程 第四十六课:全屏反走样

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  2. SG函数闲扯(转)

    http://ydcydcy1.blog.163.com/blog/static/216089040201342412717440/ 没来得及看.

  3. 41. Unique Binary Search Trees && Unique Binary Search Trees II

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  4. EXT学习之——Ext两个js之间的传参

    A  的js访问 B的js,并将A选择的guid的行传到  B的 js进行处理事项 A 的js 的写法var receiverFrom = new xxx.xxx子js方法体名 ({ parentCm ...

  5. PHP的命名空间

    简介: 防止名称冲突. 原理: 类似文件目录/usr/local这样的. 用法: namespace:定义命名空间: use:取别名: 代码示例:file 1.php <?php namespa ...

  6. oracle创建包后执行报错:object omgmig.test_package is invalid.

    今天学习了一下oracle的包的写法,然后碰到这么个问题.包声明和包主体都正确,但是就是执行报错:object omgmig.test_package is invalid. 这是会报错的sql,看起 ...

  7. kindeditor-4.1.7应用

    页面: <script type="text/javascript" src="include/kindeditor/kindeditor.js"> ...

  8. C++学习基础七——深复制与浅复制

    一.深复制与浅复制基本知识 深复制和浅复制,又称为深拷贝和浅拷贝. 深复制和浅复制的区别如下图1所示: 图1 图1表示的是,定义一个类CDemo,包含int a和char *str两个成员变量, 当深 ...

  9. dll 导出函数名的那些事

    dll 导出函数名的那些事 关键字: VC++  DLL  导出函数 经常使用VC6的Dependency或者是Depends工具查看DLL导出函数的名字,会发现有DLL导出函数的名字有时大不相同,导 ...

  10. android自定义圆形图片和遇到的问题

    画圆遇到的问题:图片单位不一样,导致图片只能显示出圆的一部分:看代码: public class MyCircleIamge extends ImageView { private Context c ...