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

C++:

 

/*
 @Date    : 2018-03-17 19:24:54
 @Author  : 酸饺子 (changzheng300@foxmail.com)
 @Link    : https://github.com/SourDumplings
 @Version : $Id$
*/

/*
https://www.patest.cn/contests/pat-a-practise/1034
 */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

struct Person
{
    char name[4];
    int w = 0;
};

static const int MAXN = 26 * 26 * 26 + 10;

int my_hash(char name[])
{
    return (name[0] - 'A') * 26 * 26 + (name[1] - 'A') * 26 + (name[2] - 'A');
}

static int N, K;
static Person data[MAXN];
static int root[MAXN];
static int head[MAXN];

int find_root(int c)
{
    if (root[c] < 0)
    {
        return c;
    }
    return find_root(root[c]);
}

static int total_w[MAXN];

void my_union(int h1, int h2, int w)
{
    int r1 = find_root(h1), r2 = find_root(h2);
    if (data[h1].w > data[head[r1]].w)
    {
        head[r1] = h1;
    }
    if (data[h2].w > data[head[r2]].w)
    {
        head[r2] = h2;
    }
    total_w[r1] += w; total_w[r2] += w;
    if (r1 != r2)
    {
        int headID = (data[head[r1]].w > data[head[r2]].w ?
            head[r1] : head[r2]);
        if (root[r1] <= root[r2])
        {
            root[r1] += root[r2];
            root[r2] = r1;
            total_w[r1] += total_w[r2];
            head[r1] = headID;
        }
        else
        {
            root[r2] += root[r1];
            root[r1] = r2;
            total_w[r2] += total_w[r1];
            head[r2] = headID;
        }
    }
    return;
}

void re_hash(char name[], int h)
{
    name[2] = h % 26 + 'A';
    h /= 26;
    name[1] = h % 26 + 'A';
    h /= 26;
    name[0] = h + 'A';
    return;
}

int main(int argc, char const *argv[])
{
    scanf("%d %d", &N, &K);
    fill(root, root+MAXN, -1);
    fill(total_w, total_w+MAXN, 0);
    for (unsigned i = 0; i < MAXN; ++i)
    {
        head[i] = i;
    }
    for (unsigned i = 0; i < N; ++i)
    {
        char name1[4], name2[4];
        int w;
        getchar();
        scanf("%s %s %d", name1, name2, &w);
        int h1 = my_hash(name1), h2 = my_hash(name2);
        data[h1].w += w; data[h2].w += w;
        my_union(h1, h2, w);
    }
    vector<pair<int, int>> gangs;
    for (unsigned i = 0; i < MAXN; ++i)
    {
        if (root[i] < -2 && total_w[i] / 2 > K)
        {
            gangs.push_back(make_pair(head[i], -root[i]));
        }
    }
    printf("%d\n", gangs.size());
    for (auto &g : gangs)
    {
        char name[4];
        re_hash(name, g.first);
        printf("%s %d\n", name, g.second);
    }
    return 0;
}

Python:



head = list(map(int, input().split()))

n = head[0]
k = head[1]

# print(head)

# 存储边
graph = {}
# 存储每个点的总通话时间
weight = {}


for i in range(n):
    line = input().split()
    a = line[0]
    b = line[1]
    value = int(line[2])
    if a not in graph:
        graph[a] = {}
    if b not in graph[a]:
        graph[a][b] = value
    else:
        graph[a][b] += value
    if b not in graph:
        graph[b] = {}
    if a not in graph[b]:
        graph[b][a] = value
    else:
        graph[b][a] += value
    if a not in weight:
        weight[a] = value
    else:
        weight[a] += value
    if b not in weight:
        weight[b] = value
    else:
        weight[b] += value

# print(graph)


def bfs():
    visit = {}

    # 每个结点初始化为未访问
    for key in graph.keys():
        visit[key] = 0

    flag = 0
    start = -1
    count = 0
    res = []
    while 1:
        flag = 0
        # 找到未访问的节点,记为start
        for key, value in visit.items():
            if value == 0:
                start = key
                flag = 1
                break
        # 如果没有未访问的节点了
        if flag == 0:
            break

        # 得到start的联通集作为列表l
        l = _bfs(start, visit)
        # 去除重复元素
        l = list(set(l))

        if len(l) > 2:
            summ = 0
            for each in l:
                summ += weight[each]
            if summ / 2 > k:
                count += 1
                rr = findhead(l)
                res.append((rr, len(l)))
    res.sort(key=lambda x: x[0])
    return (count, res)


def findhead(l):
    maxx = 0
    rr = '-1'
    for each in l:
        if weight[each] > maxx:
            maxx = weight[each]
            rr = each
    return rr


def _bfs(start, visit):
    # 将start所在的联通集作为列表返回(结点有可能重复)
    a = [start]
    # print(a)
    q = [start]
    while q != []:
        temp = []
        for each in q:
            visit[each] = 1
            for key in graph[each].keys():
                if visit[key] == 0:
                    temp.append(key)
                    a.append(key)
        q = temp
    # print(a)
    return a


r = bfs()
if r[0] == 0:
    print(0)
else:
    print(r[0])
    for each in r[1]:
        print(each[0] + ' %d' % (each[1]))

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值