1034 (DFS)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 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

题意:罪犯之间会互相通话,如果满足成员人数大于2,且总通话时间大于阈值K,这个群体就称为Gang,每个Gang中通话时间最长的就是头目head,求Gang的个数,按照字典序输出每个Gang中的头目和总人数。

分析:首先解决姓名->编号、编号->姓名的问题,因为给的是字母,所以要用两个map把它们转换成数字,从1开始排列命名所有不同的人的id,存储在两个map中,一个字符串对应id,一个id对应字符串,方便查找,正好顺便统计了总共的人数idNumber。
注意:

//因为给的是字母,所以要用两个map把它们转换成数字,从1开始排列命名所有不同的人的id,存储在两
//个map中,一个字符串对应id,一个id对应字符串,方便查找,正好顺便统计了总共的人数idNumber。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 2010;
const int INF = 1000000;
map<int, string> intTostring;   //编号->姓名
map<string, int> stringToint;   //姓名->编号
map<string, int> Gang;          //头目->人数
int G[maxn][maxn] = {0}, weight[maxn] = {0};    //边权和点权
int n, k, numPerson = 0;    //边数n、阈值k、总人数numPerson
bool vis[maxn] = {false};


//访问单个连通块
//head为头目 numMember为成员编号,totalWeight为连通块的总边权 均为引用(因为他们会发生变化)
void DFS(int nowVisit, int &head, int &numMember, int &totalWeight)

{
    numMember++;    //成员人数+1
    vis[nowVisit] = true;
    if(weight[nowVisit] > weight[head]) //当前访问结点的点权大于头目的点权
    {
        head = nowVisit;    //更新头目
    }
    for(int i = 0; i < numPerson; i++)      //枚举所有人
    {
        if(G[nowVisit][i] > 0)      //如果从nowVisit能到达i
        {
            totalWeight += G[nowVisit][i];      //连通块总边权增加该边权
            //G初始化为INF,删边后G[nowVisit][i]=0,与if(G[nowVisit][i] != INF)矛盾,相当于DFS走回头路了
            //所以条件换成G初始化为0,if(G[nowVisit][i] > 0)
            G[nowVisit][i] = G[i][nowVisit] = 0;    //边权累加完成之后,删除这条边  防止走回头路
            if(vis[i] == false)
                DFS(i, head, numMember, totalWeight);   //如果i未被访问,递归访问i
        }
    }
}

//遍历所有连通块
void DFSTrave()
{
    for(int i = 0; i < numPerson; i++)      //枚举所有人
    {
        if(vis[i] == false)         //
        {
            int head = i, numMember = 0, totalWeight = 0;       //头目,成员数,总边权(后面会更新)
            DFS(i, head, numMember, totalWeight);       //遍历i所在的连通块
            if(numMember > 2 && totalWeight > k)        //如果总边权大于阈值k且人数大于2
            {
                Gang[intTostring[head]] = numMember;    //该团伙的人数
            }
        }
    }
}

//返回姓名str对应的编号
int change(string str)
{
    if(stringToint.find(str) != stringToint.end())      //如果str已经出现过
    {
        return stringToint[str];        //返回编号
    }
    else
    {
        stringToint[str] = numPerson;   //str的编号为numPerson(也就是人数)   姓名->编号
        intTostring[numPerson] = str;   //numPerson对应str        编号->姓名
        return numPerson++;             //人数+1
    }
}
int main()
{
    int w;
    string str1, str2;
    cin >> n >> k;
    for(int i = 0; i < n; i++)
    {
        cin >> str1 >> str2 >> w;       //两条边的端点和点权
        int id1 = change(str1);         //str1转换为编号id1
        int id2 = change(str2);
        weight[id1] += w;               //增加点权
        weight[id2] += w;
        G[id1][id2] += w;               //id1->id2的边权增加w
        G[id2][id1] += w;
    }
    DFSTrave();                         //遍历整个图        
    cout << Gang.size() << endl;        //Gang的个数
    map<string, int>::iterator it;
    for(it = Gang.begin(); it != Gang.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、付费专栏及课程。

余额充值