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
Ω
警察要通过通话记录来确定帮派团伙,认为两个有通话记录的人是有联系的,那么如果有联系的人所构成的连通图人数>2且其中所有通话时长之和超过给定阈值K 就被认定为一个团伙,而通话时长最长的人则认为是团伙头目。现在给出阈值K和一些通话记录,需要你输出团伙个数、团伙头目、团伙人数。
不是很难,都是对图论基础的考察。看似本题是一道有权图,实则通话记录可以单独放一边,然后将通话关系网看成无权无向图。因为权重只涉及团伙判定,跟个体联系无关,我们只需将一个连通图内每个人的通话时长全部相加/2就能得到该分图的权重之和,顺便找出通话记录最长的头头即可。
C ☺ D E
#include <iostream>
#include <map>
#include <set>
using namespace std;
int main()
{
int n, k, t;
cin >> n >> k;
map<string, set<string>> adj;
map<string, bool> visited;
map<string, int> weight, otc;
string a, b;
for (int i = 0; i < n; ++i)
{
cin >> a >> b >> t;
adj[a].insert(b);
adj[b].insert(a);
weight[a] += t;
weight[b] += t;
visited[a] = visited[b] = false;
}
for (auto &u: visited)
{
if (u.second) continue;
set<string> gang{u.first}, tmp1{u.first}, tmp2;
visited[u.first] = true;
while (!tmp1.empty())
{
for (auto &v: tmp1)
for (auto &w: adj[v])
if (!visited[w])
{
tmp2.insert(w);
visited[w] = true;
}
gang.insert(tmp2.begin(), tmp2.end());
tmp1 = tmp2;
tmp2.clear();
}
if (gang.size() > 2)
{
int sum = 0, max = weight[*gang.begin()];
string head = *gang.begin();
for (auto &h: gang)
{
if (weight[h] > max)
{
max = weight[h];
head = h;
}
sum += weight[h];
}
if (sum / 2 > k)
otc[head] = gang.size();
}
}
cout << otc.size() << endl;
for (auto &r: otc)
cout << r.first << " " << r.second << endl;
}
一遍就过,爽
该问题涉及图论,警察通过分析电话记录来识别团伙。如果两个人之间有通话,他们被认为有联系。当一个连通图中的人数超过2且所有通话时长大于给定阈值K时,这个群体被视为团伙,通话时长最长的人为头目。程序需输出团伙总数、团伙头目及其成员数。给定的代码通过构建无权无向图并遍历找到符合条件的团伙和其头目。
376

被折叠的 条评论
为什么被折叠?



