题目链接: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. 各大引擎矩阵的矩阵存储方式 ----行矩阵 or 列矩阵

    OpenGL  里面的矩阵 float  m[16]; OpenGL中的矩阵是这样的 m[0] m[4] m[8] m[12] m[1] m[5] m[9] m[13] m[2] m[6] m[10] ...

  2. wine for MacOS

    Installing Wine on Mac This tutorial is for intermediate users who want to install and use Wine on t ...

  3. 带你玩转Eclipse项目转成AndroidStudio项目

    随着Android对Eclipse开发工具的淘汰,越来越多的公司使用AndroidStudio进行相应的Android开发工作.如此,原来用Eclipse开发的项目,怎么导入到AndroidStudi ...

  4. 如何使用 URLOpenStream 函数

    URLOpenStream 和 URLDownloadToFile 类似, 都是下载文件的 COM 函数; 前者是下载到 IStream 流, 后者是直接下载到指定路径; 不如后者使用方便. 它们都声 ...

  5. make screenshot at Eclipse

    In Eclipse, from the Window menu, select Open Perspective > Other... > DDMS. Select the Kindle ...

  6. Python爬虫-豆瓣电影 Top 250

    爬取的网页地址为:https://movie.douban.com/top250 打开网页后,可观察到:TOP250的电影被分成了10个页面来展示,每个页面有25个电影. 那么要爬取所有电影的信息,就 ...

  7. java的final关键字——修饰变量

    final修饰的变量不可变,指的是引用不可变,(除基本类型)而不是内容. final修饰的成员变量必须被初始化

  8. 【转】细谈Redis和Memcached的区别

    Redis的作者Salvatore Sanfilippo曾经对这两种基于内存的数据存储系统进行过比较: Redis支持服务器端的数据操作:Redis相比Memcached来说,拥有更多的数据结构和并支 ...

  9. mysql中使用show variables同时查询多个参数值?show variables的使用?

    需求描述: 今天在查mysq关于连接数的问题,想要通过一个show variables命令同时查出来多个值.在此记录下. 操作过程: 1.通过show variables语句的like可以匹配多个值或 ...

  10. 第二种方式,修改python unittest的执行顺序,使用猴子补丁

    1.按照测试用例的上下顺序,而不是按方法的名称的字母顺序来执行测试用例. 之前的文章链接 python修改python unittest的运行顺序 之前写的,不是猴子补丁,而是要把Test用例的类名传 ...