题目链接

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. 如何查看linux版本 如何查看LINUX是多少位

    一.如何得知自己正在使用的linux是什么版本呢,下面的几种方法将给你带来答案! 1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux ...

  2. 转: 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?

    from: https://github.com/RubyLouvre/agate/issues/8 今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以 ...

  3. 使用eclipse搭建maven多module项目(构建父子项目)

    创建空maven项目 File–>new–>project… 2.next 3.next 4.finish 5.配置pom.xml <project xmlns="http ...

  4. HTML5 Support In Visual Studio 2010

    最近HTML5浪潮已经开始了,VS2010已经有一个扩展支持在HTML5智能提示.你可以从这里下载这个扩展: http://visualstudiogallery.msdn.microsoft.com ...

  5. bottle的几个小坑

    距离我在<web.py应用工具库:webpyext>里说要换用bottle,已经过去快两个月了--事实上在那之前我已经開始着手在换了.眼下那个用于 Backbone.js 介绍的样例程序已 ...

  6. 导出数据生成Excel(MVC)

    /// <summary> /// 生成Excel /// </summary> /// <returns></returns> public File ...

  7. Lua学习五----------Lua循环

    © 版权声明:本文为博主原创文章,转载请注明出处 1.循环类型 1.1 while循环 - 语法:while(condition) do ...<执行语句> end - 解析:判断cond ...

  8. 【Servlet与JSP】请求转发与重定向

    假设一个登录系统,要求用户输入用户名和密码: 用户在上面表单当中输入了信息之后,点击登录按钮(type="submit")将表单作为请求参数进行提交. 这一提交就有两种形式:get ...

  9. apt-mirror 校验错误文件处理

    apt-mirror是一个用来将Debian或Ubuntu的软件源镜像到本地的工具,这个工具工作得非常好,不过有的时候由于网络问题,会有一些文件的校验是失败的,但apt-mirror并不能发现,等到最 ...

  10. ASP.NET数据库连接字符串的加密与解密

    ASP.NET web.config中,数据库连接字符串的加密与解密. 虽然不怎么新鲜,但相信还是有许多人不知道,好,不说废话,直接给方法:开始--->运行,输入cmd,接着输入以下内容 加密: ...