isGang?

题目描述:

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
方法一:并查集
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 #include<algorithm>
  5 using namespace std;
  6 #define N 1001
  7 typedef struct node{
  8     char name[4];//名字
  9     int time;//通话时间
 10 }Node;
 11 typedef struct gang{
 12     int head;//首领编号
 13 //    int mem[N];//成员的编号
 14     int num;//成员数目
 15     int weight;//总共通话时间
 16 }Gang;
 17 Node people[N];//用于记录每个人的信息 right
 18 int Tree[N];//用于并查集 right
 19 int k;//阈值 right
 20 Gang group[N];//统计每个组的情况 right
 21 int findRoot(int x){
 22     if(Tree[x]==-1) return x;
 23     else{
 24         int tmp=findRoot(Tree[x]);
 25         Tree[x]=tmp;
 26         return tmp;
 27     }
 28 }
 29 Gang* qsort(int Tree[],Gang group[],int num,int &count){//挑出符合的
 30     int i;
 31     //int count=0;
 32     for(i=0;i<num;i++){
 33         if(Tree[i]==-1&&group[i].weight>k&&group[i].num>2) count++;
 34     }
 35     Gang *ans;
 36     ans=(Gang*) malloc(sizeof(Gang)*count);
 37     int j=0;
 38     for(i=0;i<num;i++){
 39         if(Tree[i]==-1&&group[i].weight>k&&group[i].num>2){
 40         ans[j].head=group[i].head;
 41         ans[j].num=group[i].num;
 42         ans[j].weight=group[i].weight;
 43         j++;
 44         }
 45     }
 46     return ans;
 47 
 48 }
 49 bool cmp(Gang a,Gang b){
 50 
 51     return (strcmp(people[a.head].name,people[b.head].name)<0);
 52 }
 53 int main(){
 54     int n;//n 是记录数,k是阈值
 55     while(scanf("%d%d",&n,&k)!=EOF){
 56         int num=0;//当前人数
 57         int i;
 58         for(i=0;i<N;i++) Tree[i]=-1;
 59         //for(i=1;i<N;i++) sum[i]=0;
 60         while(n--!=0){
 61             char name1[4],name2[4];
 62             int s1,s2;//记录name在数组中位置
 63             int weight;//两人通话时长
 64             scanf("%s%s%d",name1,name2,&weight);
 65             for(i=0;i<num;i++){
 66                 if(strcmp(name1,people[i].name)==0)    break;//若已经存在
 67             }
 68             if(i==num){
 69                 strcpy(people[i].name,name1);
 70                 people[i].time=0;
 71                 group[i].num=1;
 72                 group[i].weight=0;
 73                 group[i].head=i;
 74                 num++;
 75             }
 76             s1=i;
 77             people[i].time+=weight;
 78 
 79             for(i=0;i<num;i++){
 80                 if(strcmp(name2,people[i].name)==0) break;
 81             }
 82             if(i==num){
 83                 strcpy(people[i].name,name2);
 84                 people[i].time=0;
 85                 group[i].num=1;
 86                 group[i].weight=0;
 87                 group[i].head=i;
 88                 num++;
 89             }
 90             s2=i;
 91             people[i].time+=weight;
 92 
 93             int s1_=findRoot(s1);
 94             int s2_=findRoot(s2);
 95             if(people[s1].time>people[group[s1_].head].time){//更新组里面的head
 96                 group[s1_].head=s1;
 97             }
 98             if(people[s2].time>people[group[s2_].head].time){//更新组里面的head
 99                 group[s2_].head=s2;
100             }
101             if(s1_!=s2_) {//合并
102                 Tree[s1_]=s2_;
103                 group[s2_].num+=group[s1_].num;
104                 group[s2_].weight+=(weight+group[s1_].weight);
105                 if(people[group[s2_].head].time<people[group[s1_].head].time){
106                     group[s2_].head=group[s1_].head;//
107                 }
108         
109             }
110             else group[s2_].weight+=weight;
111 
112             
113             
114 
115         
116         }
117         Gang* ans;
118         int count=0;
119         ans=qsort(Tree,group,num,count);
120         sort(ans,ans+count,cmp);
121         printf("%d\n",count);
122         for(i=0;i<count;i++){
123             printf("%s %d\n",people[ans[i].head].name,ans[i].num);
124         }
125 
126     }
127 
128 return 0;
129 }

 方法二:邻接表(不知道为什么还不能AC)

