题目链接

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. 如何部署和运行Scut服务器及游戏:Windows篇

    概述 Scut游戏引擎是一个永久免费的全脚本游戏服务器框架,采用MVC框架设计,简化数据库设计和编码工作:降低对开发人员的开发难度:同时提供了丰富的类库和API接口. 一.    安装环境 必须安装的 ...

  2. 常用 linux操作

    查看libreoffice进程 ps -ef | grep libreoffice

  3. EasyMvc入门教程-基本控件说明(2)定时器

    我们有时候希望系统能自动刷新后台数据或者做某个动作,那么定时器的作用就体现出来了. EasyMvc默认提供的服务器可以定时调前端方法与定时调后端方法,先看例子: 定时调客户端事件 实现代码: @Htm ...

  4. java基础篇5之泛型

    1 泛型的基本应用 //反射方式 指定类型,就不用强转 Construcctor<String> constructor = String.class.getConstructor(Str ...

  5. ClassPathXmlApplicationContext和FileSystemXmlApplicationContext区别

    ClassPathXmlApplicationContext 默认文件路径是src下那一级classpath:和classpath*:的区别: classpath: 只能加载一个配置文件,如果配置了多 ...

  6. linux中read用法

    read在while中的经常用法: root@ubuntu:/var/lib/logrotate :: # cat /etc/cron.daily/logrotate #!/bin/sh # Clea ...

  7. .NET创建宿主设计器--DesignHost、DesignSurface.

    一个窗口在运行时,是这样的: 但是,在设计时,却远比这复杂的多,它需要一个设计器对象:它仅存在于设计时,并连接到运行时存在的对象.   宿主容器 我们可以看到每个窗体和按钮均有与之相关的设计器.这两个 ...

  8. Android 基于Retrofit+Rxjava搭建的简单易用的网络架构

    装逼開始之前,为大家推荐两篇文章,用来更好的学习Retrofit和Rxjava. 在这里我们要感谢互联网装逼行业勇于献身,甘于奉献的的大婶们. 我仅代表个人,给您们跪舔了. Retrofit:Retr ...

  9. 席位分配问题——惯例Q值法和d&#39;hondt法的MATLAB程序

      本篇博文为追忆以前写过的算法系列第四篇 温故知新 本篇于2009年发表于百度博客,当时还没接触CSDN.所以是文学和技术博客混淆,只是这个程序博文訪问量突破2000,有不少网友评论互动.应该 ...

  10. js:string转int

    http://blog.csdn.net/leidengyan/article/details/5503594 <script>     var   str='1250' ;   aler ...