使用 图论 解决PAT甲级1107 Social Clusters
题目
When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of their hobbies in common. You are supposed to find all the clusters.
Input Specification:
Each input file contains one test case. For each test case, the first line contains a positive integer N (≤1000), the total number of people in a social network. Hence the people are numbered from 1 to N. Then N lines follow, each gives the hobby list of a person in the format:
Ki: hi[1] hi[2] ... hi[Ki]
where Ki (>0) is the number of hobbies, and hi[j] is the index of the j-th hobby, which is an integer in [1, 1000].
Output Specification:
For each case, print in one line the total number of clusters in the network. Then in the second line, print the numbers of people in the clusters in non-increasing order. The numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
8
3: 2 7 10
1: 4
2: 5 3
1: 4
1: 3
1: 4
4: 6 8 1 5
1: 4
Sample Output:
3
4 3 1
题目大意
有k人分别有不同的爱好,找到爱好,找到有爱好交集的社交圈的个数以及对应得人数。
思路
可以先构建一个人员与爱好的映射表,然后通过DFS找到对应的社交圈。
先定义映射表为G,记录用户是否已经进入社交圈数组vis。
最主要的部分是如何找到社交圈里的人员。从第一个可访问的用户进入,找到该用户所有的爱好,再找出所有拥有该爱好的人,再对这部分进行上述操作。
void DFS(int n) {vis[n] = true; num++;//遍历n号用户所有的爱好for (int i = 0; i < 1010; i++) {if (G[n][i] == 1) {//遍历所有i爱好的用户for (int m = 0; m < 1010; m++) {//m号用户有该爱好 且 没有进入社交圈if (G[m][i] == 1 && vis[m] == false)DFS(m);}}}
}
代码
#include<bits/stdc++.h>
using namespace std;int n;
int G[1010][1010];
bool vis[1010] = { false };
int num = 0;
vector<int> res;void DFS(int n) {vis[n] = true; num++;for (int i = 0; i < 1010; i++) {if (G[n][i] == 1) {for (int m = 0; m < 1010; m++) {if (G[m][i] == 1 && vis[m] == false)DFS(m);}}}
}void tracert() {for (int i = 0; i < n; i++) {if (vis[i] == false) {DFS(i);res.push_back(num);num = 0;}}
}bool cmp(int a, int b) {return a > b;
}int main() {scanf("%d", &n);for (int i = 0; i < n; i++) {int t;scanf("%d: ", &t);for (int m = 0; m < t; m++) {int q;scanf("%d", &q);G[i][q] = 1;}}tracert();cout << res.size() << endl;sort(res.begin(), res.end(), cmp);for (int i = 0; i < res.size(); i++) {if (i != 0) cout << " ";cout << res[i];}}
使用 图论 解决PAT甲级1107 Social Clusters相关推荐
- PAT甲级1107 Social Clusters (30 分):[C++题解]并查集,爱好、人数
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 凭爱好,分人群.注意点:爱好可传递.什么意思?意思是A和B的有共同爱好1, B和C有共同爱好2,那么认为A和C也是同一群人. 按照爱 ...
- PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
题目链接:点击打开链接 题目大意:一个"社交集群"是指部分兴趣爱好相同的人的集合.你需要找出所有的社交集群. 解题思路:并查集思路,轮到第几个人时,以它的 id 为 root,然后 ...
- 1107 Social Clusters
这道题目给出的示例如上图所示,一共有1-8这8个人(圆形),他们拥有1-10这10个兴趣爱好(方形),一人可以有多个爱好,拥有共同爱好的人被视为一个社区的.现在给出每个人的爱好情况,求出社区的个数,并 ...
- 1107 Social Clusters (30 分)【难度: 中 / 知识点: 并查集】
https://pintia.cn/problem-sets/994805342720868352/problems/994805361586847744 很不错的一个并查集的操作.你会发现我们需要处 ...
- 刷PAT甲级的各题思路、细节以及遇到的问题记录
1001 A+B Format (20分) 因为一定会用到字符串,而string非常好用,但是用的时候一定要注意不能越界访问,否则会在运行时出现abort() has been called. 100 ...
- PAT甲级1139 First Contact (30 分):[C++题解] 图论、暴力枚举两个点、hash映射
文章目录 题目分析 题目链接 题目分析 来源:acwing 题目分析: 图论模拟题. 给定暗恋的两个人A 和B,需要寻找一对C 和D ,满足:A和C是朋友,C和D是朋友,D和B是朋友.而且A.C同性别 ...
- PAT A1107 Social Clusters ——过尽千帆皆不是
PAT A1107 Social Clusters 知道应该是并查集,但是忘了并查集怎么写,于是先用了俩二维数组存储遍历试了一下,只能得20分,原因是第三层for循环找到爱好对应的人加入之后,就不再管 ...
- PAT甲级1076 Forwards on Weibo (30 分) :[C++题解]图论、bfs
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: BFS如何搜前k层?统计前k层的点数. ac代码 #include<bits/stdc++.h> using names ...
- PAT甲级1154 Vertex Coloring :[C++题解]图论、模拟、结构体存边
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 这题的边不用邻接矩阵.邻接表来存,仅仅用结构体来存即可,结构体正好存边的两个端点,我们只要遍历每条边,判断端点的颜色是否相同即可. ...
最新文章
- leetcode-135 分发糖果
- 唐杰:AI 未来靠迭代
- 如何发表cscd核心论文_新手如何发表论文
- ubuntu 一个好系统
- Linux下防御/减轻DDOS***的方法
- windows下进程间通信的(13种方法)
- python csv文件写入失败_python解析csv文件失败
- c++内存分配的方式
- MySQL配置(二)
- cocos2d-x 日志...
- list删除重复元素
- 计算机第二道启动密码怎么设置,电脑一道密码怎么设置
- java Calendar的学习分享
- android studio unable to save settings,记一些:Android Studio 安装-运行 异常及解决
- Android游戏编程之从零开始pdf
- 学会这招,从此解决被知乎封号烦恼
- 声音特征提取方法:综述【线性声谱图(Line Spectrum)、对数梅尔谱(Log-mel)、梅尔频率倒谱系数(MFCCs)】
- vue 组件名称错误
- 云计算 - 虚拟化技术 - 总结
- Flash Magic使用