1034. Head of a Gang

1034. Head of a Gang (30)

时间限制
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 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

提交代

做这个题时那天的状态真的好差,各种小错误。刚开始想的dfs,结果写到最后发现会构成回路,其实在做的过程中发现可以用并查集做,然后这个代码就不管了,再用并查集写一遍,我用的是字符串哈希处理字符串,然而我并不知道哈希值不能转字符串的,结果字符串转换错误好久才发现,不过这个题还好,已经规定了字符串长度就是3,然后这个问题算解决了,但是输出字符串的时候却把字符串倒着输出(我是取模求字符串),真是小问题一堆。下次想题一定要把思路想清晰再写,不然写到中间突然没思路了还要重新想思路,这要是在考试的时候就GG,太浪费时间。在网上看别人的代码用的都是离散化,我也试试,顺便再用dfs写一遍    最后附上代码  

#include<bits/stdc++.h>
using namespace std;
int visit[20005],l[1005],r[1005],v[1005],value[20005],num[20005],pre[20005];
struct node{
    int head;
    int num;
}a[1005];
int HashCode(char temp[]){
    int len=strlen(temp);
    int sum=0;
    for(int i=0;i<len;i++){
        sum=sum*26+(temp[i]-'A');
    }
    return sum;
}
int Find(int x){
    if(pre[x]==x) return x;
    return pre[x]=Find(pre[x]);
}

bool cmp(node x,node y){
    return x.head<y.head;
}
void output(int head,int num){
    char temp[5];
    for(int i=1;i<=3;i++){
        if(head){
            temp[i]=head%26+'A';
            head/=26;
        }else{
            temp[i]='A';
        }
    }
    for(int i=3;i>=1;i--){
        printf("%c",temp[i]);
    }
    printf(" %d\n",num);
}
int main(){
    int n,k,x,len=0;
    char name1[10],name2[10];
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%s%s%d",name1,name2,&x);
        l[i]=HashCode(name1);
        r[i]=HashCode(name2);
        v[i]=x;
        value[l[i]]+=x;
        value[r[i]]+=x;
        if(!visit[l[i]]){
            visit[l[i]]=1;
            num[++len]=l[i];
        }
        if(!visit[r[i]]){
            visit[r[i]]=1;
            num[++len]=r[i];
        }
    }
    for(int i=1;i<=len;i++){
        pre[num[i]]=num[i];
    }
    for(int i=1;i<=n;i++){
        int x=Find(l[i]);
        int y=Find(r[i]);
        if(x!=y){
            pre[y]=x;
        }
    }
    for(int i=1;i<=len;i++){
        Find(num[i]);
    }
    int ll=0,max1=0,max2=0,sum=0,counts=0;
    for(int i=1;i<=len;i++){
        int temp=num[i];
        if(pre[temp]==temp){
            max1=temp;max2=value[temp];sum=value[temp];counts=1;
            for(int j=1;j<=len;j++){
                if(num[j]==temp) continue;
                if(pre[num[j]]==temp){
                    if(max2<value[num[j]]){
                        max2=value[num[j]];
                        max1=num[j];
                    }
                    counts++;
                    sum+=value[num[j]];
                }

            }

        }
        if(counts>2&&sum/2>k){
            a[++ll].head=max1;
            a[ll].num=counts;
        }
        counts=0;sum=0;
    }
    sort(a+1,a+1+ll,cmp);
    printf("%d\n",ll);
    for(int i=1;i<=ll;i++){
        output(a[i].head,a[i].num);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值