九度OJ 1446 Head of a Gang -- 并查集

来源:互联网 发布:淘宝上买气排会怎么样 编辑:程序博客网 时间:2024/05/17 22:03

题目地址:http://ac.jobdu.com/problem.php?pid=1446

题目描述:

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 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 108 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
样例输出:
2AAA 3GGG 30
来源:
2012年浙江大学计算机及软件工程研究生机试真题


#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct node{char name[4];  //人名int time;      //通话时间int father;    //父节点int rank;      //秩}Node;typedef struct head{char name[4];  //头领姓名int num;       //人数}Head;Node people[1010];Head gang[1010];   //头领int num;           //总人数int FindSet (int x){//查询节点属于那个集合if (people[x].father != x){people[x].father = FindSet (people[x].father);}return people[x].father;}void Union (int x, int y){//合并两节点x = FindSet (x);y = FindSet (y);if (x == y)return;if (people[x].rank > people[y].rank){people[y].father = x;people[x].rank += people[y].rank;}else{if (people[x].rank == people[y].rank){++people[y].rank;}people[x].father = y;}}int compare1 (const void * p, const void * q){Node * p1 = (Node *)p;Node * q1 = (Node *)q;return p1->father - q1->father;}int compare2 (const void * p, const void * q){Head * p1 = (Head *)p;Head * q1 = (Head *)q;return strcmp(p1->name, q1->name);}int main(void){int N;int K;char name1[4], name2[4];int time;int i, j, k;int pre;int sum;       //总的通话时间while (scanf ("%d%d", &N, &K) != EOF){num = 0;while (N-- != 0){scanf ("%s%s%d", name1, name2, &time);/////////////////////////////////////////////////////////////////////查询输入姓名是否已在表中,如果不在,添加进表;否则,更新节点信息for (i=0; i<num; ++i){if (strcmp (people[i].name, name1) == 0){break;}}if (i == num){strcpy (people[i].name, name1);people[i].time = time;people[i].father = i;people[i].rank = 0;++num;}else{people[i].time += time;}///////////////////////////////////////////////for (j=0; j<num; ++j){if (strcmp (people[j].name, name2) == 0){break;}}if (j == num){strcpy (people[j].name, name2);people[j].time = time;people[j].father = j;people[j].rank = 0;++num;}else{people[j].time += time;}///////////////////////////////////////////////////////////////////Union (i, j);//合并节点}//最后一次更新各个集合for (i=0; i<num; ++i){FindSet (i);}//按父节点大小排序qsort (people, num, sizeof(Node), compare1);i = j = k = 0;//找到各个集合的头领及人数while (i < num){strcpy(gang[k].name, people[i].name);pre = i;++j;sum = people[i].time;while ((j < num) && (people[j].father == people[i].father)){if (people[j].time > people[pre].time){strcpy (gang[k].name, people[j].name);pre = j;}sum += people[j].time;++j;}if (j - i > 2 && (sum >> 1) > K){//每个时间在每个人处都加了一遍(即各个时间被加了两遍),所以sum >> 1gang[k].num = j - i;++k;}i = j;}printf ("%d\n", k);qsort (gang, k, sizeof(Head), compare2);for (i=0; i<k; ++i){printf ("%s %d\n", gang[i].name, gang[i].num);}}return 0;}

参考资料:并查集 -- 学习详解

0 0
原创粉丝点击