PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)

简介: PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)

题目链接:点击打开链接

题目大意:略。

解题思路:并查集思路,轮到第几张照片时,以它的 id 为 root,然后吸收它自己所有的鸟编号,这样就可以达到在区分在不同树上的鸟的集合。

AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a)
#define ssclr(ss) ss.clear(), ss.str("")
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
const int maxn=1e4+10;
unordered_set<int> ust, hust;
unordered_map<int,int> ump;
int pre[2*maxn], head[2*maxn];
void init()
{
    for(int i=1;i<2*maxn;i++) pre[i]=i;
}
int findx(int x)
{
    return x==pre[x] ? x : pre[x]=findx(pre[x]);
}
void join(int x, int y)
{
    int fx=findx(x), fy=findx(y);
    if(fx!=fy) pre[fx]=fy;
}
int main()
{
    init();
    int n,k,t,rt,a,b;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        rt=10000+i;
        head[i]=rt;
        scanf("%d",&k);
        while(k--)
        {
            scanf("%d",&t);
            join(t,rt);
            ust.insert(t);
        }
    }
    for(int i=1;i<=n;i++)
        ump[findx(head[i])]=1;
    printf("%d %d\n",ump.size(),ust.size());
    scanf("%d",&k);
    while(k--)
    {
        scanf("%d%d",&a,&b);
        if(findx(a)==findx(b)) puts("Yes");
        else puts("No");
    }
    return 0;
}
目录
相关文章
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
67 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
96 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
109 0
|
索引
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
89 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
93 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
122 0
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
117 0
PAT (Advanced Level) Practice - 1103 Integer Factorization(30 分)
PAT (Advanced Level) Practice - 1103 Integer Factorization(30 分)
83 0
|
测试技术
PAT (Advanced Level) Practice - 1029 Median(25 分)
PAT (Advanced Level) Practice - 1029 Median(25 分)
96 0
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
78 0