【PAT甲级A1107】 Social Clusters (30分)(c++)

1107 Social Clusters (30分)

作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:250 ms
内存限制:64 MB

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:

K​i​​ : h​i​​ [1] h​i​​ [2] ... h​i​​ [K​i​​ ]

where K​i​​ (>0) is the number of hobbies, and h​i​​ [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

题意:

有n个人,如果有喜欢的同一种爱好,那么他俩就是好朋友,而且好朋友的好朋友也是好朋友。求共有几组社交圈子,并降序输出,每个圈子好朋友的个数。

思路:

并查集。要通过爱好去联系人,并查集是将人的序列进行操作。如第1个人的爱好是4这个兴趣,那么4这个兴趣还没有人喜欢过,则记录hobby[4]=1,代表喜欢4这个兴趣的人可以去找1当好朋友,1的好朋友也是你的好朋友。如第4个人的爱好也是4,那么就将第4个人和第一个人组成好朋友,即Union(hobby[4],4)。并查集的时候采用路径压缩,利用哈希表记录朋友的个数。

参考代码:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
const int maxn = 1001;
int father[maxn], hobby[maxn] = {0};
int isroot[maxn] = {0};

int findfather(int x) {
    if (father[x] == x)return x;
    else {
        int F = findfather(father[x]);
        father[x] = F;
        return F;
    }
}

void Union(int a, int b) {
    int fa = findfather(a);
    int fb = findfather(b);
    if (fa != fb)
        father[fa] = fb;
}

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

int main() {
    init();
    int n, k, h, cnt = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d:", &k);
        for (int j = 0; j < k; j++) {
            scanf("%d", &h);
            if (hobby[h] == 0)hobby[h] = i;
            else Union(hobby[h], i);
        }
    }
    for (int i = 1; i <= n; i++) {
        isroot[findfather(i)]++;
    }
    vector<int> v;
    for (int i = 1; i <= n; i++) {
        if (isroot[i] != 0) {
            v.push_back(isroot[i]);
            cnt++;
        }
    }
    sort(v.begin(), v.end(), greater<int>());
    printf("%d\n", cnt);
    for (int i = 0; i < v.size(); i++) {
        if (i != 0)printf(" ");
        printf("%d", v[i]);
    }
    return 0;
}

如有错误,欢迎指正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Yes, it is possible to use the C programming language to implement the k-means algorithm. Here is an example implementation: ``` #include <stdio.h> #include <stdlib.h> #include <math.h> #define K 3 // number of clusters #define N 10 // number of data points #define D 2 // number of dimensions typedef struct { double x[D]; } Point; double distance(Point p1, Point p2) { double sum = 0; for (int i = 0; i < D; i++) { sum += pow(p1.x[i] - p2.x[i], 2); } return sqrt(sum); } int main() { Point data[N] = { {1, 2}, {2, 1}, {2, 3}, {3, 2}, {5, 6}, {6, 5}, {6, 7}, {7, 6}, {9, 8}, {8, 9} }; Point centroids[K] = { {1, 2}, {5, 6}, {9, 8} }; int assignment[N]; // repeat until convergence int iter = 0; while (1) { // assignment step int changed = 0; for (int i = 0; i < N; i++) { double min_dist = INFINITY; int min_idx; for (int j = 0; j < K; j++) { double dist = distance(data[i], centroids[j]); if (dist < min_dist) { min_dist = dist; min_idx = j; } } if (assignment[i] != min_idx) { assignment[i] = min_idx; changed = 1; } } if (!changed) { break; } // update step int counts[K] = { 0 }; for (int i = 0; i < N; i++) { int idx = assignment[i]; for (int j = 0; j < D; j++) { centroids[idx].x[j] += data[i].x[j]; } counts[idx]++; } for (int j = 0; j < K; j++) { if (counts[j] > 0) { for (int l = 0; l < D; l++) { centroids[j].x[l] /= counts[j]; } } } iter++; } // print results printf("Iterations: %d\n", iter); for (int i = 0; i < K; i++) { printf("Centroid %d: (%.2f, %.2f)\n", i+1, centroids[i].x[0], centroids[i].x[1]); } for (int i = 0; i < N; i++) { printf("Data point %d assigned to centroid %d\n", i+1, assignment[i]+1); } return 0; } ``` This implementation uses a fixed dataset of 10 two-dimensional points and 3 clusters. The algorithm iteratively assigns each data point to the nearest centroid and updates the centroids based on the assigned points. It continues until convergence, i.e., when no data points change assignments. The final centroids and assignments are printed to the console. Note that this implementation does not handle error cases or input validation, and can be improved for better performance.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值