题意:

思路:并查集模板题。

代码:

#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. Maven到底是个啥玩意

    Maven,是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 上面是百度百科对Maven的正式介绍,如果你是Maven初学者,我估计你看完之后心中肯 ...

  2. 教你打造一个Android组件化开发框架

    *本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 CC:Component Caller,一个android组件化开发框架, 已开源,github地址:https://github ...

  3. 使用springmvc的时候报错NoSuchBeanDefinitionException: No qualifying bean of type

    NoSuchBeanDefinitionException: No qualifying bean of type 其实我至今都不知道错误的根源在哪里,<context:component-sc ...

  4. Entity Framework 6:专家版本

    随着 Entity Framework 最新主版本 EF6 的推出,Microsoft 对象关系映射 (ORM) 工具达到了新的专业高度,与久负盛名的 .NET ORM 工具相比已不再是门外汉. EF ...

  5. yii2: 点击编辑后,左侧的连接(a.navtab)失效,变成在新窗口打开

    如:图一 使用a.navtab的时候,点击[自定义回复]->右侧列表,随便编辑一个,完成后 图二: 再点击,左侧的菜单,打开iframe就会失败,直接在新窗口打开.源代码如下: 造成这样的原因是 ...

  6. hdu1695莫比乌斯反演模板题

    hdu1695 求1<=i<=n&&1<=j<=m,gcd(i,j)=k的(i,j)的对数 最后的结果f(k)=Σ(1<=x<=n/k)mu[x]* ...

  7. PCB寻找器件

    1.如果你知道元件的序号(designator),可以按下J,C,然后输入序号跳到元件所在位置,元件会出现在屏幕中心.2.如果是sch跟pcb在同一个project下,你从原理图选定元件,然后右键元件 ...

  8. android xml绘图p113-p117

    1.Bitmap <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android=& ...

  9. fastjson缺陷--map转换json时出现$ref的情况

    DisableCircularReferenceDetect来禁止循环引用检测: JSON.toJSONString(..., SerializerFeature.DisableCircularRef ...

  10. Android 中Activity,Window和View之间的关系

    转自:http://hi.baidu.com/xiaofanqing/blog/item/8261ac114ab14f64cb80c435.html 我这里根据我个人的理解来讲讲我个人对这3个概念的理 ...