2012年浙大:Head of a Gang

题目描述:

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.

输入:

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.

输出:

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.

样例输入:
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
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
样例输出:
2
AAA 3
GGG 3
0
  来源:http://ac.jobdu.com/problem.php?pid=1446
题意:给定一组电话单,若一群人中(cluster,大于2)所通话时间总和大于阈值(threthold)k,则他们属于一个团伙(gangs),团伙中与其他人通话时间总和最大的人是head。输出团伙数目,按字典序输出各个团伙的head, 以及团伙中的人数。
#include<iostream>
#include<string>
#include<map>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=1005;
map<string,int> Hash;
int num;
map<int,string> Re;
map<string,int> Count;
vector<string> vec;
int n,k;
int mp[MAXN][MAXN];
int Encode(string s)
{
    if(Hash[s]==0)
    {
        num++;
        Hash[s]=num;
        return num;
    }
    else    return Hash[s];
}
int mx,node,gangs,sum;
int vis[MAXN];
void dfs(int u)
{
    vis[u]=1;gangs++;
    int s=0;
    for(int i=1;i<=num;i++)
    {
        if(mp[u][i]!=0)
        {
            sum+=mp[u][i];
            s+=mp[u][i];
            if(!vis[i])
            {
                dfs(i);
            }
        }
    }
    if(s>mx)
    {
        mx=s;
        node=u;
    }
}
int main()
{
    while(cin>>n>>k)
    {
        Hash.clear();
        Re.clear();
        vec.clear();
        Count.clear();
        memset(vis,0,sizeof(vis));
        num=0;
        memset(mp,0,sizeof(mp));
        for(int i=0;i<n;i++)
        {
            string no1,no2;
            int time;
            cin>>no1>>no2>>time;
            int u=Encode(no1);
            int v=Encode(no2);
            Re[u]=no1;
            Re[v]=no2;
            mp[u][v]+=time;
            mp[v][u]+=time;
        }
        for(int i=1;i<=num;i++)
        {
            if(!vis[i])
            {
                gangs=0;
                mx=0;
                sum=0;
                dfs(i);
                sum/=2;
                if(gangs>=3&&sum>k)
                {
                    string no=Re[node];
                    Count[no]=gangs;
                    vec.push_back(no);
                }
            }
        }
        
        sort(vec.begin(),vec.end());
        cout<<vec.size()<<endl;
        for(int i=0;i<vec.size();i++)
        {
            string no=vec[i];
            cout<<no<<" "<<Count[no]<<endl;
        }
        
    }

    return 0;
}
原文地址:https://www.cnblogs.com/program-ccc/p/5320890.html