题意:

思路:并查集模板题。

代码:

#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. CSS相关博客

    1. CSS浮动(float,clear)通俗讲解:http://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html 2. CSS po ...

  2. python 2 3 读写中文文件 使用codecs最方便

    codecs进行文件的读取 python给我们提供了一个包codecs进行文件的读取,这个包中的open()函数可以指定编码的类型: import codecs f = codecs.open('te ...

  3. DGA GAN——GAN在安全中的应用

    DGA的模型:https://github.com/Yuren-Zhong/DeepDGA CNN.LSTM.双向LSTM 论文可以看https://openreview.net/pdf?id=BJL ...

  4. OLED取模笔记

  5. UI- UIView控件知识点回顾

    //  通过一个frame来初始化一个UI控件 - (id)initWithFrame:(CGRect)frame; // YES:能够跟用户进行交互 @property(nonatomic,gett ...

  6. 点击bindingNavigatorAddNewItem 关联的dataGridView不会新增一行

    方法一. 在设计界面,修改bindingNavigator1的属性AddNewItem 为“(无)”: 方法二. 在设计器自动生成的代码中找到这一行:             // bindingNa ...

  7. 【Codeforces】894D. Ralph And His Tour in Binary Country 思维+二分

    题意 给定一棵$n$个节点完全二叉树,$m$次询问,每次询问从$a$节点到其它所有节点(包括自身)的距离$L$与给定$H_a$之差$H_a-L$大于$0$的值之和 对整棵树从叶子节点到父节点从上往下预 ...

  8. oracle的序列号(sequence)

    oracle的自增列,要采用序列号(sequence). 初始化阶段要手动建立一个sequence,然后插入的时候,还要手动自己去读这个sequence的nextval赋给相关字段,如ID,麻烦的很. ...

  9. 软件测试 Record

    fxcopnunit软件质量 EDW 数据仓库ETL KPI 敏捷 测试计划 单元测试 集成测试 系统测试 对测试结果 阶段性 分析 总结 测试结果报告 环境问题:软硬件用户有问题,我们这边没有有效问 ...

  10. 【vs2013】使用VS2013打包程序

    如何用 VS 2013 打包 程序? 摘自:http://www.zhihu.com/question/25415940 更多请见摘自. 答案就在这里,想要你的exe独立运行在XP中:1.将平台工具集 ...