当前位置:网站首页>1034 Head of a Gang
1034 Head of a Gang
2022-07-01 05:49: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每个连通分量
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;
struct edge {
int next, val;
bool flag;//记录访问
};
struct Peo {
string name;//头目
int num;//人数
} a[2010];
int n, k, cnt, ans;
vector<edge>g[20000];
bool vis[20000];
int num[20000];//记录人数
int time1[20000];//记录每人通话时间
int sum;//记录每个连通分量的总时间
int p;//记录每个连通分量的总人数
int maxx;//记录每组最大通话时间
int mName;//记录每组头目
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; //防止重复计算边权
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;
}
边栏推荐
- excel高级绘图技巧100讲(一)-用甘特图来展示项目进度情况
- 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“
- 【知识点总结】卡方分布,t分布,F分布
- Bat operation FTP upload and download command
- Send you through the data cloud
- What is the at instruction set often used in the development of IOT devices?
- HCM 初学 ( 四 ) - 时间
- Ucosiii --- engineering transplantation
- SystemVerilog学习-08-随机约束和线程控制
- Why use huluer pie disk instead of U disk?
猜你喜欢

从MLPerf谈起:如何引领AI加速器的下一波浪潮

boot+jsp的高校社团管理系统(附源码下载链接)

My experience from technology to product manager

LED lighting used in health lighting

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

从诺奖知“边缘计算”的未来!

Dear pie users, I want to confess to you!

SystemVerilog学习-10-验证量化和覆盖率

College community management system based on boot+jsp (with source code download link)

葫芦儿 APP 使用帮助
随机推荐
为什么用葫芦儿派盘取代U盘?
芯片,建立在沙粒上的帝国!
excel動態圖錶
亲爱的派盘用户,我要向您表白!
Send you through the data cloud
基于微信小程序的青少年生理健康知识小助手(免费获取源码+项目介绍+运行介绍+运行截图+论文)
excel高级绘图技巧100讲(一)-用甘特图来展示项目进度情况
FPGA - 7系列 FPGA内部结构之Clocking -02- 时钟布线资源
SystemVerilog学习-06-类的封装
OpenGL ES: (1) OpenGL ES的由来 (转)
MySQL数据迁移遇到的一些错误
OpenGL ES: (2) OpenGL ES 与 EGL、GLSL的关系
关于一道01背包问题的·拓展题的思考
He struggled day and night to protect his data
基于LabVIEW的计时器
Ssm+mysql second-hand trading website (thesis + source code access link)
boot+jsp的高校社团管理系统(附源码下载链接)
π盘,让您电脑变成个人的私有云
Build 2022 上开发者最应关注的七大方向主要技术更新
SystemVerilog学习-08-随机约束和线程控制