1107 Social Clusters (30 分) 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. Y…
题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数(不同俱乐部的两个人的爱好一定不相同). AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; vector<],st; ]; ]; int find_(int…
1107. Social Clusters (30) 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 com…
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人的编号,如果为0则表示未出现. 当前第i个人若也存在爱好k,则只要将i与vis[k]两个人合并即可. 最后father[i]相同的即处在同一个集合中. #include <iostream> #include <cstdio> #include <algorithm> #i…
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 fi…
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 fi…
简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; +; int fa[maxn]; int num[maxn]; int n; vector…
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…
Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L. Now given any weighted tre…
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 fi…
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30 分)   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 …
1107 Social Clusters(30 分) 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. Yo…
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同兴趣的人.你应该找到所有的集群. 输入规格: 每个输入文件包含一个测试用例.对于每个测试用例, 第一行包含一个正整数N(<= 1000),一个社交网络中的总人数.因此,人数从1到N.然后N行跟随,每个给出一个人的爱好列表的格式: Ki:hi [1] hi [2] ... hi [Ki] 其中Ki(>…
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. Y…
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以留在一个桌子. 例如:如果我告诉你A知道B,B知道C,D知道E,所以A,B,C可以留在一个桌子中,D,E必须留在另一个桌子中.所以Ignatius至少需要2个桌子. 思路: 并查集模板题. #include<iostream> using namespace std; ]; int find(in…
Luogu并查集模板题 #include<cstdio> using namespace std; int z,x,y,n,m,father[10001]; int getfather(int v)//找到根节点 { if (father[v]!=v) father[v]=getfather(father[v]);//路径压缩 return father[v]; } void hb(int x,int y) { x=getfather(x); y=getfather(y); father[x]…
POJ-图论-并查集模板 1.init:把每一个元素初始化为一个集合,初始化后每一个元素的父亲节点是它本身,每一个元素的祖先节点也是它本身(也可以根据情况而变). void init() { for (int i = 0; i < n; i++) p[i] = i;//p[i]即为i结点的父亲节点的编号 } 2.find(x) :查找一个元素所在的集合,即找到这个元素所在集合的祖先,判断两个元素是否属于同一集合,只要看他们所在集合的祖先是否相同即可.合并两个集合,也是使一个集合的祖先成为另一个集…
P2978 [USACO10JAN]下午茶时间Tea Time 题目描述 N (1 <= N <= 1000) cows, conveniently numbered 1..N all attend a tea time every day. M (1 <= M <= 2,000) unique pairs of those cows have already met before the first tea time. Pair i of these cows who have…
题目描述 简单的并查集模板 输入描述 第一行包含两个整数N.M,表示共有N个元素和M个操作. 接下来M行,每行包含三个整数Zi.Xi.Yi 当Zi=1时,将Xi与Yi所在的集合合并 当Zi=2时,输出Xi与Yi是否在同一集合内,是的话输出Y:否则话输出N. 分析 简单的模板,解释留到算法微解读 AC代码 #include <bits/stdc++.h> using namespace std; int n,m; int fa[10000+5]; inline int read(){ int X…
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…
并查集的基本应用 #include<bits/stdc++.h> using namespace std; ; vector<int>vec[N]; int p[N]; const int inf=0x3f3f3f3f; }; }; int findth(int x) { if(x==p[x]) return x; return p[x]=findth(p[x]); } void unionn(int x,int y) { int xx=findth(x); int yy=find…
1057 Stack (30 分)   Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element).…
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equ…
1147 Heaps (30 分)   In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or…
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Specification: Each input file contains one test case. Each case starts with a line containing 0…
题意:给出n个人(编号为1~n)以及每个人的若干个爱好,把有一个或多个共同爱好的人归为一个集合,问共有多少个集合,每个集合里有多少个人? 思路:典型的并查集题目.并查集的模板init()函数,union()函数,findSet()函数就不多讲了.这里根据爱好来归类,因此,在读入数据时把爱好进行合并.设置数组hobby[],hobby[id]表示编号为id的这个人的一个爱好,如果某个人有多个爱好,只要记录一个就好了:设置数组num[],num[fa]表示以fa为根结点的爱好集合的人数,初始化为0.…
题目链接 题解:并查集把一个家的并在一起,特殊的一点是编号大的并到小的去.这个题有个坑编号可能为0000,会错数据3和5. 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 struct node 5 { 6 int id,num,area,fa,ma; 7 int ch[10]; 8 }p[100100]; 9 10 struct fz 11 { 12 int id,all; 13 double num,area; 14 }q[1001…
代码: 1 /* 2 这道题也是简单并查集,并查集复杂度: 3 空间复杂度为O(N),建立一个集合的时间复杂度为O(1),N次合并M查找的时间复杂度为O(M Alpha(N)), 4 这里Alpha是Ackerman函数的某个反函数,在很大的范围内(人类目前观测到的宇宙范围估算有10的80次方个原子, 5 这小于前面所说的范围)这个函数的值可以看成是不大于4的,所以并查集的操作可以看作是线性的. 6 7 不弄清楚一个算法的复杂度就是不敢很用 8 9 10 题解: 11 在询问时,标记当前结点,然…
题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房产面积. 并查集,合并的时候编号小的作为父亲节点,最后父亲节点一样的即属于一个家庭,其它都是细节处理没啥好说了. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h…
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> using namespace std; /* 并查集水题 */ +; struct UF{ int father[maxn]; void init(){ ;i<maxn;i++) father[i]=i; } int find_root(int x){ if(father[x]!=x…