题意:

思路:并查集模板题。

代码:

#include <cstdio>
#include <algorithm>
using namespace std;
;
int father[maxn];
};

void Init()
{
    ;i<maxn;i++)
        father[i]=i;
}

int FindSet(int a)
{
    int root=a;
    while(father[root]!=root)
        root=father[root];
    //剪枝,(不剪枝会超时哦)
    while(father[a]!=a){
        int tmp=a;
        a=father[a];
        father[tmp]=root;
    }
    return root;
}

void Union(int a,int b)
{
    int seta=FindSet(a);
    int setb=FindSet(b);
    if(seta!=setb) father[setb]=seta;
}

int main()
{
    //freopen("pat.txt","r",stdin);
    Init();
    int n,m,pre,curr;
    ;
    scanf("%d",&n);
    ;i<n;i++){
        scanf("%d%d",&m,&pre);
        if(pre>birdCnt) birdCnt=pre;
        ;j<m;j++){
            scanf("%d",&curr);
            if(curr>birdCnt) birdCnt=curr;
            Union(pre,curr);
            pre=curr;
        }
    }
    ;i<=birdCnt;i++)
        birdSet[FindSet(i)]++;
    ;
    ;i<=birdCnt;i++)
        ) setCnt++;
    printf("%d %d\n",setCnt,birdCnt);
    int query,a,b;
    scanf("%d",&query);
    ;i<query;i++){
        scanf("%d%d",&a,&b);
        if(FindSet(a)==FindSet(b)) printf("Yes\n");
        else printf("No\n");
    }
    ;
}

1118 Birds in Forest的更多相关文章

  1. 1118 Birds in Forest (25 分)

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  2. [并查集] 1118. Birds in Forest (25)

    1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...

  3. PAT 1118 Birds in Forest [一般]

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  4. PAT甲级——1118 Birds in Forest (并查集)

    此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984   1118 Birds in Forest  ...

  5. 1118 Birds in Forest (25 分)

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  6. PAT 1118 Birds in Forest

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  7. 1118. Birds in Forest (25)

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  8. PAT A 1118. Birds in Forest (25)【并查集】

    并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...

  9. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

随机推荐

  1. java手动分页处理

    经常我们在操作数据库的时候都会用到分页,而且很多框架也提供了分页功能,像PageHelper. 但是在有些项目中,需要将数据查询出来进行手动分页,那么原理是什么呢? 其实很简单,首先需要知道数据总量, ...

  2. Spring scope解惑

    在2.0之前只有两种singleton和prototype(网上说的,没去验证),后面增加了session.request.global session三种专门用于web应用程序上下文的Bean Si ...

  3. Android中getDimension,getDimensionPixelOffset和getDimensionPixelSize 区别

    getDimension 获取某个dimen的值,如果是dp或sp的单位,将其乘以density,如果是px,则不乘   返回float getDimensionPixelOffset 获取某个dim ...

  4. B/S,C/S简单介绍

    B/S,C/S 架构 硬件环境不同:C/S 一般建立在专用的网络上, 小范围里的网络环境, 局域网之间再通过专门服务器提供连接和数据交换服务. B/S 建立在广域网之上的, 不必是专门的网络硬件环境, ...

  5. Win7性能选项

    1. 性能选项:只保留勾选下面的即可. 2. 隐藏explorer导航栏的“库”列表 HKEY_CLASSES_ROOT\CLSID\{031E4825-7B94-4dc3-B131-E946B44C ...

  6. hdu 5265

    http://acm.hdu.edu.cn/showproblem.php?pid=5256 题目不错,题面忍不住骂一句mmp.......后面说ai都是正整数,我以为修改后也必须是正整数,前面又说只 ...

  7. ubuntu 安装python3.7 以及安装pip3 出现Command '('lsb_release', '-a')' returned non-zero exit status 1问题解决

    最近因为电脑重装,东西全没了,总计一下最近重装环境的过程. 如果没有安装包,请下载: wget http://www.python.org/ftp/python/3.7.0/Python-3.7.0. ...

  8. docker 创建镜像,并推送到私有仓库

    创建镜像 创建  Dockerfile 镜像命名规则:registyr_url / namespace / depart / name : version 用这个规则创建的镜像,可直接推送到私有仓库 ...

  9. 基于openfire+smack即时通讯instant message开发

    前言 Java领域的即时通信的解决方案可以考虑openfire+spark+smack.当然也有其他的选择. Openfire 是基于Jabber协议(XMPP)实现的即时通信服务器端版本,目前建议使 ...

  10. CoreData之增删改查

    1. 导入库文件CoreData.framework2. 在iOS的Core Data 中建Data Model文件 此时有三种选择 2.1. 选Data Model(如默认名Model.xcdata ...