题目1446:Head of a Gang(并查集操作)
题目链接: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(并查集操作)的更多相关文章
- 九度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 ...
- ZOJ 3521 Fairy Wars oj错误题目,计算几何,尺取法,排序二叉树,并查集 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3521 ATTENTION:如果用long long 减小误差,这道题只能用 ...
- 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 ...
- hdu4829 带权并查集(题目不错)
题意: Information Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- 【bzoj3674】 可持久化并查集加强版
http://www.lydsy.com/JudgeOnline/problem.php?id=3674 (题目链接) 题意 维护并查集3个操作:合并:回到完成第k个操作后的状态:查询. Soluti ...
- 并查集 Union-Find
并查集能做什么? 1.连接两个对象; 2.查询两个对象是否在一个集合中,或者说两个对象是否是连接在一起的. 并查集有什么应用? 1. Percolation问题. 2. 无向图连通子图个数 3. 最近 ...
- hiho_1066_并查集
题目大意 给出N个操作,每个操作可能为两种类型之一: 1. 认定两个人属于同一个组织 2. 查询两个人是否是同一个组织 要求对于每个操作类型2,给出结果,即查询的两个人是否属于同一个组织.其中,任何人 ...
- 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...
- 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree
题目链接: http://codeforces.com/problemset/problem/698/B http://codeforces.com/problemset/problem/699/D ...
随机推荐
- mysql分页查询语句怎么写?
ref: http://www.dashen100.com/question/500 是用limit函数 取前5条数据 select * from table_name limit 0,5 或者 se ...
- 配置IDEA Scala环境
http://snglw.blog.51cto.com/5832405/1634595
- IE中自定义标签使用自封闭格式引发错误!
最近学习IONIC,其中用到了ion-menu-nav-button,由于标签开始和结尾之间没有内容,所以图省事儿使用自封闭标签的写法: <ion-menu-nav-button class=& ...
- 内存管理 初始化(六)vmalloc_init 及 ioremap
是不是我错了,本想这个函数会如网上所说将进行非连续内存管理的初始化,但是对于2.6.34的ARM架构而言,该函数实际完成的业务非常少. 内存管理的初始化读到此处,我感觉原有的认识存在很大缺陷: (1) ...
- Vue.nextTick和Vue.$nextTick
`Vue.nextTick(callback)`,当数据发生变化,更新后执行回调. `Vue.$nextTick(callback)`,当dom发生变化,更新后执行的回调. 参考原文:http://w ...
- Nginx系列6之-rewirte功能使用案例总结
使用案例1 网站架构如下: 公司部分服务器目前通过二级目录来调度到不同的服务器,如下所示: 按照上述需求,我们此时就配置了两个location,如下所示: [root@localhost nginx] ...
- mysql中,创建包含json数据类型的表?创建json表时候的注意事项?查询json字段中某个key的值?
需求描述: 在mysql数据库中,创建包含json数据类型的表.记录下,在创建的过程中,需要注意的问题. 操作过程: 1.通过以下的语句,创建包含json数据类型的表 mysql> create ...
- web -- 前端访问后台跨区问题解决
package com.xindatai.ibs.web.filter; import java.io.IOException; import javax.servlet.Filter; import ...
- Winform判断EventHandler是否已经添加
斜体部分替换成自己需要的 private bool HasValueChangedEventHandler(DateTimePicker b) { FieldInfo f1 = typeof(Date ...
- 后端判断用户是否关闭浏览器(关闭网站相关的全部tab)
一)程序步骤 1.js 写一个定时请求后端(php),后端接收到请求到,把当前时间戳写入文件 2.php 阻塞,这里我写的是 30 秒,也就是 sleep(30) 3.获取当前时间和文件里的时间作比较 ...