题目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 ...
随机推荐
- 【转】在64位windows下使用instsrv.exe和srvany.exe创建windows服务
本文转自:https://www.iflym.com/index.php/computer-use/201205020001.html 在32位的windows下,包括windows7,windows ...
- Jedis客户端操作redis缓存命令详解
1.对value操作的命令 exists(key):确认一个key是否存在 del(key):删除一个key type(key):返回值的类型 keys(pattern):返回满足给定pattern的 ...
- .NET Best Practices
Before starting with best practices tobe followed, it is good to have clear understanding of how mem ...
- Redhat 5.6(RHEL 5.6)下安装PostgreSQL9.3
1,下载Respository的更新包 http://yum.postgresql.org/9.3/redhat/rhel-5-x86_64/pgdg-redhat93-9.3-1.noarch.rp ...
- log4j MDC用户操作日志追踪配置
一.MDC介绍 MDC(Mapped Diagnostic Context,映射调试上下文)是 log4j 和 logback 提供的一种方便在多线程条件下记录日志的功能.某些应用程序采用多线程的方式 ...
- Xcode文件被锁定:The file ".xcodeproj" could not be unlocked
同事从svn上面checkout项目到本地,通过xcode打开的时候提示的这个问题. The file "xcodeproj" could not be unlocked. Cou ...
- web -- 前端访问后台跨区问题解决
package com.xindatai.ibs.web.filter; import java.io.IOException; import javax.servlet.Filter; import ...
- NetBpm 安装篇(1)
尊重别人劳动成果 转载注明出处:http://www.cnblogs.com/anbylau2130/p/3875718.html 官方主页 http://www.netbpm.org/docs/in ...
- gem install cocoapods ERROR: While executing gem ... (Gem::FilePermissionError)
在cocoapods 执行 sudo gem install cocoapods 的时候出现 While executing gem ... (Gem::FilePermissionError) ...
- windows本地hash值获取和破解详解
powershell版的procdump https://www.t00ls.net/articles-48428.html procdump procdump是微软官方提供的一个小工具, 微软官方下 ...