PAT A1034 Head of a Gang

1034 Head of a Gang

分数 30

作者 CHEN, Yue

单位 浙江大学

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

一个图的遍历,非要想着这么复杂,还想到了并查集。这个题在半年前看算法笔记的时候就看到过,当时还上手实践过,然而今天看到这个题,脑袋依旧是没开窍,这第一个代码就是今天写的,又长又臭。

第二个代码的字符串转换为数字,不是用进制来进行转换的,值得学习。当字符串的长度是七八九十个的时候,用进制转换为数字当作数组的下标,明显不现实,但借助 一个map容器,就可简单将字符串转换为数字的大小控制在字符串的数量以内。看代码实现,这第二个是算法笔记上的代码。

不管怎么说,只要从此掌握了这个转换方法,也不能说不是一种收获。

 * 输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,都需要
 * 用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还需要用到原来
 * 的名字,因此,此时的数字也能返过去访问原来的名字,所以要将名字转换为的数字与
 * 名字相映射,用一个map容器记录就行。
 * 由于是一个不超过三个字符的名字,所以可以直接用26进制进行英文转换为数字。
 *
 * 同一个团伙的成员可以用并查集进行合并,头领记录该团伙的成员数量,其他成员指向
 * 该头领。最后再用图的遍历确定每个团队的首领。
 *
 * 最后注意计算团队总时长 > 阈值k时,题目要求的总时长是总时长/2,即两个人之间的通话
 * 只能算一次,通话时长总计需要除以2。

/**
 * 输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,都需要
 * 用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还需要用到原来
 * 的名字,因此,此时的数字也能返过去访问原来的名字,所以要将名字转换为的数字与
 * 名字相映射,用一个map容器记录就行。
 * 由于是一个不超过三个字符的名字,所以可以直接用26进制进行英文转换为数字。
 * 
 * 同一个团伙的成员可以用并查集进行合并,头领记录该团伙的成员数量,其他成员指向
 * 该头领。最后再用图的遍历确定每个团队的首领。
 * 
 * 最后注意计算团队总时长 > 阈值k时,题目要求的总时长是总时长/2,即两个人之间的通话
 * 只能算一次,通话时长总计需要除以2。
*/

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>

using namespace std;

typedef pair<string,int> PSI;

//dfs返回的类型,total记录该团队的成员之间通话的总时间,maxv记录该团伙中打电话时长
//最长的一个人的时长,h记录该成员的字符串映射的数字编号;
struct Node
{
    int total, maxv, h;
};

const int N = 3e5;
int fath[N]; //并查集数组
bool hs[N]; //该成员是否被访问过
map<int,string> int_nm; //数字映名字
int num[N]; //每个成员的通话时长
vector<int> Adj[N]; //邻接表
set<PSI> res; //记录结果
int k;

//字符串转换为数字
int Stoi(string s)
{
    int n = 1;
    for(int i=0; i<s.size(); ++i)
        n = n*27 + s[i] - 'A' + 1;
    return n;
}

//并查集的查找祖宗结点编号
int find(int u)
{
    if(fath[u] < 0)
        return u;
    else 
        return fath[u] = find(fath[u]);
        //此处需要trturn,否则会出现段错误,想想也是
}

//并查集合并函数
void Union(string a, string b, int t)
{
    int u = Stoi(a), v = Stoi(b);
    int_nm[u] = a, int_nm[v] = b;
    Adj[u].push_back(v), Adj[v].push_back(u);
    num[u] += t, num[v] += t;

    int fau = find(u), fav = find(v);
    if(fau != fav)
    {
        fath[fau] += fath[fav]; //注意此处是加,不是相减
        fath[fav] = fau;
    }
}

void Read()
{
    int n;
    memset(fath, -1, sizeof(fath)); //注意初始化并查集数组
    cin >> n >> k;
    
    while(n--)
    {
        string a, b;
        int t;
        cin >> a >> b >> t;
        Union(a, b, t);
    }
}

