PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

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

题意:

给定n条记录(注意不是n个人的记录),两个人之间的关系的权值为这两个人之间所有电话记录的时间之和。

一个连通块的权值为所有关系权值之和。

如果一个连通块节点数大于2,且权值大于给定的k,称这是一个gang,拥有关系权值和最多的人是gang的头。

要求输出gang的数量,每个gang的头,每个gang的人数。按照gang的头的字典序排序。

题解:

bfs求连通块。有几个注意点:

1、给定的是n条记录,n<=1000, 这说明人数应该是2000以内而不是1000以内。否则会测试点3运行时错误。

2、每一条记录都要考虑,bfs时不能仅判断这个节点是否被访问,还应该设置边的vis数组。

错误点:

1、忘记排序,测试点2,5答案错误,否则会测试点3运行时错误

2、n<=1000, 这说明人数应该是2000以内而不是1000以内

AC代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstring>
using namespace std;
map<string,int>m1;
map<int,string>m2;
vector<int>v[2005];//给定的是n条记录,n<=1000, 这说明人数应该是2000以内而不是1000以内。测试点3运行时错误
int ok[2005];//标记有没有被访问过
struct node{//存储最终答案
    string head;
    int num;
}ans[2005];
int w[2005];//计算每个点的权重
int n,minw;
queue<int>q;
int k=1;//map里的个数
int ans_num=0;//答案个数
bool cmp(node x,node y){
    return x.head<y.head;
}
void bfs(int x){
    int w_sum=0;//计算总权重
    int num=0;//计算人数
    int max_w=w[x];
    int head=x;
    q.push(x);
    ok[x]=1;//标记有没有被访问过
    num++;
    w_sum+=w[x];
    while(!q.empty()){
        int a=q.front();
        q.pop();
        for(int i=0;i<v[a].size();i++){
            int b=v[a].at(i);
            if(ok[b]) continue;
            if(max_w<w[b]){//找出权重的为头目
                head=b;
                max_w=w[b];
            }
            ok[b]=1;
            q.push(b);
            w_sum+=w[b];
            num++;
        }
    }
    w_sum/=2;
    if(num>=3 && w_sum>minw){
        //cout<<"答案:num"<<num<<" "<<m2[head]<<" "<<ans_num<<" w_sum "<<w_sum<<endl;
        node answer;
        answer.num=num;
        answer.head=m2[head];
        ans[++ans_num]=answer;
    }
}
int main(){
    cin>>n>>minw;
    memset(w,0,sizeof(w));
    memset(ok,0,sizeof(ok));
    for(int i=1;i<=n;i++){
        if(!v[i].empty()) v[i].clear();
    }
    for(int i=1;i<=n;i++){
        string na1,na2;
        int ww;
        cin>>na1>>na2>>ww;
        if(!m1[na1]) {
            m2[k]=na1;
            m1[na1]=k++;
        }
        if(!m1[na2]) {
            m2[k]=na2;
            m1[na2]=k++;
        }
        w[m1[na1]]+=ww;
        w[m1[na2]]+=ww;
        v[m1[na1]].push_back(m1[na2]);
        v[m1[na2]].push_back(m1[na1]);
    }
    for(int i=1;i<=n;i++){
        if(!ok[i]){
            bfs(i);
        }
    }
    cout<<ans_num<<endl;
    sort(ans+1,ans+1+ans_num,cmp);//排序,一开始忘记排了
    for(int i=1;i<=ans_num;i++){
        cout<<ans[i].head<<" "<<ans[i].num<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/caiyishuai/p/11419290.html

时间: 2024-12-28 20:47:58

PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)的相关文章

PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

题目 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 mad

1034. Head of a Gang (30) -string离散化 -map应用 -并查集

题目例如以下: 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 call

【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)

题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个正整数L(<=10000),代表条带的长度,接着输入L个正整数表示条带上的颜色的编号.输出以喜爱颜色顺序排列的最长子串长度(不必每种颜色都有,只保证相对位置相同,同种颜色可连续). 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>

【PAT甲级】1068 Find More Coins (30 分)(背包/DP)

题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[10007];int dp[107];int vis[10007][107];int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.ti

1034 Head of a Gang (30分)

1. 题目 2. 思路 使用并查集归属集合 3. 注意点 可以使用map<string, string> 模拟int类型的并查集,减少了string和int类型转换的问题 因为c++的map,值如果不存在会自动初始化 map<string, string> father; //定义 // 查找root string findfather(string x){ if(father[x] == ""){ father[x] = x; }else{ while(fat

1034. Head of a Gang (30)

注意这里n是电话数,电话数不超过1000代表人数不超过两千,...好坑.... 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 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

PAT (Advanced Level) 1034. Head of a Gang (30)

简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<string> #include<queue> #include<stack> #include<algorithm> #include<iostream> using namespace st

1034 Head of a Gang (30)(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 b

PAT 甲级 1054 The Dominant Color (20 分)

1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of information for each pixel. In an image, the color with the largest proportional area is called the dominant color. A st