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 a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between
the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you
are supposed to find the gangs and the heads.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N lines follow, each in the following format:
Name1 Name2 Time
where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.
Output Specification:
For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according
to the alphabetical order of the names of the heads.
Sample Input 1:
8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10
Sample Output 1:
2
AAA 3
GGG 3
Sample Input 2:
8 70
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10
Sample Output 2:
0
这道题我最初想到的方法是用map中套map来表示所有人之间的通话关系,其中map第一层代表一个人,它内部的map记录着和他通话的所有人以及时长,这样对于单个人的统计是方便的,但是难以确定一个团体,后来我想到了用并查集来解决这个问题,我在这里遇到了麻烦,原因是我把三位字母按照26进制规则对应为了数字,这样的对应关系不容易实现并查集,后来看了sunbaigui的解法,豁然开朗,我是比较完整的参考了他的代码,因此下面是对他代码的分析。
他是通过两个map和一个set实现了人名从0到N-1的编号:
map<string, int> name2id;
map<int, string> id2name;
set<string> name;
在输入姓名时,由于无法判断人数,因此无法立即生成对应关系表,因此在输入时把通话记录暂存在Call结构体中,同时把每个名字都压入set。
在输入结束后,通过set即可判断人数,这时候利用迭代器从头到尾遍历set,设立一个id变量,每处理一个人id+1,保证每个名字对应的id不一样,对应方法为:
对于name2id图,将名字对应的索引存放id;对于id2name图,将id对应的索引存放名字,从而实现双向查找:
set<string>::iterator it1;
int id = 0;
for (it1=name.begin(); it1!=name.end(); it1++,++id){
name2id[*it1] = id, id2name[id]=*it1;
}
经过这样的处理,每个名字都可以对应为一个0到N-1的序号,从而可以实现正常的并查集。
对于0到N-1的这些结点,每个结点有两个属性,权值和父结点,初始化集合时将父结点全部初始化为自己,对于每个通话记录Call,只要发生在A、B之间,A和B的权值均增加这个值(这也就造成了在这之后对权值和与阈值进行比较时要考虑二倍阈值,因为双向记录造成了时间的翻倍)。然后合并集合A、B,处理完所有通话记录后就得到了并查集S。
接下来建立一张图map<int,Gang>来表示黑帮,其中第一维的int表示每个集合的父结点,第二维黑帮结构体记录着帮内人数、通话总时长(判断是否满足阈值)、最大通话时长(判断老大)、老大的编号。通过并查集的合并,会形成多个连通子图,对于同一个连通子图,只要进行过路径压缩,通过findSet找到的父结点是一致的,通过这个特性,可以保证一个黑帮对应一个连通子图。
这时候只需要遍历整个并查集中所有元素,先找到父结点top,如果map中不存在top这一维,说明这个黑帮还没有被建立,则建立新的黑帮,初始化黑帮的人数为1,通话总时长和最大通话时长(用于判断老大是谁)都是这个人的通话时长,老大也初始化为这个人,此后再发现属于同一个黑帮的人(父结点一致),这个黑帮人数+1,通话总时长加上这个人的时长,如果这个人的总时长大于当前最大通话时长,则把头目更新为这个人,如此下来,得到的就是所有黑帮的信息。
下面就是对黑帮进行筛选,注意要判断是否大于二倍阈值,最后输出即可,为了保证按照人名字母顺序,可以先建立一个容器存放老大的名字和人数,然后对人名排序,最后输出。
下面是sunbaigui的代码:
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <stdio.h>
using namespace std; typedef struct Node
{
int weight, parent;
}Node;
typedef struct Call
{
string a, b;
int t;
}Call;
typedef struct Gang
{
int head, num, sum, maxw;
Gang(){head=-1;num=0;sum=0;maxw=-1;}
};
vector<Node> p;//disjoint set
vector<Call> call;
map<string, int> name2id;
map<int, string> id2name;
set<string> name;
int n, k;
int realn;//number of distinct node
void InitSet()
{
p.resize(realn);
for (int i = 0; i < realn; ++i)
{
p[i].parent = i; p[i].weight = 0;
}
}
void CompressSet(int top, int x)
{
if (top != p[x].parent)
{
CompressSet(top, p[x].parent);
p[x].parent = top;
}
}
int FindSet(int x)
{
if (x != p[x].parent)
{
int top = FindSet(p[x].parent);
CompressSet(top, x);
}
return p[x].parent;
}
void UnionSet(int x, int y)
{
int a = FindSet(x);
int b = FindSet(y);
p[a].parent = b;
}
int main()
{
int n,k;
cin >> n >> k;
call.resize(n);
name.clear();
for (int i = 0; i < n; ++i)
{
cin>>call[i].a; cin>>call[i].b; cin>>call[i].t;
name.insert(call[i].a); name.insert(call[i].b);
}
//get the person number
realn = name.size();
name2id.clear();
id2name.clear();
set<string>::iterator it1;
int id = 0;
for (it1=name.begin(); it1!=name.end(); it1++,++id){
name2id[*it1] = id, id2name[id]=*it1;
}
//build disjoint set
InitSet();
for (int i = 0; i < call.size(); ++i)
{
int u = name2id[call[i].a]; int v = name2id[call[i].b]; int t = call[i].t;
p[u].weight += t; p[v].weight += t;
UnionSet(u, v);
}
//collect all set
map<int,Gang> allSet;//father and weight of set
map<int,Gang>::iterator it;
for (int i = 0; i < realn; ++i)
{
int top = FindSet(i);
it = allSet.find(top);
if (it != allSet.end())
{
allSet[top].sum += p[i].weight;
allSet[top].num++;
if (p[i].weight > allSet[top].maxw)
{
allSet[top].head = i;
allSet[top].maxw = p[i].weight;
} }
else
{
Gang tmp;
tmp.head = i; tmp.maxw = p[i].weight;
tmp.num = 1; tmp.sum = p[i].weight;
allSet[top] = tmp;
}
}
//threthold gang
std::vector<Gang> gang;
for (it = allSet.begin(); it!=allSet.end(); it++)
if (it->second.sum > k * 2 && it->second.num > 2)
gang.push_back(it->second);
//output
vector<pair<string, int> > head;
for (int i = 0; i < gang.size(); ++i)
head.push_back(make_pair(id2name[gang[i].head],gang[i].num));
sort(head.begin(), head.end());
printf("%d\n",head.size());
for (int i = 0; i < head.size(); ++i)
cout<<head[i].first<<" "<<head[i].second<<endl;
return 0;
}
1034. Head of a Gang (30) -string离散化 -map应用 -并查集的更多相关文章
- 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 ...
- 1034. Head of a Gang (30)
分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...
- POJ 1733 Parity game(离散化+带权并查集)
离散化+带权并查集 题意:长度为n的0和1组成的字符串,然后问第L和R位置之间有奇数个1还是偶数个1. 根据这些回答, 判断第几个是错误(和之前有矛盾)的. 思路:此题同HDU 3038 差不多,询问 ...
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
- 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.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...
- 7-10 社交网络图中结点的“重要性”计算(30 point(s)) 【并查集+BFS】
7-10 社交网络图中结点的"重要性"计算(30 point(s)) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络 ...
随机推荐
- 四柱加强版汉诺塔HanoiTower----是甜蜜还是烦恼
我想很多人第一次学习递归的时候,老师或者书本上可能会举汉诺塔的例子. 但是今天,我们讨论的重点不是简单的汉诺塔算法,而是三柱汉诺塔的延伸.先来看看经典的三柱汉诺塔. 一.三柱汉诺塔(Hanoi_Thr ...
- 前端开发利器VSCode
最近找到一款非常好用的开发利器,VSCode.一直认为微软做的东西都很一般,这个软件让我刮目相看了. 之前使用webstorm卡的不行,换了这个非常好用. 用着还不错,这里记录下一些使用的心得. VS ...
- lodop打印收费小票
//收费系统打印机功能:收费/退费,需要使用到lodop var LODOP;//打印机 $(function () { //初始化 $("body").append('<o ...
- 阻止Enter键回发到服务端Asp.net
//阻止enter键回发到服务端$(function () { $("input[type=text]").each(function () { $(this) ...
- 关于java的Synchronized,你可能需要知道这些(下)
上一篇文章介绍了synchronized的基本使用方法和实现,在实现部分说明了synchronized的底层实现依赖系统互斥锁mutex,但是这个一个重型锁,竞争导致线程阻塞挂起,后续拿到锁后再恢复线 ...
- URL重定向漏洞,python打造URL重定向漏洞检测脚本
前言: 今天学习了重定向漏洞,这个漏洞比较好理解 漏洞名:URL重定向漏洞 威胁:低 漏洞的来源:开发者对head头做好对应的过滤和限制 例子: 有漏洞的网站:http://a.com/x.php?u ...
- CMS垃圾收集器
介绍 CMS垃圾回收器的全称是Concurrent Mark-Sweep Collector,从名字上可以看出两点,一个是使用的是并发收集,第二个是使用的收集算法是Mark-Sweep.从而也可以推测 ...
- 分布式一致性协议Raft原理与实例
分布式一致性协议Raft原理与实例 1.Raft协议 1.1 Raft简介 Raft是由Stanford提出的一种更易理解的一致性算法,意在取代目前广为使用的Paxos算法.目前,在各种主流语言中都有 ...
- Unity发布至IOS的流程(踩坑记录)
这篇文章主要用于记录本人亲身经历过的Unity发布到IOS平台所遇到的所有坑(其实也就是一些自己并不明白的强制设定),以便于后续再有类似需求时少走些弯路. 我的环境: Unity 5.2.2 个人版( ...
- Windows下Java调用BAT批处理不弹出cmd窗口
常规Windows下Java调用BAT方式肯定会弹出cmd窗口 Runtime.getRuntime().exec("cmd.exe /C start D:\\test.bat") ...