Node dfs(int u)
{
    hs[u] = 1;
    int total = num[u], maxval = num[u];
    //cout << int_nm[u] << "->" << num[u] << endl;
    
    int h = u;
    for(int i=0; i<Adj[u].size(); ++i)
    {
        int v = Adj[u][i];
        if(hs[v] == 0)
        {
            //cout << int_nm[u] << "->" << int_nm[v] << endl;
            auto ans = dfs(v);
            
            //搜索每个团队的首领
            if(ans.maxv > maxval)
            {
                h = ans.h;
                maxval = ans.maxv;
            }
            total += ans.total;
        }
    }
    
    return {total, maxval, h};
}

void Deal()
{
    for(int i=0; i<N; ++i)
    {
        if(Adj[i].size()) //如果i有团队,
        {
            for(auto s : Adj[i])
            {
                if(hs[s] == 0) //如果此时s还未被访问过
                {
                    int fau = find(s);
                    if(fath[fau] >= -2) //s所在的团队成员数量不足2,不满足题意
                        continue;
                    Node ans = dfs(s);
                    
                    //注意此处的团队通话时长总计需要除以2,因为返回的是团队中每个
                    //人的通话总时长,而题目要求的是总时长/2 > 阈值k;
                    if(ans.total/2.0 <= k)   continue; 
                    res.insert({int_nm[ans.h], -fath[fau]});
                   // cout << ans.total << "**" << ans.maxv << "**" << ans.h 
                     //   << "**" << int_nm[ans.h] << "**" << num[ans.h] << endl;
                }
            }
        }
    }
}

void Print()
{
    cout << res.size() << endl;
    for(auto s : res)
        cout << s.first << ' ' << s.second << endl;
}

int main()
{
    Read();
    
    Deal();
    
    Print();
    
    return 0;
}

2)《算法笔记》之代码:

思路:
输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,
都需要用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还
需要用到原来的名字,因此,此时的数字也能返过去访问原来的名字,那么定
义两个map容器便可。
剩下的便是图的遍历了。同时要注意,进行图的遍历时,是从0这个顶点开始进行遍历的。

第二个代码的字符串转换为数字,不是用进制来进行转换的,值得学习。当字符串的长度是七八九十个的时候,用进制转换为数字当作数组的下标,明显不现实,但借助 一个map容器,就可简单将字符串转换为数字的大小控制在字符串的数量以内。看代码实现,这第二个是算法笔记上的代码。

/*
思路:
输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,
都需要用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还
需要用到原来的名字,因此,此时的数字也能返过去访问原来的名字,那么定
义两个map容器便可。
剩下的便是图的遍历了。同时要注意,进行图的遍历时,是从0这个顶点开始进行遍历的。
*/

#include <iostream>
#include <map>
#include <vector>

using namespace std;

map<string, int> str_int;
map<int, string> int_str;
map<string, int> gang;

const int N = 2010;
int w[N];
vector<int> Adj[N];
bool hs[N];

int n, k;
int idx;

int change(string s)
{
    if(str_int.find(s) != str_int.end())
        return str_int[s];
    else
        return idx++;
}

void Read()
{
    cin >> n >> k;
    while (n -- )
    {
        string a, b;
        int t;
        cin >> a >> b >> t;
        int u = change(a), v = change(b);
        str_int[a] = u, str_int[b] = v;
        int_str[u] = a, int_str[v] = b;
        w[u] += t, w[v] += t;
        Adj[u].push_back(v), Adj[v].push_back(u);
    }
}

void dfs(int index, int &h, int &sum, int &num)
{
    hs[index] = 1;
    if(w[index] > w[h])
        h = index;
    ++num;
    sum += w[index];
    
    for(int i=0; i<Adj[index].size(); ++i)
    {
        int v = Adj[index][i];
        if(hs[v] == 0)
            dfs(v, h, sum, num);
    }
}

void Traver()
{
    for(int i=0; i<idx; ++i)
    {
        if(hs[i] == 0)
        {
            int h = i, sum = 0, num = 0;
            dfs(i, h, sum, num);
            if(sum/2.0 > k && num > 2)
                gang.insert({int_str[h], num});
        }
    }
    
    cout << gang.size() << endl;
    for(auto s : gang)
        cout << s.first << ' ' << s.second << endl;
}

int main()
{
    Read();
    
    Traver();
    
    return 0;
    
}

3)将自己用进制实现的字符串转换为数字的函数,变为算法笔记上的数字逐渐累加的函数。

