1107. Social Clusters (30)
来源:互联网 发布:java逐个读取单词 编辑:程序博客网 时间:2024/05/17 22:30
最近偏爱bfs,抛弃了dfs
这道题目用了两个二维数组存放信息。
一起bfs。
为了显示出区别,所以用负数表示人的编码
用正数表示爱好
AC代码:
#include <bits/stdc++.h>using namespace std;bool cmp(const int a, const int b){ return a > b; }int main(void){ int n; cin >> n; vector<vector<int> > peo(n); vector<vector<int> > larr(1001); vector<bool> test(n); vector<bool> test1(1001); int i, j, t; int temp; for (i = 0; i < n; i++) { scanf("%d: ", &t); for (j = 0; j < t; j++) { scanf("%d", &temp); larr[temp].push_back(-i); peo[i].push_back(temp); } } vector<int> res; queue<int> sup; for (i = 0; i < n; i++) { int jt = 0, fron; if (test[i]) continue; else { jt++; test[i] = true; for (j = 0; j < peo[i].size(); j++) { if (test1[peo[i][j]]) continue; sup.push(peo[i][j]); test1[peo[i][j]] = true; } while (!sup.empty()) { fron= sup.front(); sup.pop(); if (fron <= 0) { fron = -fron; if (!test[fron]) { jt++; test[fron] = true; for (j = 0; j < peo[fron].size(); j++) { if (test1[peo[fron][j]]) continue; sup.push(peo[fron][j]); test1[peo[fron][j]] = true; } } } else { for (j = 0; j < larr[fron].size(); j++) { if (test[-larr[fron][j]]) continue; sup.push(larr[fron][j]); } } } } res.push_back(jt); } sort(res.begin(), res.end(), cmp); cout << res.size() << endl; for (i = 0; i < res.size() - 1; i++) { cout << res[i] << " "; } cout << res[i];}
阅读全文
0 0
- 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)
- 【PAT】1107. Social Clusters (30)
- Fresco 初体验各种问题解决
- Ubuntu 14.04 server 64bit安装jdk
- JAVA基础复习五-数组及栈堆内存分配
- 70. Climbing Stairs
- csdn粘贴代码后出现编辑异常的解决
- 1107. Social Clusters (30)
- SQL 连接
- c3p0 c3p0.properties
- Jarvis OJ PHPINFO【审计代码反序列化】
- 2017年严重拖延着患者欠下的债
- 实现在一组数中找出最大值与最小值
- 后端总结1
- Nginx
- 总有一天你将破蛹而出