1107. Social Clusters (30) Search

#include<bits/stdc++.h>

using namespace std;
vector<int>rel[1010];
vector<int>hob[1010];
bool bok [1010];
int cnt;
void dfs(int s);
void dfs2(int s){
	for(int i=0;i<rel[s].size();i++){
		int t = rel[s][i];
		if (! bok [t]) {
			cnt++;
			side [t] = 1;
			dfs(t);
		}
	}
}
void dfs(int s){
	for(int i=0;i<hob[s].size();i++){
		dfs2(hob[s][i]);	
	}
}
intmain()
{
	int n,k;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d:",&k);
		while(k--){
			int t;
			scanf("%d",&t);
			hob[i].push_back(t);
			rel[t].push_back(i);
		}
	}
	int fa=0;
	priority_queue<int,vector<int>,less<int> >ans;
	for(int i=1;i<=n;i++){
		if (! bok [i]) {
			fa++;
			cnt=1;
			side [i] = 1;
			dfs(i);	
			ans.push(cnt);		
		}
	}
	cout<<ans.size()<<endl;
	while(!ans.empty())
	{
		cout<<ans.top();ans.pop();
		if(!ans.empty())cout<<" ";
	}
	
	return 0;
}


The meaning of the question is a bit confusing. First, give everyone's hobbies and each hobby must have some people who have this hobby. Then let us count the number of networks in total according to the network composed of different hobbies. The number of members of each network 

It is not very good to count this information because a person can have some hobbies and there are some different people under each hobby but people in a network must be able to connect through hobbies

Two-layer mapping relationship can be established. Double-layer dfs 

First, store the hobbies of each person in the hobby table, then find a hobby from one person, then go to the rel table to find different people under the same hobby, this person has many hobbies, and then go to the hobby table to find different people

The point (people.. um) that the whole process can run to during the continuous call to each other is the number of people recorded in this network

Finally, enumerate and search once to get the solution


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325732664&siteId=291194637