PAT:1034 Head of a Gang (30分)

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 threshold 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

题意

找满足以下两个要求的帮派:
(1) 帮派人数大于2人
(2)帮派的总联系度(通话时长)大于给定的K
除此之外找到满足要求的帮派的老大,老大是帮派中单人联系度最大的人。
输出要求:
先输出满足要求的帮派数,
然后按帮派老大的名字的字典序输出,同时后面输出这个帮派的人数

思路

首先需要将每个人的名字映射出一个唯一编号(id),id和名字需要互相映射。
之后根据联系建图,然后基于dfs,对每个人染色,每次dfs染色的人一定是同一个帮派的人(即每次dfs给有联系的人染同一种颜色),dfs过程中统计需要的信息,最后根据题意处理一下即可。

理清思路,题目实现起来不难,我在本题中使用了比较多STL容器来辅助处理数据。

代码

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
const int P = 2e3+5;
int id;
map<int,string>mp; //id:name
map<string,int>mp2;//name:id
vector<int>edge[P];
int w[P]; //w[i]:编号为i的人的联系度
set<int>st; //所有的人(里面存每个人的编号)
bool vis[P]; //vis[i]标记i这个人是否被染色
int color_id; //帮派编号
vector<int>vec[P]; //每个帮派的人
int head[P]; //每个帮派里的老大
int w_total[P]; 
//w_total[i]:帮派编号i所有人联系度的和(最后统计时需要/2,因为两个人联系是互相的,统计了两遍)
//ans
struct Node
{
    string h;
    int cnt;
    bool operator<(const Node &x)const
    {
        return h < x.h;
    }
} node[P];
//每个人映射一个唯一id
inline int get_id(string s)
{
    if(mp2[s])
        return mp2[s];
    else
    {
        mp[++id] = s;
        mp2[s] = id;
        return id;
    }

}
//建图
inline void add_edge(int x_id,int y_id,int v)
{
    edge[x_id].push_back(y_id);
    edge[y_id].push_back(x_id);
    w[x_id] += v;
    w[y_id] += v;
    st.insert(x_id);
    st.insert(y_id);
}
//dfs 染色
void dfs(int x)
{
    int cnt = edge[x].size();
    for(int i = 0; i < cnt; ++i)
    {
        int to = edge[x][i];
        if(!vis[to])
        {
            vis[to] = true;
            vec[color_id].push_back(to);
            if(w[to] > w[head[color_id]]) head[color_id] = to;
            w_total[color_id] += w[to];
            dfs(to);
        }
    }
    return;
}


int main()
{
    int n,k;
    cin>>n>>k;
    for(int i = 0; i < n; ++i)
    {
        string x,y;
        int t;
        cin>>x>>y>>t;
        int x_id = get_id(x);
        int y_id = get_id(y);
        add_edge(x_id,y_id,t);
    }

    for(set<int>:: iterator it=st.begin(); it!=st.end(); it++)
    {
        if(!vis[*it])
        {
            color_id++;
            vec[color_id].push_back(*it);
            w_total[color_id] += w[*it];
            head[color_id] = *it;
            vis[*it] = true;
            dfs(*it);
        }
    }
    int t = 0;
    for(int i = 1; i <= color_id; ++i)
    {
        int cnt = vec[i].size();
        if(cnt > 2 && w_total[i]/2 > k)
        {
            node[t].h = mp[head[i]];
            node[t].cnt = cnt;
            t++;
        }
    }
    sort(node,node+t);
    cout<<t<<endl;
    for(int i = 0; i < t; ++i)
    {
        cout<<node[i].h<<" "<<node[i].cnt<<endl;
    }
    return 0;
}

$Daily English

Shut out all of your past except that which help you weather your tomorrows.
放下那些不能帮助你前行的过去。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leo Bliss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值