L3-003 社交集群 (30 分)

#include <bits/stdc++.h>
using namespace std;
int vis[1010];
vector<int> table[1010]; // 每个人对应的兴趣
vector<int> val_person[1010]; // 每个兴趣对应的人
bool cmp(int a, int b){
    
    
	return a > b;
} 
void getHorde(int people, int & ans){
    
    
	if(vis[people]) return;
	vector<int>::iterator it = table[people].begin();
	ans++;
	vis[people] = 1;
	while(it != table[people].end()){
    
    
		for(vector<int>::iterator p = val_person[*it].begin(); p != val_person[*it].end(); ++p)
			getHorde(*p, ans);
		it++;
	}
}
//getHorde: 即选中一个人,找出其所有的兴趣,再找出其兴趣对应有多少人,再遍历他们,选中一个人,找出其所有的兴趣,再找出其兴趣对应有多少人...
//显然是递归过程
//一个人仅能加入一个集群,所以有个vis
int main(){
    
    
	memset(vis, 0, 4040);
	int n; 
	cin >> n;
	for(int i = 1; i <= n; ++i){
    
    
		int m, temp;
		char ch;
		cin >> m >> ch;
		for(int j = 1; j <= m; ++j){
    
    
			cin >> temp;
			table[i].push_back(temp);
			val_person[temp].push_back(i);
		}
	}
	vector<int> v;
	for(int i = 1; i <= n; ++i){
    
    
		int val = 0;
		getHorde(i, val);
		if(val) v.push_back(val);
	}
	sort(v.begin(), v.end(), cmp);
	cout << v.end() - v.begin() << endl;
	for(int i = 0; i < v.end() - v.begin(); ++i){
    
    
		if(i) cout << " ";
		cout << v[i];
	}
}

猜你喜欢

转载自blog.csdn.net/qq_33987764/article/details/113690188
今日推荐