题目地址: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 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
来源:
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 >> 1
gang[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;
}

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

九度OJ 1446 Head of a Gang -- 并查集的更多相关文章

  1. 九度OJ小结

    1. 高精度问题 可参考题目 题目1137:浮点数加法   http://ac.jobdu.com/problem.php?pid=1137 对于高精度问题可以考虑使用结构体.上述为浮点数加法,因此该 ...

  2. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  3. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  4. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  5. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  6. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  7. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

  8. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

  9. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

随机推荐

  1. Java基础学习中一些词语和语句的使用

    在Java基础学习中,我们刚接触Java会遇到一些词和语句的使用不清的情况,不能很清楚的理解它的运行效果会是怎么样的,如:break,continue在程序中运行效果及跳转位置, 1.先来看看brea ...

  2. 【翻译自mos文章】DBA_JOBS 和 DBA_JOBS_RUNNING 不同的结果的解释

    DBA_JOBS 和 DBA_JOBS_RUNNING 不同的结果 參考原文: Different Results from DBA_JOBS and DBA_JOBS_RUNNING (Doc ID ...

  3. [Practical Git] Filter commit history with git log arguments

    In the last lesson, we learned how to format the git log output; in this lesson we will learn how to ...

  4. 【问题汇总】ListView的FooterView设置可见性的问题

    ListView的FooterView一般用来给用户展示一些提示信息. 正常情况下,是这么使用的.代码例如以下: // footer footerLayout = new PullLoadingLay ...

  5. android学习日记05--Activity间的跳转Intent实现

    Activity间的跳转 Android中的Activity就是Android应用与用户的接口,所以了解Activity间的跳转还是必要的.在 Android 中,不同的 Activity 实例可能运 ...

  6. [WebGL入门]二,開始WebGL之前,先了解一下canvas

    年2月)HTML5依旧处于草案阶段. HTML5支持网页端的多媒体功能和画布功能,追加了非常多全新的更合理的Tag标签.各个浏览器也都在逐渐的完好这些新的特性. Canvas对象表示一个 HTML画布 ...

  7. air写文件 SecurityError: fileWriteResource 时报错的解决方法

    用 File.applicationDerectoryPath.resolv("text.txt")会报SecuriyError错误! 解决: var _Path:File = F ...

  8. insert erase和replace的例子

    9.43 编写一个函数,接受三个string参数s.oldVal和newVal.使用迭代器及insert和erase函数将s中所有oldval替换为newVal.测试程序,用它替换通用的简写形式,如, ...

  9. (原)C++解析XML生成类对象_v1.0 函数指针

    要写一个xml解析,解析后获得到的数据变成各个类的对象. 解析有现成的库,使用tinyxml,但是解析出来的类库如何变成各个类的对象, 例如一下这个xml, <musics> <mu ...

  10. 项目源码--Android高质量图片浏览器源码

      下载源码   技术要点: 1. 浏览所有格式的图片 2. 图片缓存到数据库 3. Sqlite数据库的高级应用 4. 文件夹缩图显示 5. 多点触控技术 6. 动画技术 7. 支持超高清图片 8. ...