PAT-A 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

思路:输入1000条边,极端情况下可能有2000个点,使用int型的邻接矩阵,需要内存32*10^6B,远小于题目限制。这次使用广度优先遍历计算连接分量的权重和连接分量点的数量。要注意一条边的权重只能计入连通分量权重一次。

#include <iostream>
#include <map>
#include <string>
#include <queue>
#include <cstring>

using namespace std;

const int maxn = 2005;
map<string, int> n2c; // name to code
string c2n[maxn];     // code to name
int g[maxn][maxn];
int weight[maxn];  // weight of a node
bool inq[maxn];   // 结点是否入过队

void init()
{
    memset(inq, false, sizeof(inq));
    memset(weight, 0, sizeof(weight));
    memset(g, 0, sizeof(g));
}

int gangNum; // 一个连通图中结点的数量
int head; // 一个连通分量的头目结点
int gangW; // 一个连通分量的权重

// 广度优先遍历连通图
void BFS(int now)
{
    queue<int> q;
    q.push(now);
    inq[now] = true;
    gangNum = 1; // 连通分量的结点数量置为1
    gangW = 0; // 连通分量权重置零
    head = now; // 视当前结点为权值最大的结点
    while(!q.empty())
    {
        now = q.front(); // 当前访问结点的编号
        q.pop();
        for(int i = 0; i < n2c.size(); i++) // 访问当前结点的每一条边
        {
            if(g[now][i] != 0)
            {
                gangW += g[now][i];
                g[i][now] = 0; // 将从i到now的边权重置0,防止重复计算连通分量的权重
            }
            if(g[now][i] != 0 && !inq[i])
            {
                q.push(i);
                gangNum++;
                inq[i] = true;
                // 判断新入队结点能否成为head
                if(weight[i] > weight[head])
                    head = i;
            }
        }
    }
}
// 将name转换成数组下标
int name2Code(string name)
{
    int code;
    if(n2c.find(name) == n2c.end())
    {
        code = n2c.size();
        n2c[name] = code;
    }
    else
        code = n2c[name];
    return code;
}

int main()
{
    init();
    int n, k;
    scanf("%d%d", &n, &k);

    for(int i = 0; i < n; i++)
    {
        string st, ed;
        int time;
        cin >> st >> ed >> time;
        int code1 = name2Code(st);
        int code2 = name2Code(ed);
        c2n[code1] = st;
        c2n[code2] = ed;

        // 增加边的权重
        g[code1][code2] += time;
        g[code2][code1] += time;
        // 增加点的权重
        weight[code1] += time;
        weight[code2] += time;

    }

    map<string, int> ans; // head name -> gang number
    for(int i = 0; i < n; i++)
    {
        if(!inq[i])
        {
            BFS(i);
            string& name = c2n[head];
            if(gangW > k && gangNum > 2)
                ans[name] = gangNum;
        }
    }
    cout << ans.size() << endl;
    for(auto it = ans.begin(); it != ans.end(); it++)
        cout << it->first << " " << it->second << endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值