PAT (Advanced Level)1034. Head of a Gang (30) map的灵活运用
2018-03-07 13:27
489 查看
题目链接
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
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 threshold, respectively. Then N lines follow, each in the following format:Name1 Name2 Timewhere 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:
代码来源这里,感谢大神简介通俗的代码
//1034. Head of a Gang(30)#include <iostream>
//map是关联性容器,包含关键字---值得索引,map<关键值,值>map
//由于每个人的ID是通过字符串给出的,需要离散化。遍历的过程中需要实时更新权值最大的节点,
//使用map容器来完成对图的构建,以及使用map来对节点权值的索引;可以说此题就是在考察对map的灵活使用,
//当要遍历某个数据结构的时候,如果要检索的索引是字符串,就要考虑使用map, 并且使用map来存储数据,
//天然已经按字符串的字
4000
典序排好了。最后输出的时候避免排序。
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <vector>
using namespace std;
string head;
int cnt, total;
map<string, int>weight;
map<string, bool>visit;
map<string, vector<string> >adjlist;
map<string, int>res;
void DFS(string start)
{
visit[start] = 1;
cnt++;
total += weight[start];
if (weight[start]>weight[head])
head = start;
vector<string>::iterator it = adjlist[start].begin();
for (; it != adjlist[start].end(); it++)
{
if (visit[*it] == 0)
{
DFS(*it);
}
}
}
int main()
{
int N, K, T, i = 0;
cin >> N >> K;
string member1, member2;
while (i<N)
{
cin >> member1 >> member2 >> T;
weight[member1] += T;
weight[member2] += T;
adjlist[member1].push_back(member2);
adjlist[member2].push_back(member1);
visit[member1] = 0;
visit[member2] = 0;
i++;
}
map<string, bool>::iterator ite = visit.begin();
for (; ite != visit.end(); ite++)
{
if (ite->second == 0)
{
total = 0;
cnt = 0;
head = ite->first;
DFS(ite->first);
}
if (cnt>2 && total / 2>K)
res[head] = cnt;
}
map<string, int>::iterator it = res.begin();
cout << res.size() << endl;
for (; it != res.end(); it++)
cout << it->first << " " << it->second << endl;
return 0;
}有关map的相关操作,请访问这里
1034. Head of a Gang (30)
时间限制100 ms内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
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 threshold, respectively. Then N lines follow, each in the following format:Name1 Name2 Timewhere 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 10Sample Output 1:
2 AAA 3 GGG 3Sample 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 10Sample Output 2:
0map的灵活使用
代码来源这里,感谢大神简介通俗的代码
//1034. Head of a Gang(30)#include <iostream>
//map是关联性容器,包含关键字---值得索引,map<关键值,值>map
//由于每个人的ID是通过字符串给出的,需要离散化。遍历的过程中需要实时更新权值最大的节点,
//使用map容器来完成对图的构建,以及使用map来对节点权值的索引;可以说此题就是在考察对map的灵活使用,
//当要遍历某个数据结构的时候,如果要检索的索引是字符串,就要考虑使用map, 并且使用map来存储数据,
//天然已经按字符串的字
4000
典序排好了。最后输出的时候避免排序。
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <vector>
using namespace std;
string head;
int cnt, total;
map<string, int>weight;
map<string, bool>visit;
map<string, vector<string> >adjlist;
map<string, int>res;
void DFS(string start)
{
visit[start] = 1;
cnt++;
total += weight[start];
if (weight[start]>weight[head])
head = start;
vector<string>::iterator it = adjlist[start].begin();
for (; it != adjlist[start].end(); it++)
{
if (visit[*it] == 0)
{
DFS(*it);
}
}
}
int main()
{
int N, K, T, i = 0;
cin >> N >> K;
string member1, member2;
while (i<N)
{
cin >> member1 >> member2 >> T;
weight[member1] += T;
weight[member2] += T;
adjlist[member1].push_back(member2);
adjlist[member2].push_back(member1);
visit[member1] = 0;
visit[member2] = 0;
i++;
}
map<string, bool>::iterator ite = visit.begin();
for (; ite != visit.end(); ite++)
{
if (ite->second == 0)
{
total = 0;
cnt = 0;
head = ite->first;
DFS(ite->first);
}
if (cnt>2 && total / 2>K)
res[head] = cnt;
}
map<string, int>::iterator it = res.begin();
cout << res.size() << endl;
for (; it != res.end(); it++)
cout << it->first << " " << it->second << endl;
return 0;
}有关map的相关操作,请访问这里
相关文章推荐
- 1034. Head of a Gang (30)【离散化+并查集+搜索】——PAT (Advanced Level) Practise
- PAT (Advanced Level) 1034. Head of a Gang (30) 黑帮头目,并查集
- PAT (Advanced Level) 1034. Head of a Gang (30)
- 【PAT】【Advanced Level】1034. Head of a Gang (30)
- 【PAT】【Advanced Level】1131. Subway Map (30)
- PAT (Advanced Level) Practise 1034 Head of a Gang (30)
- PAT (Advanced Level) Practise 1111 Online Map (30)
- PAT (Advanced Level) 1022. Digital Library (30) 数字图书馆 map
- PAT (Advanced) 1034. Head of a Gang (30)
- PAT (Advanced Level) Practise 1111 Online Map (30)
- PAT (Advanced) 1034. Head of a Gang (30)
- PAT (Advanced Level) Practise 1034 Head of a Gang (30)
- PAT (Advanced Level) Practise 1131 Subway Map (30)
- PAT (Advanced Level) Practise 1131 Subway Map (30)
- 1034. Head of a Gang (30)-PAT甲级真题
- 【PAT】【Advanced Level】1123. Is It a Complete AVL Tree (30)
- 【PAT】【Advanced Level】1004. Counting Leaves (30)
- PAT (Advanced Level) 1014. Waiting in Line (30) 银行排队
- PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- 1022. Digital Library (30)——PAT (Advanced Level) Practise