题目链接

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. 社交集群 【并查集】的更多相关文章

  1. PAT天梯赛练习题 L2-013 红色警报(并查集+逆序加边)

    L2-013. 红色警报 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 战争中保持各个城市间的连通性非常重要.本题要求你编写一 ...

  2. PAT天梯赛练习题——L3-003. 社交集群(并查集按秩合并)

    L3-003. 社交集群 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在社交网络平台注册时,用户通常会输入自己的兴趣爱好, ...

  3. PAT天梯赛练习 L3-003 社交集群 (30分) DFS搜索

    题目分析: 一共有N个编号为1~1000的人,以及一共有编号为1~1000种不同的兴趣,在题目给出1~N编号的人员每个人喜欢的兴趣的id后,要求统计出不同的人员集合的个数以及每个人员几个的人数从大到小 ...

  4. 天梯L3-003. 社交集群——并查集

    在社交网络平台注册时,用户通常会输入自己的兴趣爱好,以便找到和自己兴趣相投的朋友.有部分兴趣相同的人们就形成了“社交集群”.现请你编写程序,找出所有的集群. 输入格式: 输入的第一行给出正整数N(&l ...

  5. MongoDB之分片集群与复制集

    分片集群 1.1.概念 分片集群是将数据存储在多台机器上的操作,主要由查询路由mongos.分片.配置服务器组成. ●查询路由根据配置服务器上的元数据将请求分发到相应的分片上,本身不存储集群的元数据, ...

  6. 分布式缓存技术redis学习系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

  7. Redis集群(五):集群搭建

    一.本文目的        演示在一台机器上搭建3主3从的redis集群,通过演示了解redis集群的搭建,使用和注意事项     二.搭建说明        1.同一台机器搭建3主3从的伪集群   ...

  8. rabbitMQ集群部署以及集群之间同步

    MQ集群部署 期待的部署架构 其中,一个机房有两台机器部署MQ,并组成集群,有一个机房的MQ集群作为中心集群,其他机房的MQ集群将消息同步到中心MQ集群中. 安装erlang,略.. 安装rabbit ...

  9. 搭建mongodb集群(副本集+分片)

    搭建mongodb集群(副本集+分片) 转载自:http://blog.csdn.net/bluejoe2000/article/details/41323051 完整的搭建mongodb集群(副本集 ...

  10. 分布式缓存技术redis学习(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

随机推荐

  1. eos wasm虚拟机相关接口定义实现

    wasm虚拟机相关接口定义实现 执行流程 controller::push_transaction()  // 事务 -> transaction_context::exec()  // 事务 ...

  2. sprinvmvc整合swagger实现实时接口信息展示

    1.pom.xml引入swagger插件 <dependency> <groupId>io.springfox</groupId> <artifactId&g ...

  3. apache默认路径

    读启动文件 /etc/inid.d/httpd 默认web路径 /var/www/html inux下Apache PHP MYSQL 默认安装路径 apache:如果采用RPM包安装,安装路径应在 ...

  4. [Android]egit取消文件版本号控制

    开发项目,多人合作开发变得越来越重要了,在此同一时候,使用git作为协同工具也是越来越多.在此.介绍一下egit取消文件版本号控制的方法. (egit即为eclipse中的git插件) 1.打开Nav ...

  5. UVA12096 - The SetStack Computer(set + map映射)

    UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来, ...

  6. Html5上传插件封装

    前段时间将flash的上传控件替换成使用纯js实现的,在此记录 1.创建标签 <div class="camera-area" style="display:inl ...

  7. 【转载】教你使用 Reflexil 反编译.NET

    简介 反编译的方式有很多种,其实最靠谱的还是IL反编译. 如果不懂IL可以尝试我这边文章入门:http://www.wxzzz.com/278.html 不过我下面要说的不是IL这种底层的代码反编译, ...

  8. 淘宝数据库OceanBase SQL编译器部分 源代码阅读--解析SQL语法树

    OceanBase是阿里巴巴集团自主研发的可扩展的关系型数据库,实现了跨行跨表的事务,支持数千亿条记录.数百TB数据上的SQL操作. 在阿里巴巴集团下,OceanBase数据库支持了多个重要业务的数据 ...

  9. (webstorm的css编写插件)Emmet:HTML/CSS代码快速编写神器

    Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...

  10. ServletContext读取配置文件

    package servlet; import java.io.FileInputStream;import java.io.IOException;import java.io.InputStrea ...