PAT甲级 1034 -- 没有注释

来源:互联网 发布:网络黑白txt百度云盘 编辑:程序博客网 时间:2024/05/17 21:57

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 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 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
Sample Output 1:
2AAA 3GGG 3
Sample Input 2:
8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
Sample Output 2:
0

//#include <bits/stdc++.h>#include <iostream>#include <math.h>#include <algorithm>#include <string.h>#include <vector>#include <stdio.h>#include <queue>using namespace std;#define MAXSIZE 300000+10#define INF 0x7fffffffint unionSet[MAXSIZE];int length[MAXSIZE];int member[MAXSIZE];vector<pair<int,int> >vp;struct node{    int id,len;}personLen[MAXSIZE];int cmp(const void * a,const void *b){    return (*(node *)b).len-(*(node*)a).len;}bool cmp2(const pair<int,int> a,const pair<int,int> b){    return a.first<b.first;}int nameToInt(char name[]){    int rst;    rst=(name[0]-'A')*10000;    rst+=(name[1]-'A')*100;    rst+=name[2]-'A';    return rst;}void intToName(int n,char name[]){    name[0]=n/10000+'A';    name[1]=n%10000/100+'A';    name[2]=n%100+'A';    name[3]='\0';}int find(int n){    int r=n;    while(r!=unionSet[r])r=unionSet[r];    int i=n,j;    while(unionSet[i]!=r){        j=unionSet[i];        unionSet[i]=r;        i=j;    }    return r;}void join(int x,int y,int len){    int a=find(x),b=find(y);    if(a!=b){        unionSet[a]=b;        length[b]+=length[a]+len;        length[a]=0;        member[b]+=member[a];        member[a]=1;    }else{        length[a]+=len;    }}int main(){    freopen("./in","r",stdin);    int i,j,k;    int n,threshold;    scanf("%d%d",&n,&threshold);    for(i=0;i<MAXSIZE;i++)unionSet[i]=i,member[i]=1,personLen[i].id=i;    char na[3],me[3];    int len;    for(i=0;i<n;i++){        scanf("%s%s%d",na,me,&len);        join(nameToInt(na),nameToInt(me),len);        personLen[nameToInt(na)].len+=len;        personLen[nameToInt(me)].len+=len;    }    qsort(personLen,MAXSIZE,sizeof(node),cmp);    int id;    int count=0;    for(i=0;i<MAXSIZE;i++){         if(personLen[i].len==0)break;        id=personLen[i].id;        if(member[find(id)]>2 && length[find(id)]>threshold){            count++;            vp.push_back(make_pair(id,member[find(id)]));            member[find(id)]=0;        }    }    printf("%d\n",count);    sort(vp.begin(),vp.end(),cmp2);    for(vector<pair<int,int> >::iterator it=vp.begin();it!=vp.end();it++){        intToName((*it).first,na);        printf("%s %d\n",na,(*it).second);    }    return 0;}
原创粉丝点击