PAT甲组 1107. Social Clusters (30)

重温一下前几天的一个题,放松一下放松一下,今天大家都太强了QAQ

1107. Social Clusters (30)

时间限制
1000 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

题意:给个N,有N个人,接下来按编号1~N的顺序,每行给出一个人的感兴趣的活动,先给个K,表示K个活动,然后引号后面有K个活动编号,表示这个人喜欢这个活动。如果有两个人有任意重叠的喜欢的活动,它们就是一个小圈子的,要求输出有几个小圈子,接下来降序输出每个小圈子有几个人。

思路:并查集大法(千万不要一边读一边合并,不知道为啥一边读一边合并有三个测试点就是过不去,惆怅)↓

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
const int maxv = 1111;

int fa[maxv],root[maxv];

int findfa(int x)//找爸爸
{
	int a=x;
	while(x!=fa[x])
	{
		x=fa[x];
	}
	while(a!=fa[a])
	{
		int z=a;
		a=fa[a];
		fa[z]=x;
	}
	return x;
}


void uni(int a,int b)//合并两个小朋友
{
	int faa=findfa(a);
	int fab=findfa(b);
	if(faa!=fab)
	{
		fa[faa]=fab;
	}
}

struct node
{
	int num;//喜欢该活动的人数
	int a[maxv];//喜欢该活动的人的编号
}act[maxv];

int N,K,temp;

int main()
{
	scanf("%d",&N);
	for(int i=1;i<=N;i++)
	{
		fa[i]=i;
	}
	for(int i=1;i<=N;i++)
	{
		scanf("%d:",&K);
		for(int j=1;j<=K;j++)
		{
			scanf("%d",&temp);
			act[temp].a[++act[temp].num]=i;
		}
	}
	for(int i=1;i<maxv;i++)
	{
		for(int j=2;j<=act[i].num;j++)
		{
			uni(act[i].a[1],act[i].a[j]);
		}
	}
	for(int i=1;i<=N;i++)
	{
		root[findfa(i)]++;
	}
	int co=0;
	for(int i=1;i<=N;i++)
	{
		if(root[i]>0)
			co++;
	}
	printf("%d\n",co);
	sort(root+1,root+1+N);
	for(int i=N;i>N-co;i--)
	{
		if(i==N)
			printf("%d",root[i]);
		else
			printf(" %d",root[i]);
	}

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.util.HashMap; public class Manage { public static void main(String[] args) { HashMap<String, String> teacherClassMap = new HashMap<>(); // 存储老师和班级的对应关系 HashMap<String, String[]> groupMap = new HashMap<>(); // 存储班级和小组的对应关系 HashMap<String, String[]> classStudentMap = new HashMap<>(); // 存储班级和学生的对应关系 // 添加数据 teacherClassMap.put("小王", "一班"); teacherClassMap.put("小李", "二班"); teacherClassMap.put("小明", "三班"); groupMap.put("一班", new String[]{"一组", "二组", "三组"}); groupMap.put("二班", new String[]{"甲组", "乙组", "丙组"}); groupMap.put("三班", new String[]{"A组", "B组", "C组"}); classStudentMap.put("一班", new String[]{"一组学生1", "一组学生2", "一组学生3"}); classStudentMap.put("二班", new String[]{"甲组学生1", "甲组学生2", "甲组学生3"}); classStudentMap.put("三班", new String[]{"A组学生1", "A组学生2", "A组学生3"}); // 遍历数据 for (String teacher : teacherClassMap.keySet()) { String className = teacherClassMap.get(teacher); System.out.println(teacher + " 管理的班级是:" + className); for (String groupName : groupMap.keySet()) { String[] groupName = groupMap.get(className); System.out.println(className + " 所有的组是:" + groupName); String[] students = classStudentMap.get(groupName); for (String student : students) { System.out.println(" 学生姓名:" + student); } } } } }
最新发布
07-14
代码存在一些错误,我已经修改了一下: ```java import java.util.HashMap; public class Manage { public static void main(String[] args) { HashMap<String, String> teacherClassMap = new HashMap<>(); // 存储老师和班级的对应关系 HashMap<String, String[]> groupMap = new HashMap<>(); // 存储班级和小组的对应关系 HashMap<String, String[]> classStudentMap = new HashMap<>(); // 存储班级和学生的对应关系 // 添加数据 teacherClassMap.put("小王", "一班"); teacherClassMap.put("小李", "二班"); teacherClassMap.put("小明", "三班"); groupMap.put("一班", new String[]{"一组", "二组", "三组"}); groupMap.put("二班", new String[]{"甲组", "乙组", "丙组"}); groupMap.put("三班", new String[]{"A组", "B组", "C组"}); classStudentMap.put("一组", new String[]{"一组学生1", "一组学生2", "一组学生3"}); classStudentMap.put("二组", new String[]{"二组学生1", "二组学生2", "二组学生3"}); classStudentMap.put("三组", new String[]{"三组学生1", "三组学生2", "三组学生3"}); classStudentMap.put("甲组", new String[]{"甲组学生1", "甲组学生2", "甲组学生3"}); classStudentMap.put("乙组", new String[]{"乙组学生1", "乙组学生2", "乙组学生3"}); classStudentMap.put("丙组", new String[]{"丙组学生1", "丙组学生2", "丙组学生3"}); classStudentMap.put("A组", new String[]{"A组学生1", "A组学生2", "A组学生3"}); classStudentMap.put("B组", new String[]{"B组学生1", "B组学生2", "B组学生3"}); classStudentMap.put("C组", new String[]{"C组学生1", "C组学生2", "C组学生3"}); // 遍历数据 for (String teacher : teacherClassMap.keySet()) { String className = teacherClassMap.get(teacher); System.out.println(teacher + " 管理的班级是:" + className); String[] groups = groupMap.get(className); for (String groupName : groups) { System.out.println(className + " 所有的组是:" + groupName); String[] students = classStudentMap.get(groupName); for (String student : students) { System.out.println(" 学生姓名:" + student); } } } } } ``` 这段代码将会输出老师、班级、小组和学生的对应关系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值