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 threthold 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
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
并查集的方法,较为复杂:
#include <iostream>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
map<string,string> map1;
map<string,int> map2;
map<string,int > map3;
map<string,int > map4;//用于计数
struct Node{
string s;
int num;
};
bool cmp(Node node1,Node node2){
return node1.s<node2.s;
}
string find(string s){
while(s!=map1[s]){
s=map1[s];
}
return s;
}
void myunion(string s1,string s2){
s1=find(s1);
s2=find(s2);
if(map1[s1]==map1[s2]){
return;
}
if(map2[s1]>map2[s2]){
map1[s2]=s1;
map3[s1]=map3[s2]+map3[s1];
map3[s2]=0;
map4[s1]=map4[s2]+map4[s1];
map4[s2]=0;
}else{
map1[s1]=s2;
map3[s2]=map3[s1]+map3[s2];
map3[s1]=0;
map4[s2]=map4[s2]+map4[s1];
map4[s1]=0;
}
}
int main()
{
int N,K;
cin>>N>>K;
vector<string> v1;
vector<string> v2;
for(int i=0;i<N;i++){
string s1,s2;
int time;
cin>>s1>>s2>>time;
map1[s1]=s1;
map1[s2]=s2;
map2[s1]+=time;
map2[s2]+=time;
map3[s1]+=time;
map3[s2]+=time;
map4[s1]=1;
map4[s2]=1;
v1.push_back(s1);
v2.push_back(s2);
}
for(int i=0;i<v1.size();i++){
myunion(v1[i],v2[i]);
}
vector<Node> vector1;
for(auto x:map1){
string s=x.first;
if(map3[s]>K*2&&map4[s]>2){
//cout<<map3[s]<<endl;
Node node;
node.s=s;
node.num=map4[s];
vector1.push_back(node);
}
}
sort(vector1.begin(),vector1.end(),cmp);
cout<<vector1.size()<<endl;
for(auto x:vector1){
cout<<x.s<<" "<<x.num<<endl;
}
return 0;
}
深度优先搜索的版本
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <unordered_set>
#include <climits>
using namespace std;
map<string,vector<string>> graph;
map<string,int> weights;
map<string,bool> visit;
string head;
int totalWeight;
int totalNum=0;
int maxWeight;
void dfs(const string &s){
totalNum++;
totalWeight+=weights[s];
visit[s]= true;
if(weights[s]>maxWeight){
head=s;
maxWeight=weights[s];
}
for(string s1:graph[s]){
if(!visit[s1]){
dfs(s1);
}
}
}
int main(){
int N,K;
cin>>N>>K;
int j=0;
unordered_set<string> names;
for (int i = 0; i < N; ++i) {
string name1,name2;
int time;
cin>>name1>>name2>>time;
graph[name1].push_back(name2);
graph[name2].push_back(name1);
weights[name1]+=time;
weights[name2]+=time;
names.insert(name1);
names.insert(name2);
}
map<string,int> res;
for (auto iter=names.begin();iter!=names.end();iter++){
string s=*iter;
head=s;
totalNum=0;
totalWeight=0;
maxWeight=INT_MIN;
dfs(s);
if(totalNum>2&&totalWeight>2*K){
res[head]=totalNum;
}
}
cout<<res.size()<<endl;
for (auto pair:res){
cout<<pair.first<<" "<<pair.second<<endl;
}
return 0;
}