pat甲级1107

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.

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

这道题考察并查集。

开始遇到了数组越界的问题,后来调试发现是最近考研复习计算机系统基础遇到的一个知识点:

1     for (i = 1; i < 1001; i++)
2     {
3         for (j = 0; j < hobby[i].size() - 1; j++)
4             _union(hobby[i][j], hobby[i][j + 1]);
5     }

我开始是这么写的,看似不会发生数组越界的问题,但是因为hobby[i].size()是无符号数unsigned型,当hobby[i].size()等于0时,减去1就成了无符号数2^32-1,所以发生了越界。

看来了解底层的一些原理确实很重要,usigned型要慎用。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<vector<int>> hobby(1001);
vector<int> father(1001, -1);
void _union(int i, int j);
int find(int i);

int main()
{
    int N;
    cin >> N;
    int i, j, k, t;
    char c;
    for (i = 1; i <= N; i++)
    {
        cin >> k >> c;
        for (j = 0; j < k; j++)
        {
            cin >> t;
            hobby[t].push_back(i);
        }
    }
    for (i = 1; i < 1001; i++)
    {
        for (j = 1; j < hobby[i].size(); j++)
            _union(hobby[i][j], hobby[i][j - 1]);
    }
    vector<int> cluster;
    for (i = 1; i <= N; i++)
    {
        if (father[i] < 0)
            cluster.push_back(-father[i]);
    }
    sort(cluster.begin(), cluster.end());
    cout << cluster.size() << endl << cluster[cluster.size() - 1];
    for (i = cluster.size() - 2; i >= 0; i--) cout << " " << cluster[i];
    return 0;
}

void _union(int i, int j)
{
    int a = find(i);
    int b = find(j);
    if (a == b) return;
    if (father[a] < father[b])
    {
        father[a] += father[b];
        father[b] = a;
    }
    else
    {
        father[b] += father[a];
        father[a] = b;
    }
}
int find(int i)
{
    while (father[i] > 0) i = father[i];
    return i;
}

原文地址:https://www.cnblogs.com/lxc1910/p/9690539.html

时间: 2024-10-09 12:56:42

pat甲级1107的相关文章

PAT甲级考前整理

终于在考前,刷完PAT甲级130道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种境界.PAT甲级题目总的说卡题目的比较多,卡测试点的比较少,有些题目还会有题意混淆,这点就不吐槽了吧.静下心来耍这130道题,其实磨练的是一种态度与手感,养成的是一种习惯.热爱AC没有错!! 130道题目主要的考点: 1.排序:快速排序,直接插入排序,希尔排序,分治排序,堆排序. 2.图论:拓扑排序.最短路径.深度搜索.广度搜索. 3.树:树的遍历.完全二叉树.AVL. 4.其他:并查集,模拟,哈希.背包.

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

PAT甲级考试题库1001 A+B Format 代码实现及相关知识学习

准备参加九年九月份的PAT甲级证书考试,对网站上的题目进行总结分析: 1001题 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). 计算a+b的值并以一定格式输出其和sum(数字需要

PAT甲级专题|最短路

PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做,只能得20分 #include<bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 510; int cmax,n,ter,m; int caps[maxn]; int g[maxn][m

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names

PAT甲级第二次真题练习

1005 Spell It Right (20)(20 分)提问 Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English. Input Specification: Each input file contains one test case. Each case occupies one

PAT 甲级 1035 Password

https://pintia.cn/problem-sets/994805342720868352/problems/994805454989803520 To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to di

pat甲级 1001 A+B Format

要写甲级题,首要任务是解决英文这个大难题. 困难词汇(我不认识的):calculate计算  standard format 标准格式  digits数字  separated 分离  commas逗号 这道题的大致意思是,给出两个数a和b,并且a和b都大于等于-10的6次方小于等于10的6次方,求出a和b的和,将这个和,每三个数字用逗号分隔一下.看懂了题意以后这题就简单了 ac代码如下: #include <iostream> #include<string> #include&

PAT——甲级1046S:shortest Distance

这道题,折磨了我一个多小时,前前后后写了三个算法. 1046 Shortest Distance (20 point(s)) The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input