题目链接:http://ac.jobdu.com/problem.php?pid=1446

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
// 1446 Head of a Gang.cpp
// Jobdu
//
// Created by PengFei_Zheng on 17/04/2017.
// Copyright © 2017 PengFei_Zheng. All rights reserved.
// #include <stdio.h>
#include <map>
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include <list>
#define maxn 1010
using namespace std; int parent[maxn];
int n, k, i;
int currNum;
map<string, int> baseMap; char marray[maxn][];
char resultArr[maxn][];
int callTime[maxn];
int singleCallTime[maxn];
int fatherArr[maxn];
int visit[maxn]; struct Node {
char name[];
int size;
} nodes[maxn]; //并查集寻找父亲节点
int findRoot(int x) {
while (parent[x] != x) {
x = parent[x];
}
return x;
} //并查集合并节点
void unionSet(int a, int b) { a = findRoot(a);
b = findRoot(b);
if (a == b) return;
if (a > b) {
parent[a] = b;
} else {
parent[b] = a;
}
} //初始化基本数据,这在以后会用到
void initData() {
baseMap.clear();
memset(callTime, , sizeof(callTime));
memset(visit, , sizeof(visit));
memset(singleCallTime, , sizeof(singleCallTime));
memset(fatherArr, , sizeof(fatherArr));
for (i = ; i < maxn; i++) {
parent[i] = i;
}
} //将字符串转为数字,否则后面不太好处理
int getCurrentNum(char c[]) {
int num = ;
map<string, int>::iterator it = baseMap.find(c);
if (it == baseMap.end()) {
currNum++;
num = currNum;
baseMap.insert(make_pair(c, num));
} else {
num = it->second;
}
return num;
} //记录每个人打电话的时间
void constructData(char a[], char b[], int anum, int bnum, int d) {
if (singleCallTime[anum] == ) {
singleCallTime[anum] = d;
} else {
singleCallTime[anum] += d;
}
strcpy(marray[anum], a);
strcpy(marray[bnum], b);
callTime[anum] += d;
callTime[bnum] += d;
} bool cmp(Node node1, Node node2) {
return strcmp(node1.name, node2.name) < ;
} int main() {
while (scanf("%d%d", &n, &k) != EOF) {
initData();
currNum = ;
char a[];
char b[];
int d;
for (i = ; i < n; i++) {
scanf("%s%s%d", a, b, &d);
int anum = getCurrentNum(a);
int bnum = getCurrentNum(b);
constructData(a, b, anum, bnum, d);
unionSet(anum, bnum);
} int tmpk = ;
for (i = ; i < currNum + ; i++) {
parent[i] = findRoot(i);
if (parent[i] == i) {
fatherArr[tmpk] = i;
tmpk++;
}
}
int num = ;
for (i = ; i < tmpk; i++) {
int size = ;
int allTime = ;
int maxTime = -;
int maxMem = ;
for (int j = ; j < currNum + ; j++) {
if (fatherArr[i] != && visit[j] == ) {
if (parent[j] == fatherArr[i]) {
size++;
allTime += singleCallTime[j];
if (callTime[j] > maxTime) {
maxTime = callTime[j];
maxMem = j;
}
visit[j] = ;
}
}
}
if (size < ) {
continue;
}
if (allTime <= k) {
continue;
}
strcpy(nodes[num].name, marray[maxMem]);
nodes[num].size = size;
num++;
}
sort(nodes, nodes + num, cmp);
printf("%d\n", num);
for (i = ; i < num; i++) {
printf("%s %d\n", nodes[i].name, nodes[i].size);
} }
return ;
} /**************************************************************
Problem: 1446
User: zpfbuaa
Language: C++
Result: Accepted
Time:30 ms
Memory:1096 kb
****************************************************************/

题目1446:Head of a Gang(并查集操作)的更多相关文章

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

    题目地址:http://ac.jobdu.com/problem.php?pid=1446 题目描述: One way that the police finds the head of a gang ...

  2. ZOJ 3521 Fairy Wars oj错误题目,计算几何,尺取法,排序二叉树,并查集 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3521 ATTENTION:如果用long long 减小误差,这道题只能用 ...

  3. PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

    题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...

  4. hdu4829 带权并查集(题目不错)

    题意: Information Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. 【bzoj3674】 可持久化并查集加强版

    http://www.lydsy.com/JudgeOnline/problem.php?id=3674 (题目链接) 题意 维护并查集3个操作:合并:回到完成第k个操作后的状态:查询. Soluti ...

  6. 并查集 Union-Find

    并查集能做什么? 1.连接两个对象; 2.查询两个对象是否在一个集合中,或者说两个对象是否是连接在一起的. 并查集有什么应用? 1. Percolation问题. 2. 无向图连通子图个数 3. 最近 ...

  7. hiho_1066_并查集

    题目大意 给出N个操作,每个操作可能为两种类型之一: 1. 认定两个人属于同一个组织 2. 查询两个人是否是同一个组织 要求对于每个操作类型2,给出结果,即查询的两个人是否属于同一个组织.其中,任何人 ...

  8. 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany

    先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...

  9. 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree

    题目链接: http://codeforces.com/problemset/problem/698/B http://codeforces.com/problemset/problem/699/D ...

随机推荐

  1. Linux下设置SSH Server设置时间链接限制

    OpenSSH基于安全的理由,如果用户连线到SSH Server后闲置一段时间,SSH Server会在超过特定时间后自动终止SSH连线.本人习惯长时间连接,需要做如下修改: 1.打开ssh配置文件: ...

  2. array_multisort—对多个数组或多维数组进行排序

    From: http://www.cnblogs.com/lwbqqyumidi/archive/2013/01/31/2887188.html PHP中array_multisort可以用来一次对多 ...

  3. 消息队列之 RabbitMQ

    https://www.jianshu.com/p/79ca08116d57 关于消息队列,从前年开始断断续续看了些资料,想写很久了,但一直没腾出空,近来分别碰到几个朋友聊这块的技术选型,是时候把这块 ...

  4. 设置回车的默认按钮detectEnter

    场景: 页面有一个搜索文本框和搜索按钮.正常情况下,当我在搜索文本框输入关键字后按回车键就可以触发搜索按钮进行内容搜索,但由于页面上还有其它按钮,而且默认不是搜索按钮,怎样才能实现回车就触发我们的搜索 ...

  5. 安装oh my zsh和git插件

    http://macshuo.com/?p=676   --安装oh my zsh http://www.jianshu.com/p/9189eac3e52d https://github.com/r ...

  6. 【PCA】

    http://blog.csdn.net/xiaojidan2011/article/details/11595869 非常清楚 核心部分解释:主成份用于降纬,通过线型变换,从高纬度映射到低纬度,其中 ...

  7. MysqlHelper使用反射机制智能推算数据类型以及属性名称

    public class MySqlHelper { private string ConnString; public MySqlHelper(string connectionString) { ...

  8. java字符集

    在utf-8编码中,unicode(编码字符集)是utf-8(字符编码)的表现形式 http://www.cnblogs.com/hanruyue/p/5859107.html

  9. c++静态全局,局部变量---18

    原创博文,转载请标明出处--周学伟  http://www.cnblogs.com/zxouxuewei/ static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static. ...

  10. Mac OS 电信3G上网设置

    打开客户端后(安装客户端mobile partner需要先安装jdk),在“系统偏好设置”里选择“网络”,网络左侧添加“huaweimobile-modem”,“电话号码”填写电信卡号,“账户名称”和 ...