当前位置:网站首页>1034 Head of a Gang
1034 Head of a Gang
2022-07-01 06:10:00 【Brosto_ Cloud】
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:
0dfs Each connected component
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;
struct edge {
int next, val;
bool flag;// Record access
};
struct Peo {
string name;// Leader
int num;// The number of
} a[2010];
int n, k, cnt, ans;
vector<edge>g[20000];
bool vis[20000];
int num[20000];// Record the number of people
int time1[20000];// Record each person's talk time
int sum;// Record the total time of each connected component
int p;// Record the total number of people in each connected component
int maxx;// Record the maximum talk time of each group
int mName;// Record each group leader
int stoi1(string s) {
return (s[0] - 'A') * 26 * 26 + (s[1] - 'A') * 26 + s[2] - 'A';
}
string itos(int x) {
string s = "";
s += (char)(x % 26 + 'A');
x /= 26;
s += (char)(x % 26 + 'A');
x /= 26;
s += (char)(x + 'A');
reverse(s.begin(), s.end());
return s;
}
void dfs(int x) {
for (int i = 0; i < g[x].size(); i++) {
if (g[x][i].flag) {
continue;
}
sum += g[x][i].val;
g[x][i].flag = 1;
if (vis[g[x][i].next]) {
continue;
}
vis[g[x][i].next] = 1;
p++;
if (time1[g[x][i].next] > maxx || (time1[g[x][i].next] == maxx && g[x][i].next < mName)) {
maxx = time1[g[x][i].next];
mName = g[x][i].next;
}
dfs(g[x][i].next);
}
}
bool cmp(Peo p1, Peo p2) {
return p1.name < p2.name;
}
int main() {
cin >> n >> k;
string s1, s2;
int u, v, t;
for (int i = 0; i < n; i++) {
cin >> s1 >> s2 >> t;
u = stoi1(s1);
v = stoi1(s2);
if (!vis[u]) {
vis[u] = 1;
cnt++;
num[cnt] = u;
}
if (!vis[v]) {
vis[v] = 1;
cnt++;
num[cnt] = v;
}
time1[u] += t;
time1[v] += t;
edge e;
e.flag = 0;
e.next = v;
e.val = t;
g[u].push_back(e);
e.next = u;
e.val = 0; // Prevent double counting of edge weights
g[v].push_back(e);
}
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= cnt; i++) {
if (vis[num[i]]) {
continue;
}
vis[num[i]] = 1;
p = 1;
sum = 0;
mName = num[i];
maxx = time1[num[i]];
dfs(num[i]);
if (p > 2 && sum > k) {
ans++;
a[ans].name = itos(mName);
a[ans].num = p;
}
}
sort(a + 1, a + 1 + ans, cmp);
cout << ans << endl;
for (int i = 1; i <= ans; i++) {
cout << a[i].name << ' ' << a[i].num << endl;
}
return 0;
}
边栏推荐
- srpingboot security demo
- Skywalking integrated Nacos dynamic configuration
- OpenGL ES: (5) OpenGL的基本概念、OpenGL ES 在屏幕产生图片的过程、OpenGL管线(pipeline)
- MySQL怎么存储emoji?
- MySQL里记录货币
- 2022 年面向初学者的 10 大免费 3D 建模软件
- TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)
- π disk, turning your computer into a personal private cloud
- MySQL中 in 和 exists 的区别
- OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
猜你喜欢

C# ManualResetEvent 类的理解

C language beginner level - realize the minesweeping game

蚂蚁新村田头村变甜头村 让厦门灌口镇田头村变甜头村的特色农产品之一是

CJC8988带2个立体声耳机驱动器的低功率立体声编解码器

Dear pie users, I want to confess to you!

Infinite horizontal marble game

Know the future of "edge computing" from the Nobel prize!

One of the characteristic agricultural products that make Tiantou village, Guankou Town, Xiamen into a "sweet" village is

OpenGL ES: (5) OpenGL的基本概念、OpenGL ES 在屏幕产生图片的过程、OpenGL管线(pipeline)

Scope data export mat
随机推荐
让田头村变甜头村的特色农产品是仙景芋还是白菜
DHT11 温湿度传感器
Servlet
SystemVerilog学习-09-进程间同步、通信和虚方法
Cjc8988 Low Power Stereo codec with 2 stereo headphone drivers
【文件系统】如何在ubi之上运行squashfs
数据库问题,如何优化Oracle SQL查询语句更快,效率更高
Oracle sequence + trigger
The row and column numbers of each pixel of multi-source grid data in the same area are the same, that is, the number of rows and columns are the same, and the pixel size is the same
In win10 and win11, the scroll direction of Elan touch panel is reversed, and "double finger click to open the right-click menu" and "double finger scroll" are started“
【笔记】电商订单数据分析实战
Flink实战--多流合并
Diagramme dynamique Excel
highmap gejson数据格式转换脚本
π disk, turning your computer into a personal private cloud
Stack Title: parsing Boolean expressions
Arcserver password reset (account cannot be reset)
SQL必会题之留存率
PLA不粘貼在床上:6個簡單的解决方案
Huluer app help