题干:
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
题目大意:
警察发现团伙头目的一种方法是检查人们的电话。如果a和B之间有电话,我们说a和B是相关的。关系的权重被定义为两个人之间所有通话的总时间长度。一个“团伙”是一个由2个以上的人组成的群体,他们相互之间有关联,总关联权重大于给定的阈值K。在每一组中,总重量最大的是头部。现在给出一个电话列表,你应该找到团伙和头目。
解题报告:
并查集维护集合元素个数,集合的权值和,每个元素的权值即可。
最后按要求输出答案。
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
map<string,int> mp;
string NAME[MAX];
char s1[5],s2[5];
int n,K;
int tot,f[MAX],num[MAX],V[MAX];
int val[MAX];
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]);
}
void merge(int u,int v,int w) {int t1 = getf(u),t2 = getf(v);if(t1 == t2) {V[t1] += w;return;}f[t2] = t1;num[t1] += num[t2];V[t1] += V[t2]+w;
}
vector<int> boss;
PII pr[MAX];
struct Node {string name;int num;
} ans[MAX];
int top;
bool cmp(Node a,Node b) {return a.name < b.name;
}
int main()
{cin>>n>>K;for(int w,i = 1; i<=n; i++) {scanf("%s%s%d",s1,s2,&w);if(mp.find(s1) == mp.end()) mp[s1] = ++tot,NAME[tot] = s1,num[tot] = 1,f[tot] = tot;if(mp.find(s2) == mp.end()) mp[s2] = ++tot,NAME[tot] = s2,num[tot] = 1,f[tot] = tot;val[mp[s1]] += w;val[mp[s2]] += w;merge(mp[s1],mp[s2],w);}for(int i = 1; i<=tot; i++) {if(f[i] == i && num[i] > 2 && V[i] > K) boss.push_back(i),pr[i].first = i,pr[i].second = val[i]; }for(int i = 1; i<=tot; i++) {int tar = getf(i);if(val[i] > pr[tar].second) {pr[tar].first = i; pr[tar].second = val[i];}}for(auto x : boss) {ans[++top].name = NAME[pr[x].first];ans[top].num = num[x];}sort(ans+1,ans+top+1,cmp);printf("%d\n",top);for(int i = 1; i<=top; i++) {cout << ans[i].name << " " << ans[i].num << endl;}return 0 ;
}