• 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 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

    解题思路:初看题目,求类别肯定用并查集,然后在数据处理上对应不上。看了map的用法后,还是很强大的STL。
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<map>
    #include<vector>
    using namespace std;
    struct Node{
    	char n1[10];
    	char n2[10];
    	int time;
    }node[1002];
    struct RES{
    	int maxtime,sumtime;
    	int member;
    	string name;
    	RES():maxtime(0),sumtime(0),member(0){
    	}
    };
    map<string, int>name_id,tmp,re;
    int index[2002];
    map<int,RES>res;
    int find(int a){
    	while(a!=index[a])a=index[a];
    	return a;
    }
    void union_t(int a,int b){
    	int x=find(a);
    	int y=find(b);
    	if(x!=y){
    		index[x]=y;
    	}
    }
    int main(){
    	int n,k;
    	int i,j;
    	scanf("%d%d",&n,&k);
    	for(i=0;i<n;i++){
    		cin>>node[i].n1>>node[i].n2>>node[i].time;
    		name_id[node[i].n1]+=node[i].time;
    		name_id[node[i].n2]+=node[i].time;
    	}
    	int cnt=0;
    	for(map<string,int>::iterator it=name_id.begin();it!=name_id.end();it++){
    		index[cnt]=cnt;
    		tmp[it->first]=cnt++;
    	}
    	for(i=0;i<n;i++){
    		union_t(tmp[node[i].n1],tmp[node[i].n2]);
    	}
    	for(map<string,int>::iterator it=name_id.begin();it!=name_id.end();it++){
    		int fa=find(tmp[it->first]);
    		if(res[fa].maxtime<it->second){
    			res[fa].maxtime=it->second;
    			res[fa].name=it->first;
    		}
    		res[fa].member++;
    		res[fa].sumtime+=it->second;
    	}
    	for(map<int,RES>::iterator it=res.begin();it!=res.end();it++){
    		if(it->second.member>2 && it->second.sumtime/2 > k){
    			re[it->second.name]=it->second.member;
    		}
    	}
    	int size=re.size();
    	printf("%d
    ",size);
    	for(map<string,int>::iterator it=re.begin();it!=re.end();it++){
    		printf("%s %d
    ",it->first.c_str(),it->second);
    	} 
    	return 0;
    }
    

      

      

  • 相关阅读:
    Turn the corner
    全排列的递归算法
    全排列的递归算法
    二分   三分搜索
    二分   三分搜索
    理解 Linux 的硬链接与软链接
    一个 Linux 上分析死锁的简单方法
    char能表示(-128~127)
    UNIX网络编程——ioctl 函数的用法详解
    UNIX网络编程——原始套接字(dos攻击)
  • 原文地址:https://www.cnblogs.com/grglym/p/7747925.html
Copyright © 2020-2023  润新知