1107 Social Clusters (30 分) + 测试要点

17 篇文章 0 订阅
10 篇文章 0 订阅

题目描述

1107 Social Clusters (30 分)
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.
(……)

测试要点

  1. 输出的最后不要加多余的空格
  2. 如果只能过前3个测试点,后面的测试点全部显示“答案错误”,则将数组开大点,比题目要求的1000大,我这里开了1010

AC代码

#include<iostream>
#include<fstream>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#define MAX 1010
using namespace std;

int n;
int father[MAX];
int hobby[MAX]={0};
int cnt[MAX]={0};
int m;

void init(int n) {
    for(int i=1; i<=n; i++) {
        father[i]=i;
    }
}

int findFather(int a) {
    if(a==father[a])
        return a;
    while(a!=father[a]) {
        a=father[a];
    }
    return a;
}

void uni(int a, int b){
    if(findFather(a)== findFather(b)) {
        return;
    }
    father[findFather(a)]= findFather(b);
}

bool cmp(int a, int b) {
    return a>b;
}

int main() {
//    fstream fin;
//    fin.open("tmp.txt");
//    fin>>n;

    cin>>n;
    init(n);
    int maxh=0;
    for(int i=1;i<=n;i++) {
        int h;
//        fin>>m;
//        fin.ignore();
        cin>>m;
        cin.ignore();

        for(int j=0;j<m;j++) {
//            fin>>h;
            cin>>h;
            if(hobby[h]==0) {
                hobby[h]=i;
            } else {
                uni(i, hobby[h]);
            }
            if(h>maxh) {
                maxh=h;
            }
        }
    }

    for(int i=1; i<=n; i++) {
        cnt[findFather(i)]++;
    }
    sort(cnt+1, cnt+n+1, cmp);

    int cntf=0;
    for(int i=1;i<=n;i++) {
        if(cnt[i]==0) break;
        cntf++;
    }
    cout<<cntf<<endl;
    for(int i=1;i<=n;i++) {
        if(cnt[i+1]==0) {
            cout<<cnt[i];
            break;
        }
        cout<<cnt[i]<<' ';
    }




}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值