#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define LEN 26*26*26+1
struct HeadNum{
    int name;
    int num;
    /*HeadNum(int nam,int n){
        name=nam;
        num=n;
    }*/
}result[1500];
int size;
vector<int> arr[LEN];//邻接表
int sum[LEN];//每个结点访问的时长
bool isRead[LEN];//是否已经访问过
vector<int> linkNode;//存连通图
int str2num(char s[]){
    int ans;
    ans=(s[0]-'A')*26*26+(s[1]-'A')*26+(s[0]-'A');
    return ans;
}
void dfs(int head){//连出一个连通图
    if(isRead[head]==true) return;
    isRead[head]=true;
    int size=arr[head].size();
    int i;
    for(i=0;i<size;i++){
        dfs(arr[head][i]);
    }
    linkNode.push_back(head);
    return;
}
/*HeadNum* isGang(const vector<int> &v,int k){//判断一个连通图是不是满足,并返回
    int s=v.size();
    if(s<3) return NULL;
    int total=0;
    int i;
    int max=0;
    int head=-1;
    for(i=0;i<s;i++){
        total+=sum[v[i]];
        if(sum[v[i]]>max){
            max=sum[v[i]];
            head=v[i];
        }
    }
    if(total/2<=k) return NULL;
    else return new HeadNum(head,s);
}*/
void isGang(const vector<int> &v,int k){//判断一个连通图是不是满足,并返回
    int s=v.size();
    if(s<3) return;
    int total=0;
    int i;
    int max=0;
    int head=-1;
    for(i=0;i<s;i++){
        total+=sum[v[i]];
        if(sum[v[i]]>max){
            max=sum[v[i]];
            head=v[i];
        }
    }
    if(total/2<=k) return ;
    else {
        result[size].name = head;
        result[size].num = s;
        size++;
    }
}
bool cmp(HeadNum a,HeadNum b){
    return a.name <b.name;
}
void num2str(int num, char str[]){
    int i;
    for(i=2;i>=0;i--){
        str[i]=num%26+'A';
        num/=26;
    }
    str[3]=0;
}
int main(){
    int n,k;
    queue<int> node;//队列
    //vector<HeadNum> res;//记录最终结果
    while(scanf("%d%d",&n,&k)!=EOF){
        int i;
        while(!node.empty ()){
            node.pop ();
        }
        //res.clear();
        for(i=0;i<LEN;i++){
            sum[i]=0;
            isRead[i]=false;
            arr[i].clear();
        }
        while(n--){//建立邻接表
            int a,b;
            char s1[5],s2[5];
            int time;
            scanf("%s%s%d",s1,s2,&time);
            a=str2num(s1);
            b=str2num(s2);
            arr[a].push_back(b);//邻接表
            arr[b].push_back(a);//注意因为是无向图
            sum[a]+=time;
            sum[b]+=time;
            node.push(a);//入队列
        }
        //HeadNum* head=NULL;
        size=0;
        while(!node.empty()){
            linkNode.clear();
            dfs(node.front());
            node.pop();
            //head=isGang(linkNode,k);
            isGang(linkNode,k);
            //if(head!=NULL){
            //    res.push_back (*head);
            //}
        }
        //sort(res.begin(),res.end(),cmp);
        sort(result,result+size,cmp);
        printf("%d\n",size);
        char name[5];
        /*for(i=0;i<res.size ();i++){
            num2str(res[i].name,name);
            printf("%s %d\n",name,res[i].num);
        }*/
        for(i=0;i<size;i++){
            num2str(result[i].name,name);
            printf("%s %d\n",name,result[i].num);
        }
    }
  return 0;
}

 

posted @ 2017-02-04 22:48  流浪猫还挑食  阅读(261)  评论(0编辑  收藏  举报