题意:

思路:并查集模板题。

代码:

#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. JNIjw03

    1.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_JNIjw03.h" JNIEXPORT void JNICALL ...

  2. IOS-SDWebImage根据网络状态加载图片

    iOS开发-你真的会用SDWebImage? 2016-05-17 hosea_zhou 有意思啊 原创作者:hosea_zhou 原文地址:http://www.jianshu.com/p/dabc ...

  3. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)-mysql.sock丢失解决方案

    我们的LAMP是搭建在UBUNTU 12.04 LTS上的. LAMP是通过编译的方式进来安装的. 在一次处理意外挂机时由于未知的原因在重启后发现无法连接数据库了, 在打开网站时出现如下的的提示: E ...

  4. JavaScript中url 传递参数(特殊字符)解决方法及转码解码的介绍

    有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了.下表中列出了一些URL特殊符号及编码   十六进制值 1. + URL 中+号表示空格 %2B 2. 空 ...

  5. Audio/Movie/Image

    Audio 1. 引入AVFoundation 库,此库用于处理音频的播放. > 使用AVAudioPlayer 播放音频,此类只能播放本地音频文件.对于流媒体(边下边播)的播放使用第三方框架实 ...

  6. C++静态数据成员实现

    静态数据成员是在一个类中用关键字static声明的数据成员.在C++中,一般使用静态成员来代替C语言的全局变量,以达到数据共享.C和C++的全局变量有一定的局限性,可以任意被修改,也容易和其它的变量名 ...

  7. warning MSB8004: Output Directory does not end with a trailing slash.

    当在VC里编译时,发现这个警告,就是说设置的目录参数不是以反斜杠结束的目录名称,如下: 1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V ...

  8. auto_ptr, unique_ptr, shared_ptr and weak_ptr智能指针讲解

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...

  9. shfileoperation 删除文件 FileDelete(CString strName)

    From:http://blog.csdn.net/lvwx369/article/details/41440883 注意:其中namePath 为全局变量 Cstring namePath; BOO ...

  10. 【Javascrpt 速成篇】 一:js基础

     本系列文章Javascript一律简称js,javascript太长了((⊙﹏⊙)b) js概述 js是面向对象和基于事件驱动的解释型语言,主要用于WEB前端,处理用户交互.几年前js只是作为一种前 ...