第一次的代码(测试点全部没过,感觉代码没错)
经典并查集题
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n;
const int maxn = 1010;
vector<int> person[maxn], root, clu;
int father[maxn];
void init() {
for(int i = 1; i <= n; i++)
father[i] = i;
}
int findFather(int x) {
int a = x;
while(x != father[x]) {
x = father[x];
}
while(a != father[a]) {
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b) {
int faA = findFather(a);
int faB = findFather(b);
if(faA != faB) {
father[faA] = faB;
}
}
bool isCluster(int a, int b) {
for(int i = 0; i < person[a].size(); i++) {
for(int j = 0; j < person[b].size(); j++) {
if(person[a][i] == person[b][j]) {
return true;
}
}
}
return false;
}
bool cmp(int a, int b) {
return a > b;
}
int main() {
cin >> n;
init();
clu.resize(n);
int num, temp;
for(int i = 0; i < n; i++) {
scanf("%d:", &num);
for(int j = 0; j < num; j++) {
cin >> temp;
person[i].push_back(temp);
}
}
for(int i = 1; i <= n; i++) {
for(int j = i + 1; j <= n; j++) {
if(isCluster(i, j)) {
Union(i, j);
}
}
}
for(int i = 1; i <= n; i++) {
if(father[i] == i) root.push_back(i);
}
int ans = root.size();
for(int i = 0; i < ans; i++) {
for(int j = 1; j <= n; j++) {
if(father[j] == root[i]) clu[i]++;
}
}
sort(clu.begin(), clu.end(), cmp);
cout << ans << endl;
for(int i = 0; i < ans; i++) {
if(i) cout << " ";
cout << clu[i];
}
return 0;
}
改进版(过了两个测试点)
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n;
const int maxn = 1010;
vector<int> person[maxn], root, clu;
int father[maxn];
void init() {
for(int i = 1; i <= n; i++)
father[i] = i;
}
int findFather(int x) {
int a = x;
while(x != father[x]) {
x = father[x];
}
while(a != father[a]) {
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b) {
int faA = findFather(a);
int faB = findFather(b);
if(faA != faB) {
father[faA] = faB;
}
}
bool isCluster(int a, int b) {
for(int i = 0; i < person[a].size(); i++) {
for(int j = 0; j < person[b].size(); j++) {
if(person[a][i] == person[b][j]) {
return true;
}
}
}
return false;
}
bool cmp(int a, int b) {
return a > b;
}
int main() {
cin >> n;
init();
clu.resize(n);
int num, temp;
for(int i = 0; i < n; i++) {
scanf("%d:", &num);
for(int j = 0; j < num; j++) {
cin >> temp;
person[i].push_back(temp);
}
}
for(int i = 1; i <= n; i++) {
for(int j = i + 1; j <= n; j++) {
if(isCluster(i, j)) {
Union(i, j);
}
}
}
for(int i = 1; i <= n; i++) {
if(father[i] == i) root.push_back(i);
}
int ans = root.size();
for(int i = 0; i < ans; i++) {
for(int j = 1; j <= n; j++) {
if(findFather(j) == root[i]) clu[i]++; // 只修改这一行代码
} // 路径压缩的锅
}
sort(clu.begin(), clu.end(), cmp);
cout << ans << endl;
for(int i = 0; i < ans; i++) {
if(i) cout << " ";
cout << clu[i];
}
return 0;
}
精简代码(还是只过了原来的那两个测试点)
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n;
const int maxn = 1010;
vector<int> person[maxn];
int father[maxn], isRoot[maxn] = { 0 };
void init() {
for(int i = 1; i <= n; i++)
father[i] = i;
}
int findFather(int x) {
int a = x;
while(x != father[x])
x = father[x];
while(a != father[a]) {
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b) {
int faA = findFather(a);
int faB = findFather(b);
if(faA != faB) {
father[faA] = faB;
}
}
bool isCommon(int a, int b) { // 用于判断a, b 两个人是否有共同爱好
for(int i = 0; i < person[a].size(); i++) {
for(int j = 0; j < person[b].size(); j++) {
if(person[a][i] == person[b][j]) { // 如果 a , b 这俩个人有共同爱好 返回true
return true;
}
}
}
return false;
}
bool cmp(int a, int b) {
return a > b;
}
int main() {
cin >> n;
init();
int num, temp;
for(int i = 0; i < n; i++) {
scanf("%d:", &num);
for(int j = 0; j < num; j++) {
cin >> temp;
person[i].push_back(temp); // 添加第i个人的爱好
}
}
for(int i = 1; i < n; i++) {
for(int j = i + 1; j <= n; j++) {
if(isCommon(i, j)) {
Union(i, j);
}
}
}
for(int i = 1; i <= n; i++) {
isRoot[findFather(i)]++;
}
int ans = 0;
for(int i = 1; i <= n; i++) {
if(isRoot[i]) ans ++;
}
sort(isRoot + 1, isRoot + n + 1, cmp);
cout << ans << endl;
for(int i = 1; i <= ans; i++) {
if(i!=1) cout << " ";
cout << isRoot[i];
}
return 0;
}
找到错误原因:出在了Union()上,函数功能没写错,但是这样子调用,会不断更新当前社群的根结点。而不同的人,爱好数量不一样,按照上述代码的思路就是,当一个人和当前社群的根结点有共同爱好,才给合并到当前社群,因此导致bug。然而题目的意思是,当一个结点和该社群中的任意一个结点存在相同爱好,就应该合并一次,将该结点加入到当前社群。修改后AC,如下:
(不过上述AC代码还是有一定参考价值的,通过记录第一次出现该爱好的人,极大地精简了代码)
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n;
const int maxn = 1010;
vector<int> person[maxn];
int father[maxn], isRoot[maxn] = { 0 }, hobby[maxn] = {0};
void init() {
for(int i = 1; i <= n; i++)
father[i] = i;
}
int findFather(int x) {
int a = x;
while(x != father[x])
x = father[x];
while(a != father[a]) {
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b) {
int faA = findFather(a);
int faB = findFather(b);
if(faA != faB) {
father[faA] = faB;
}
}
bool cmp(int a, int b) {
return a > b;
}
int main() {
cin >> n;
init();
int num, temp;
for(int i = 1; i <= n; i++) {
scanf("%d:", &num);
for(int j = 0; j < num; j++) {
cin >> temp;
if(hobby[temp] == 0) {
hobby[temp] = i;
}
Union(i, findFather(hobby[temp]));
}
}
for(int i = 1; i <= n; i++) {
isRoot[findFather(i)]++;
}
int ans = 0;
for(int i = 1; i <= n; i++) {
if(isRoot[i]) ans ++;
}
sort(isRoot + 1, isRoot + n + 1, cmp);
cout << ans << endl;
for(int i = 1; i <= ans; i++) {
if(i!=1) cout << " ";
cout << isRoot[i];
}
return 0;
}
AC后猛然发现,原来的思路根本没有错,只是在记录一个人的爱好时使用了从0开始的下标,修改后同样AC。
代码如下
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n;
const int maxn = 1010;
vector<int> person[maxn];
int father[maxn], isRoot[maxn] = { 0 };
void init() {
for(int i = 1; i <= n; i++)
father[i] = i;
}
int findFather(int x) {
int a = x;
while(x != father[x])
x = father[x];
while(a != father[a]) {
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b) {
int faA = findFather(a);
int faB = findFather(b);
if(faA != faB) {
father[faA] = faB;
}
}
bool isCommon(int a, int b) { // 用于判断a, b 两个人是否有共同爱好
for(int i = 0; i < person[a].size(); i++) {
for(int j = 0; j < person[b].size(); j++) {
if(person[a][i] == person[b][j]) { // 如果 a , b 这俩个人有共同爱好 返回true
return true;
}
}
}
return false;
}
bool cmp(int a, int b) {
return a > b;
}
int main() {
cin >> n;
init();
int num, temp;
for(int i = 1; i <= n; i++) { // 就只是修改这一行,改为1 ~ n, 而不是 0 ~ n-1
scanf("%d:", &num);
for(int j = 0; j < num; j++) {
cin >> temp;
person[i].push_back(temp);
}
}
for(int i = 1; i < n; i++) {
for(int j = i + 1; j <= n; j++) {
if(isCommon(i, j)) {
Union(i, j);
}
}
}
for(int i = 1; i <= n; i++) {
isRoot[findFather(i)]++;
}
int ans = 0;
for(int i = 1; i <= n; i++) {
if(isRoot[i]) ans ++;
}
sort(isRoot + 1, isRoot + n + 1, cmp);
cout << ans << endl;
for(int i = 1; i <= ans; i++) {
if(i!=1) cout << " ";
cout << isRoot[i];
}
return 0;
}