并查集模拟_1034 Head of a Gang (30 分)

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

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

 题目中要求找出总联系时间大于k,并且联系的圈子人数大于2人的圈子,输出圈子中联系时间最长的人以及圈子的人数,按照圈子人数联系时间最长的人姓名排序

首先可以想到,将相互联系的人建成一棵树,并且联系时间最长的要放在树根,那么最简单的就是使用并查集来实现了,只能实现离线操作,因为如果是在线操作,整棵树上可以找到相应树的父亲节点,但是找不到当前节点的儿子节点,无法更新根部(联系时间最长的节点),

尽量将联系时间长的人放在根部,那么他可以在Union函数中实现,即判断两个节点的大小关系,在更新per函数即可,实现还算是比较简单的,只是有种样例考虑了很久

3 50
aa bb 20
bb cc 20
cc dd 20

这种情况下,aa节点权值为40,bb权值40,cc权值40,所以最后到底是输出aa、bb、还是cc呢?我的程序输出是cc,测试样例中应该是没有这种样例,因为总是感觉输出aa会比较好一些

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f

using namespace std;
typedef long long ll;
const int maxn = 2010;
int per[maxn],num[maxn];
map names;
queue > que;
string _next[maxn];

int n,k;
struct people
{
    string name;
    int lenth;
}peo[maxn];


void Init()
{
    for(int i = 1;i <= 2*n;i ++) per[i] = i;
}
int Find(int x)
{
    return  x == per[x] ? x:per[x] = Find(per[x]);
}

void Union(int x,int y)
{
    x = Find(x);
    y = Find(y);
    if(x != y)
    {
        if(peo[x].lenth > peo[y].lenth)
            per[y] = x;
        else
            per[x] = y;
    }
}

int main()
{
    scanf("%d%d",&n,&k);
    Init();
    getchar();
    string name1,name2;
    int len,index = 0;
    for(int i = 1;i <= n;i ++)
    {
        cin>>name1>>name2>>len;
        if(!names[name1]){
            names[name1] = ++index;
            peo[index].name = name1;
            peo[index].lenth = len;
        }
        else
            peo[names[name1]].lenth += len;
        if(!names[name2]){
            names[name2] = ++index;
            peo[index].name = name2;
            peo[index].lenth = len;
        }
        else
            peo[names[name2]].lenth += len;
        que.push(make_pair(name1,name2));
    }
//    for(int i = 1;i <= index;i ++)
//        cout << peo[i].name<<" "< p;
    while(!que.empty())
    {
        p = que.front();que.pop();
        Union(names[p.first],names[p.second]);
    }
    for(int i = 1;i <= index;i ++)
    {
        int x = Find(i);
        if(per[i] != i)
            peo[x].lenth += peo[i].lenth;
        num[x] ++;
    }
//    for(int i = 1;i <= index;i ++)
//        cout << peo[i].name<<" "< 2 && peo[i].lenth > k*2)
            _next[++pos] = peo[i].name;
    }
    if(pos == 0)
        printf("0\n");
    else{
        printf("%d\n",pos);
        sort(_next+1,_next+pos+1,less());
        for(int i = 1;i <= pos;i ++)
            cout << _next[i] << " "<

 

你可能感兴趣的:(PAT练习,模拟)