1107. Social Clusters (30) -- 简单 DFS / BFS
来源:互联网 发布:汉以强亡 知乎 编辑:程序博客网 时间:2024/06/05 11:14
题目地址
https://www.patest.cn/contests/pat-a-practise/1107
题目描述
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
ac
这题就直接dfs,用变量标记某个人是否已经访问过
注意结果可能包含重复的
比如
44 3 3 1
- multiset
- 或者直接vector 然后再sort排序
ac1, DFS
#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <stack> #include <map> #include <set> #include <unordered_map>using namespace std;const int INF = 0x7fffffff;const int MIN_INF = - INF -1;typedef long long int LL;const int N = 1001;int n;vector<int> hobbys[N];vector<int> persons[N];bool vis[N];int cnt;void dfs(int index,int depth){ //cout << index << endl; cnt ++; int len = persons[index].size(); for(int i=0;i<len;i++) { int h = persons[index][i]; int lenh = hobbys[h].size(); for(int j=0;j<lenh;j++) { int perh = hobbys[h][j]; if(perh != index && !vis[perh]) { vis[perh] = true; dfs(perh,depth + 1); } } }}int main(){ //freopen("in.txt","r",stdin); while(scanf("%d", &n) != EOF) { for(int i=0;i<N;i++) { hobbys[i].clear(); persons[i].clear(); vis[i] = false; } int k,h; for(int i=0;i<n;i++) { scanf("%d:", &k); for(int j = 0;j<k;j++) { scanf("%d",&h); persons[i+1].push_back(h); hobbys[h].push_back(i+1); } } vector<int> ans; for(int i=1;i<=n;i++) { if(vis[i] == false) { vis[i] = true; cnt = 0; dfs(i,1); ans.push_back(cnt); } } int len = ans.size(); sort(ans.begin(), ans.end()); printf("%d\n",len); for(int i= len-1;i>0;i--) { printf("%d ", ans[i]); } printf("%d\n", ans[0]); } //printf("\n"); return 0;}
ac2, BFS
#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <list>#include <stack> #include <map> #include <set> #include <iterator> #include <unordered_map>using namespace std;const int INF = 0x7fffffff;typedef long long int LL;const int N = 1000 + 5;int n;vector<int> hobby[N];vector<int> hpo[N];bool vis[N];int bfs(int id){ int cnt = 1; queue<int> que; que.push(id); while(!que.empty()) { int tid = que.front(); que.pop(); int tHobbysSize = hobby[tid].size(); for(int i=0;i<tHobbysSize;i++) { int th = hobby[tid][i]; int othersSize = hpo[th].size(); for(int j = 0;j < othersSize; j ++) { int oId = hpo[th][j]; if(vis[oId] == false) { que.push(oId); vis[oId] = true; cnt ++; } } } } return cnt;}bool cmp(int a, int b){ return a > b;}int main(){ //freopen("in.txt", "r" , stdin); while(scanf("%d", &n) != EOF) { for(int i=1;i<=n;i++) { int k; scanf("%d: ", &k); while(k--) { int tmp; scanf("%d", &tmp); hobby[i].push_back(tmp); hpo[tmp].push_back(i); } } for(int i=1;i<=n;i++) { vis[i] = false; } vector<int> ans; int ansLen = 0; for(int i=1;i<=n;i++) { if(vis[i] == false) { vis[i] = true; int cnt = bfs(i); ans.push_back(cnt); ansLen ++; } } if(ansLen > 1) sort(ans.begin(), ans.end(), cmp); printf("%d\n", ansLen); printf("%d", ans[0]); for(int i=1;i<ansLen;i++) { printf(" %d", ans[i]); } printf("\n"); } return 0;}
- 1107. Social Clusters (30) -- 简单 DFS / BFS
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 《大话数据结构》
- MFC 关于对话框
- Netbeans安装sublime的Monokai配色方案
- 开发工具eclipse打开就闪退?
- ubutnu下交换capslock和ctrl键
- 1107. Social Clusters (30) -- 简单 DFS / BFS
- 杭电OJ 2043
- 《大话数据结构》
- 【总结】基本的git使用命令
- 大型网站系统架构
- 链表编程笔记
- python自动化之路-day3.1
- Recyclerview 常见问题的 Demo 解决办法
- Java MD5工具