PAT 天梯赛 L3-003. 社交集群 【并查集】
题目链接
https://www.patest.cn/contests/gplt/L3-003
思路
并查集
用一个 cou[i] 来表示 第 i 门课程 的第一个 感兴趣的人
并的时候
判断 cou[i]
如果 cou[i] 存在 第一个 感兴趣的人 那么 将这两人 join 起来
如果 不存在 那么 这门课程 第一个感兴趣的人 就是这个人
然后 最后查找有几个 连通块
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7;
int pre[maxn], cou[maxn];
int find(int x)
{
int r = x;
while (pre[r] != r)
r = pre[r];
int j = x, i;
while (j != r)
{
i = pre[j];
pre[j] = r;
j = i;
}
return r;
}
void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (x != fy)
pre[fx] = fy;
}
bool comp(int x, int y)
{
return x > y;
}
int main()
{
CLR(cou);
int n, k, num;
cin >> n;
for (int i = 1; i <= n; i++)
pre[i] = i;
for (int i = 1; i <= n; i++)
{
scanf("%d:", &k);
for (int j = 0; j < k; j++)
{
scanf("%d", &num);
if (cou[num])
join(i, cou[num]);
else
cou[num] = i;
}
}
map <int, int> m;
for (int i = 1; i <= n; i++)
m[find(i)]++;
vector <int> v;
map <int, int>::iterator it;
for (it = m.begin(); it != m.end(); it++)
v.push_back(it -> second);
sort (v.begin(), v.end(), comp);
int len = v.size();
cout << len << endl;
for (int i = 0; i < len; i++)
{
if (i)
printf(" ");
printf("%d", v[i]);
}
cout << endl;
}
错误思路
用一个结构体 来保存 社交集群
第一个人 自然就是 第一个社交集群
结构体中 保存 人数 以及 里面所有人感兴趣的课程
然后 从第二个人开始 就开始 在已经有的社交集群里面查找 自己的感兴趣的课程 是否 已经有的社交集群中已经存在 如果有 那么 就加入
如果没有 自己就新建一个 社交集群
但是这样有一个问题
就是 后面查找的时候
如果一个人 同时 与两个 甚至多个 社交集群 都有同时感兴趣的课程
是要将这两个甚至多个 社交集群 并起来的。。
少了 这一步 操作 所以 代码 只能拿 20分
WA代码
#include <cstdio> //错误思路
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7;
struct Node
{
int id;
int tot;
map <int, int> m;
}temp;
bool comp(Node x, Node y)
{
return x.tot > y.tot;
}
int main()
{
vector <Node> v;
map <int, int> vis;
int n, k, num;
cin >> n;
for (int i = 1; i <= n; i++)
{
scanf("%d:", &k);
temp.id = v.size() + 1;
temp.tot = 1;
temp.m.clear();
int flag = 0, opt;
for (int j = 0; j < k; j++)
{
scanf("%d", &num);
if (vis[num])
{
flag = 1;
opt = vis[num];
}
temp.m[num] = 1;
}
if (flag)
v[opt - 1].tot++;
else
{
v.push_back(temp);
opt = temp.id;
}
opt--;
map <int, int>::iterator it;
for (it = temp.m.begin(); it != temp.m.end(); it++)
{
v[opt].m[it -> first] = 1;
vis[it -> first] = v[opt].id;
}
}
sort (v.begin(), v.end(), comp);
cout << v.size() << endl;
vector <Node>::iterator iter;
for (iter = v.begin(); iter != v.end(); iter++)
{
if (iter != v.begin())
printf(" ");
printf("%d", (*iter).tot);
}
cout << endl;
}
PAT 天梯赛 L3-003. 社交集群 【并查集】的更多相关文章
- PAT天梯赛练习题 L2-013 红色警报(并查集+逆序加边)
L2-013. 红色警报 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 战争中保持各个城市间的连通性非常重要.本题要求你编写一 ...
- PAT天梯赛练习题——L3-003. 社交集群(并查集按秩合并)
L3-003. 社交集群 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在社交网络平台注册时,用户通常会输入自己的兴趣爱好, ...
- PAT天梯赛练习 L3-003 社交集群 (30分) DFS搜索
题目分析: 一共有N个编号为1~1000的人,以及一共有编号为1~1000种不同的兴趣,在题目给出1~N编号的人员每个人喜欢的兴趣的id后,要求统计出不同的人员集合的个数以及每个人员几个的人数从大到小 ...
- 天梯L3-003. 社交集群——并查集
在社交网络平台注册时,用户通常会输入自己的兴趣爱好,以便找到和自己兴趣相投的朋友.有部分兴趣相同的人们就形成了“社交集群”.现请你编写程序,找出所有的集群. 输入格式: 输入的第一行给出正整数N(&l ...
- MongoDB之分片集群与复制集
分片集群 1.1.概念 分片集群是将数据存储在多台机器上的操作,主要由查询路由mongos.分片.配置服务器组成. ●查询路由根据配置服务器上的元数据将请求分发到相应的分片上,本身不存储集群的元数据, ...
- 分布式缓存技术redis学习系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)
本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...
- Redis集群(五):集群搭建
一.本文目的 演示在一台机器上搭建3主3从的redis集群,通过演示了解redis集群的搭建,使用和注意事项 二.搭建说明 1.同一台机器搭建3主3从的伪集群 ...
- rabbitMQ集群部署以及集群之间同步
MQ集群部署 期待的部署架构 其中,一个机房有两台机器部署MQ,并组成集群,有一个机房的MQ集群作为中心集群,其他机房的MQ集群将消息同步到中心MQ集群中. 安装erlang,略.. 安装rabbit ...
- 搭建mongodb集群(副本集+分片)
搭建mongodb集群(副本集+分片) 转载自:http://blog.csdn.net/bluejoe2000/article/details/41323051 完整的搭建mongodb集群(副本集 ...
- 分布式缓存技术redis学习(四)——redis高级应用(集群搭建、集群分区原理、集群操作)
本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...
随机推荐
- CityEngine结合ArcGIS制作地质体web场景 [转]
CityEnginewebWebWEB地质体 原文地址:http://www.cnblogs.com/esrichina/archive/2012/11/12/2762163.html 当我们想要对地 ...
- 【音乐App】—— Vue-music 项目学习笔记:歌手详情页开发
前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 歌曲列表 歌曲播放 一.子 ...
- 解决使用maven jetty启动后无法加载修改过后的静态资源
jetty模式是不能修改js文件的,比如你现在调试前端js,发现在myeclipse/eclipse的源码里面无法修改文件,点都不让你点,所以,你只能采用一些办法,更改jetty的模式配置. Look ...
- 云数据库 RDS 版怎么创建数据库和账号MySQL 5.7版
若要使用云数据库RDS,您需要在实例中创建数据库和账号.对于MySQL 5.7版本的实例,您需要通过RDS控制台创建一个初始账号,然后可以通过数据管理(DMS)控制台创建和管理数据库.本文将主要介绍在 ...
- HTML字体对应word字体
42磅对应初号. 36磅对应小初. 26磅对应一号. 24磅对应小一号. 22磅对应二号. 18磅对应小二号. 16磅对应三号. 15磅对应小三号. 14磅对应四号. 12磅对应小四号. 10.5磅对 ...
- 面试题 15:链表中倒数第 k 个结点
面试题 15:链表中倒数第 k 个结点 题目:输入一个链表,输出该链表中倒数第 k 个结点.为了符合大多数人的习惯, 本题从 1 开始计数,即链表的尾结点是倒数第一个结点.例如一个有 6 个结点的 链 ...
- vim 模式查找
1. / 正向查找, ?反向查找 2. \v 激活very magic搜索模式,撰写正则表达式更接近于perl的正则表达式,大多数字符不需要进行转义 3. \V 激活noVeryMagic模式,按字符 ...
- 并发insert情况下数据重复插入问题的解决方案
背景介绍 通常我们在接口里要保存一条数据时,会先判断该条记录在数据库里是否存在,如果不存在就插入,如果存在就返回已经存在. 就拿常见的工单来举例 Order order = orderService. ...
- 分布式搜索elasticsearch 环境搭建
1.elasticsearch安装 elasticsearch的安装超级easy,解压即用(要事先安装好java环境). 到官网 http://www.elasticsearch.org下载最新版的 ...
- 有一个长为n的数组A,求满足0≤a≤b<n的A[b]-A[a]的最大值。 给定数组A及它的大小n,请返回最大差值。
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...