/**
 * 输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,都需要
 * 用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还需要用到原来
 * 的名字,因此,此时的数字也能返过去访问原来的名字,所以要将名字转换为的数字与
 * 名字相映射,用一个map容器记录就行。
 * 由于是一个不超过三个字符的名字,所以可以直接用26进制进行英文转换为数字。
 * 
 * 同一个团伙的成员可以用并查集进行合并,头领记录该团伙的成员数量,其他成员指向
 * 该头领。最后再用图的遍历确定每个团队的首领。
 * 
 * 最后注意计算团队总时长 > 阈值k时,题目要求的总时长是总时长/2,即两个人之间的通话
 * 只能算一次,通话时长总计需要除以2。
*/

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>

using namespace std;

typedef pair<string,int> PSI;

//dfs返回的类型,total记录该团队的成员之间通话的总时间,maxv记录该团伙中打电话时长
//最长的一个人的时长,h记录该成员的字符串映射的数字编号;
struct Node
{
    int total, maxv, h;
};

const int N = 3e5;
int fath[N]; //并查集数组
bool hs[N]; //该成员是否被访问过
map<int,string> int_nm; //数字映名字
map<string, int> str_int;
int num[N]; //每个成员的通话时长
vector<int> Adj[N]; //邻接表
set<PSI> res; //记录结果
int k;
int idx;

//字符串转换为数字
int change(string s)
{
    if(str_int.find(s) != str_int.end())
        return str_int[s];
    else
        return idx++;
}

//并查集的查找祖宗结点编号
int find(int u)
{
    if(fath[u] < 0)
        return u;
    else 
        return fath[u] = find(fath[u]);
        //此处需要trturn,否则会出现段错误,想想也是
}

//并查集合并函数
void Union(string a, string b, int t)
{
    int u = change(a), v = change(b);
    int_nm[u] = a, int_nm[v] = b;
    str_int[a] = u, str_int[b] = v;
    Adj[u].push_back(v), Adj[v].push_back(u);
    num[u] += t, num[v] += t;

    int fau = find(u), fav = find(v);
    if(fau != fav)
    {
        fath[fau] += fath[fav]; //注意此处是加,不是相减
        fath[fav] = fau;
    }
}

void Read()
{
    int n;
    memset(fath, -1, sizeof(fath)); //注意初始化并查集数组
    cin >> n >> k;
    
    while(n--)
    {
        string a, b;
        int t;
        cin >> a >> b >> t;
        Union(a, b, t);
    }
}

Node dfs(int u)
{
    hs[u] = 1;
    int total = num[u], maxval = num[u];
    //cout << int_nm[u] << "->" << num[u] << endl;
    
    int h = u;
    for(int i=0; i<Adj[u].size(); ++i)
    {
        int v = Adj[u][i];
        if(hs[v] == 0)
        {
            //cout << int_nm[u] << "->" << int_nm[v] << endl;
            auto ans = dfs(v);
            
            //搜索每个团队的首领
            if(ans.maxv > maxval)
            {
                h = ans.h;
                maxval = ans.maxv;
            }
            total += ans.total;
        }
    }
    
    return {total, maxval, h};
}

void Deal()
{
    for(int i=0; i<idx; ++i)
    {
        if(Adj[i].size()) //如果i有团队,
        {
            for(auto s : Adj[i])
            {
                if(hs[s] == 0) //如果此时s还未被访问过
                {
                    int fau = find(s);
                    if(fath[fau] >= -2) //s所在的团队成员数量不足2,不满足题意
                        continue;
                    Node ans = dfs(s);
                    
                    //注意此处的团队通话时长总计需要除以2,因为返回的是团队中每个
                    //人的通话总时长,而题目要求的是总时长/2 > 阈值k;
                    if(ans.total/2.0 <= k)   continue; 
                    res.insert({int_nm[ans.h], -fath[fau]});
                   // cout << ans.total << "**" << ans.maxv << "**" << ans.h 
                     //   << "**" << int_nm[ans.h] << "**" << num[ans.h] << endl;
                }
            }
        }
    }
}

void Print()
{
    cout << res.size() << endl;
    for(auto s : res)
        cout << s.first << ' ' << s.second << endl;
}

int main()
{
    Read();
    
    Deal();
    
    Print();
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值