1034. Head of a Gang (30)
分析:
考察并查集,注意中间合并时的时间的合并和人数的合并。
#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)的更多相关文章
- pat 甲级 1034. Head of a Gang (30)
1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...
- 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 ...
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- PAT甲题题解-1034. Head of a Gang (30)-并查集
给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...
- PAT (Advanced Level) 1034. Head of a Gang (30)
简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
随机推荐
- EditText 自动格式化电话电话号码
需要格式化的格式为:xxx xxxx xxxx 有两种方式:1.为监听当前输入的长度,当长度为第四位,九位的时候,在原内容上追加空格.(from stackOverFlow)2.每次输入后,格式化当前 ...
- linux 安装软件程序
1.用aptitude管理软件包 查看已安装的/未安装的等软件包 无法通过aptitude看到一个细节是所有跟某个特定软件包关联的所有文件的列表.利用dpkg命令能看到这个列表. dpkg -L pa ...
- NBU AIX ORACLE10G RAC恢复到AIX单实例(表空间恢复)
ln -s /usr/openv/netbackup/bin/libobk.a64 /oraclev3/product/10.2.0/lib/libobk.a不建软连接会报如下错误: using ta ...
- Codeforces Round #228 (Div. 1) B
B. Fox and Minimal path time limit per test 1 second memory limit per test 256 megabytes input stand ...
- xml 中转意字符&\/使用方法
所有 XML 元素都须有关闭标签 在 HTML,经常会看到没有关闭标签的元素: <p>This is a paragraph <p>This is another paragr ...
- 网站搭建 so easy
服务器(国际购买):http://www.gigsgigscloud.com/ 域名(阿里云): 解析到服务器 服务器需要安装 1.putty 2.CuteFTP(自己感觉这个靠谱点) / ...
- web form 控件
webform的简单控件1.Label:文本显示,编译后是span属性:颜色,样式,边线宽度2.Literal:纯文本显示,编译后是text 3.TextBox:文本框TextMode-密码框.文本域 ...
- C语言中的union
1.union中可以定义多个成员,union的大小由最大的成员的大小决定. 2.union成员共享同一块大小的内存,一次只能使用其中的一个成员,与struct形成鲜明对比. 3.对某一个成员赋值,会覆 ...
- avalon2学习教程12数据验证
avalon2砍掉了不少功能(如ms-include,ms-data),腾出空间加了其他更有用的功能.数据验证就是其中之一.现在avalon2内置的验证指令是参考之前的oniui验证框架与jquery ...
- JavaScript(复习总结)
一.三个常用对话框 1.alert(""):警告对话框,作用是弹出一个警告对话框(最常用) 2.confirm(""):确定对话框,弹出一个带确定和取消按钮的对 ...