前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >九度OJ——1446Head of a Gang

九度OJ——1446Head of a Gang

作者头像
AI那点小事
发布2020-04-18 19:51:59
发布2020-04-18 19:51:59
39000
代码可运行
举报
文章被收录于专栏:AI那点小事AI那点小事
运行总次数:0
代码可运行

题目描述: 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. 输入: 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. 输出: 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. 样例输入: 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 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 样例输出: 2 AAA 3 GGG 3 0


思路:这道题是到并查集的题目,但有所不同的电话的双方都可以通话,故首先的判断输入的名字是否在已在数组里,若不在则追加进数组,如在那么就更新通话时间。之后对电话两端进行并操作。 AC代码:

代码语言:javascript
代码运行次数:0
复制
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

typedef struct node{
    string name;        //人名 
    int time;           //通话时间 
    int father;         //父节点 
    int rank;           
}Node;

typedef struct head{
    string name;
    int num;
}Head;

const int MAX = 2001;
Node people[MAX];
Head gang[MAX];
string name1,name2;
int N,threthold,num,time,sum;
int i,j,k;

int Find(int x){
    if(people[x].father == x){
        return x;
    }else{
        return people[x].father = Find(people[x].father);
    }
}

void Union(int root1,int root2)
{
    root1 = Find(root1);
    root2 = Find(root2);
    if(root1 == root2){
        return;
    }else if(people[root1].rank > people[root2].rank){
        people[root1].rank += people[root2].rank;
        people[root2].father = root1;
    }else{
        if(people[root2].rank == people[root1].rank){
            people[root2].rank++;
        }
        people[root1].father = root2;
    }
}

bool cmp_father(Node p1,Node p2)
{
    return p1.father - p2.father;
}

bool cmp_name(Head p1,Head p2)
{
    return p1.name.compare(p2.name);
}

int main()
{
    while(cin>>N>>threthold){
        num = 0;
        while(N--){
            cin>>name1>>name2>>time;
            //查询输入姓名是否已在表中,如果不在,添加进表;否则,更新节点信息  
            for(i = 0 ; i < num ; i++){
                if(people[i].name.compare(name1) == 0){
                    break;
                }
            }
            if(i == num){//表中没有则把结点加入到表中 
                people[i].name = name1;
                people[i].father = i;
                people[i].time = time;
                people[i].rank = 0;
                num++;
            }else{
                people[i].time += time;
            }
            for(j = 0 ; j < num ; j++){
                if(people[j].name.compare(name2) == 0){
                    break;
                }
            }
            if(j == num){//表中没有则把结点加入到表中 
                people[j].name = name2;
                people[j].father = j;
                people[j].time = time;
                people[j].rank = 0;
                num++;
            }else{
                people[j].time += time;
            }
            Union(i,j);
        }
        for(i = 0 ; i < num ; i++){
            Find(i);
        }
        sort(people,people+num,cmp_father);
        i = j = k =0;
        int pre;
        while(i < num){
            gang[k].name = people[i].name;
            pre = i;
            j++;
            sum = people[i].time;
            while((j < num) && (people[j].father == people[i].father)){
                if(people[j].time > people[pre].time){
                    gang[k].name = people[j].name;  
                    pre = j;
                }
                sum += people[j].time;
                j++;
            }
            if(j-i>2 && sum/2>threthold){
                gang[k].num = j-i;
                k++;
            }
            i = j;
        }
        cout<<k<<endl;
        sort(gang,gang+k,cmp_name);
        for(i = 0 ;i < k ; i++){
            cout<<gang[i].name<<" "<<gang[i].num<<endl;
        }
    }

    return 0;
}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档