1034 Head of a Gang (30 分)
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
#include<bits/stdc++.h>
using namespace std;
int n;
int par[2005];int Rank[2005];
void init()
{
for(int i=0;i<2005;i++)par[i]=i,Rank[i]=0;
}
int find(int x)
{
if(par[x]==x)return x;
else return par[x]=find(par[x]);
}
bool same(int x,int y)
{
return find(x)==find(y);
}
void unite(int x,int y)
{
if(same(x,y))return;
x=find(x);y=find(y);
if(Rank[x]<Rank[y])par[x]=y;
else {
par[y]=x;
if(Rank[y]==Rank[x])Rank[x]++;
}
}
int m;int k;
int cnt=0;
map<string,int>mp;
string name[2005];
int e[2005][2005];
int tot[2005];
vector<string>gg;
map<string,int>mp2;
int main()
{
//freopen("in.txt","r",stdin);
init();
cin>>m>>k;
memset(e,0,sizeof(e));
string a,b;int c;
while(m--)
{
cin>>a>>b>>c;
if(mp.find(a)==mp.end()){name[cnt]=a;mp[a]=cnt++;}
if(mp.find(b)==mp.end()){name[cnt]=b;mp[b]=cnt++;}
int x=mp[a];int y=mp[b];
if(e[x][y]==-1)e[x][y]=e[y][x]=c;
else e[x][y]+=c,e[y][x]+=c;
tot[x]+=c;tot[y]+=c;
if(!same(x,y))
unite(x,y);
}
set<int>st;st.clear();
for(int i=0;i<cnt;i++)st.insert(find(i));
int ans=0;
set<int>::iterator it=st.begin();
while(it!=st.end())
{
vector<int>vec;vec.clear();
int p=*it;
for(int i=0;i<cnt;i++)
{
if(find(i)==p)vec.push_back(i);
}
int sum=0;int id=0;
for(int i=0;i<vec.size();i++)
{
for(int j=i+1;j<vec.size();j++)
{
int x=vec[i];int y=vec[j];
sum+=e[x][y];
}
}
if(sum>k&&vec.size()>2){
for(int i=0;i<vec.size();i++)
{
if(tot[vec[i]]>tot[vec[id]])id=i;
}
gg.push_back(name[vec[id]]);
mp2[name[vec[id]]]=vec.size();
ans++;
}
it++;
}
cout<<ans<<endl;
sort(gg.begin(),gg.end());
for(int i=0;i<ans;i++)
{
cout<<gg[i]<<" "<<mp2[gg[i]]<<endl;
}
return 0;
}