并查集合集(PAT甲级)

PAT 1118 Birds in Forest

Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds, tell if they are on the same tree.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤10^​4​​ ) which is the number of pictures. Then N lines follow, each describes a picture in the format:

K B1 B2 ... B​K

​​where K is the number of birds in this picture, and B​i’s are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 10​^4​​ .After the pictures there is a positive number Q (≤10 ^​4) which is the number of queries. Then Q lines follow, each contains the indices of two birds.

Output Specification:
For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, print in a line Yes if the two birds belong to the same tree, or No if not.

Sample Input:

4
3 10 1 2
2 3 4
4 1 5 7 8
3 9 6 4
2
10 5
3 7

Sample Output:

2 10
Yes
No

思路:将每张照片上的鸟使用Union合并,定义一个map完成鸟的编号到其数量的映射,方便计算集合的个数和鸟的总数。最后判断两只鸟是否属于一个集合只需判断findFather()是否相等即可。

AC代码:

#include <stdio.h>
#include <map>
using namespace std;
const int maxn=10010;
int n,k,a,first;
int father[maxn];

int findFather(int x){
    int a=x;
    while(x!=father[x])
        x=father[x];
    while(a!=father[a]){    //路径压缩
        int t=a;
        a=father[a];
        father[t]=x;
    }
    return x;
}

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

int main(){
    scanf("%d",&n);
    map<int,int> m;
    int cnt=0;
    for(int i=1;i<=maxn;i++)
        father[i]=i;
    for(int i=0;i<n;i++){
        scanf("%d%d",&k,&first);
        m[first]+=1;
        for(int j=0;j<k-1;j++){
            scanf("%d",&a);
            m[a]+=1;
            Union(first,a);
        }
    }
    for(map<int,int>::iterator it=m.begin();it!=m.end();it++){
        if(findFather(it->first)==it->first)
            cnt++;
    }
    int q,q1,q2;
    scanf("%d",&q);
    printf("%d %d\n",cnt,m.size());
    for(int i=0;i<q;i++){
        scanf("%d%d",&q1,&q2);
        if(findFather(q1)==findFather(q2))
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

PAT 1107 Social Clusters

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: hi[1] hi[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

思路:使用multimap完成人到爱好的映射,将multimap中值相同的两个键进行Union(有相同爱好的两个人进行合并),再借助一个map记录集合的个数及每个集合的人数。

AC代码:

#include <stdio.h>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int father[1010];

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

void Union(int x,int y){
    if(findFather(x)==findFather(y))
        return;
    father[findFather(x)]=findFather(y);
}

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

int main(){
    int n,k,h,cnt=0;
    multimap<int,int> m;
    map<int,int> m1;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        father[i]=i;
    for(int i=1;i<=n;i++){
        scanf("%d:",&k);
        for(int j=0;j<k;j++){
            scanf("%d",&h);
            m.insert(make_pair(i,h));
        }
    }
    multimap<int,int>::iterator it1,it2;
    for(it1=m.begin();it1!=m.end();it1++){
        for(it2=m.begin();it2!=it1;it2++){
            if(it1->second==it2->second){
                Union(it1->first,it2->first);
            }
        }
    }
    for(int i=1;i<=n;i++){
        m1[findFather(i)]+=1;
    }
    printf("%d\n",m1.size());
    map<int,int>::iterator it;
    vector<int> ans;
    for(it=m1.begin();it!=m1.end();it++){
        ans.push_back(it->second);
    }
    sort(ans.begin(),ans.end(),cmp);
    printf("%d",ans[0]);
    for(int i=1;i<ans.size();i++)
        printf(" %d",ans[i]);
    return 0;
}
内容概要:本文介绍了一个关于超声谐波成像中幅度调制聚焦超声所引起全场位移和应变的分析模型,并提供了基于Matlab的代码实现。该模型旨在精确模拟和分析在超声谐波成像过程中,由于幅度调制聚焦超声作用于生物组织时产生的力学效应,包括全场的位移与应变分布,从而为医学成像和治疗提供理论支持和技术超声谐波成像中幅度调制聚焦超声引起的全场位移和应变的分析模型(Matlab代码实现)手段。文中详细阐述了模型构建的物理基础、数学推导过程以及Matlab仿真流程,具有较强的理论深度与工程应用价值。; 适合人群:具备一定声学、生物医学工程或力学背景,熟悉Matlab编程,从事医学成像、超声技术或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于超声弹性成像中的力学建模与仿真分析;②支持高强度聚焦超声(HIFU)治疗中的组织响应预测;③作为教学案例帮助理解超声与组织相互作用的物理机制;④为相关科研项目提供可复用的Matlab代码框架。; 阅读建议:建议读者结合超声物理和连续介质力学基础知识进行学习,重点关注模型假设、偏微分方程的数值求解方法及Matlab实现细节,建议动手运行并修改代码以加深理解,同时可拓展应用于其他超声成像或治疗场景的仿真研究。
### 关于PAT Basic Level Practice的测试点及题目解析 #### 题目难度分级 PAT(Programming Ability Test)是由浙江大学举办的计算机程序设计能力考试,分为不同级别。其中乙级即Basic Level主要面向初学者,考察基本编程技能[^1]。 #### 测试点特点 对于PAT Basic Level中的某些特定题目而言,其测试点设置较为严格。例如,在处理字符串匹配类问题时,需要注意算法逻辑中何时应当终止循环以防止不必要的重复计算;而在涉及数值运算的问题里,则可能因为边界条件而增加复杂度[^3]。 #### 编程语言的选择影响 值得注意的是,尽管大部分简单题目可以作为学习某种新语言的良好实践材料,但在实际操作过程中可能会遇到由于所选语言特性而导致难以通过全部测试点的情况。比如Java在面对部分效率敏感型试题时表现不佳,这可能是由于该语言本身的执行速度相对较慢以及内存管理方式等因素造成的。因此有时不得不转而采用其他更适合解决此类问题的语言版本来完成解答[^2]。 ```cpp #include<bits/stdc++.h> using namespace std; int a[100000]; int c=1; void getPrime(){ int flag=0; for(int i=2;i<105000;i++){ flag=1; for(int j=2;j<=sqrt(i);j++){ if(i%j==0){ flag=0; break; } } if(flag==1) a[c++]=i; } } int main(){ int m,n,i,t=1; scanf("%d %d",&m,&n); getPrime(); for(i=m;i<=n;i++){ if(t%10==1){ printf("%d",a[i]); t++; }else{ printf(" %d",a[i]); t++; } if((t-1)%10==0) printf("\n"); } return 0; } ``` 上述C++代码展示了如何实现一个简单的质数打印功能,并且针对输出格式进行了特殊处理以满足特定要求。这段代码很好地体现了编写高效解决方案的重要性,尤其是在应对像PAT这样的在线评测系统时[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值