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高级应用( ...
随机推荐
- 实现页面切换(动画效果实现,不用ViewPager)
源代码地址 http://download.csdn.net/detail/u013210620/8791687 先看主页面布局activity_main <?xml version=" ...
- Redis 架构设计
1.设计层面 (1) 存储小而热的数据 (2) 结合业务数据特点,正确使用内存类型 (3) 冷.热数据分离 2.架构层面 (1) 提前做好容量(内存)规划 (2) 结合持久化模式优劣正确使用,一般建议 ...
- java8新特性学习笔记(二) 使用流(各种API)
筛选和切片 用谓词筛选,筛选出各个不相同的元素,忽略流中的头几个元素,或将流截断至指定长度 用谓词筛选 Stream接口支持filter方法,该操作接受一个谓词(返回一个boolean的函数) 作为参 ...
- imagemagick imagick
imagemagick#图像处理软件 安装解压 ./configure make make install imagick#是php图像扩展模块 调用imagemagick处理图像 安装解压/opt/ ...
- C 语言经典100例
C 语言经典100例 C 语言练习实例1 C 语言练习实例2 C 语言练习实例3 C 语言练习实例4 C 语言练习实例5 C 语言练习实例6 C 语言练习实例7 C 语言练习实例8 C 语言练习实例9 ...
- 【LeetCode】Partition List ——链表排序问题
[题目] Given a linked list and a value x, partition it such that all nodes less than x come before nod ...
- 设计模式之MVC设计模式初阶
MVC M:Model(数据) V:View(界面) C:Control(控制) 1⃣️Control可以直接访问View和Model 2⃣️View不可以拥有Control和Model属性,降低耦合 ...
- 开发ActiveX控件调用另一个ActiveX系列1——开发一个MFC ActiveX控件
ActiveX开发的教程有很多,我也从中受益匪浅,例如以下这几篇: 基本教程:http://www.cnblogs.com/guenli/articles/1629915.html 注意事项:http ...
- 扒一扒P2P风控的底牌(转)
互联网金融,这里面水就太深了,能当理财买的一般有两类,一个是货币基金,比如余额宝,这个大家已经十分清楚了,没什么风险, 但问题就是收益越来越低.实在是不过瘾了.而另外一种就是P2P理财了,收益很高,也 ...
- linux环境tomcat配置及hadoop 2.6伪分布模式安装配置
一.ubuntu 15.04.openjdk1.7.tomcat7环境配置 1. 配置openjdk1.7,输入命令: -jdk 2. 查看java是否安装成功,输入命令: envjava -vers ...