思路:并查集一套带走。


AC代码

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 10000+5;

int par[maxn], vis[maxn];

int findRoot(int x) {
    return x == par[x] ? x : par[x] = findRoot(par[x]);
}

void unionSet(int x, int y) {
    x = findRoot(x);
    y = findRoot(y);
    if(x != y) {
        par[x] = y;
    }
}

void init(int n) {
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i <= n; i++) {
        par[i] = i;
    }
}

int main() {
    int n, k, q, len;
    len = -1;
    scanf("%d", &n);
    init(maxn);
    for(int i = 0; i < n; i++) {
        int x, y;
        scanf("%d%d", &k, &x);
        len = max(len, x);
        for(int j = 1; j < k; j++) {
            scanf("%d", &y);
            len = max(len, y);
            unionSet(x, y);
            x = y;
        }
    }
    int tol = 0;
    for(int i = 1; i <= len; i++) {
        int root = findRoot(i);
        if(!vis[root]) {
            vis[root] = 1;
            tol++;
        }
    }
    printf("%d %d\n", tol, len);
    scanf("%d", &q);
    int x, y;
    for(int i = 0; i < q; i++) {
        scanf("%d%d", &x, &y);
        x = findRoot(x);
        y = findRoot(y);
        if(x == y) {
            printf("Yes\n");
        } else {
            printf("No\n");
        }
    }
    return 0;
}

如有不当之处欢迎指出!

PAT1118. Birds in Forest (并查集)的更多相关文章

  1. PAT1118:Birds in Forest

    1118. Birds in Forest (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Some ...

  2. CodeForces 755C PolandBall and Forest (并查集)

    题意:给定每一点离他最远的点,问是这个森林里有多少棵树. 析:并查集,最后统计不同根结点的数目即可. 代码如下: #pragma comment(linker, "/STACK:102400 ...

  3. PAT A1118 Birds in Forest (25 分)——并查集

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

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

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

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

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

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

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

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

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

  8. Codeforces 755C:PolandBall and Forest(并查集)

    http://codeforces.com/problemset/problem/755/C 题意:该图是类似于树,给出n个点,接下来p[i]表示在树上离 i 距离最远的 id 是p[i],如果距离相 ...

  9. CodeForces - 755C PolandBall and Forest (并查集)

    题意:给定n个数,Ai的下标为1~n.对于每一个i,Ai与i在同一个树上,且是与i最远的点中id最小的点(这个条件变相的说明i与Ai连通).求森林中树的个数. 分析:若i与Ai连通,则在同一个树上,因 ...

随机推荐

  1. _0_web_基础

    创:18_3_2017修:20_3_2017 什么是前端? --在浏览器中展示内容以及处理请求 什么是浏览器? --一款能将网页内容展现给用户查看,并且让用户与网页交互的软件 什么是内核? --渲染引 ...

  2. BSA Network Shell系列-nsh命令

    nsh nsh命令软链接到zsh,直接运行nsh可进入Network Shell,所有的Network Shell命令都需要运行nsh进入Network Shell执行 1 使用cd命令访问远程主机和 ...

  3. android studio获取sha1签名

    转载 :http://blog.csdn.net/kezhongke/article/details/42678077

  4. ssh免秘钥登录

    简介 SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定:SSH 为建立在应用层基础上的安全协议.SSH 是目前较可靠,专为远程 ...

  5. Python笔记001-----简介及常用的库

    1.Python是一种解释性语言,大部分代码要比编译型语言(如C++,java等)运行要慢点多.2.对于高并发,多线程的应用程序而言,Python并不是理想语言,python有全局解释器锁(Globa ...

  6. ArcGIS License启动无响应

    根据对网上的总结以及个人的经验,首先建议关闭电脑的防火墙以及杀毒软件 如果点击启动,依然没有反应,建议在dos下进行启动,命令为: lmgrd -z -c service.txt 如果需要深入研究,可 ...

  7. Django的ModelForm

    基于django.forms.ModelForm:与模型类绑定的Form 先定义一个ModelForm类,继承ModelForm类 from django.forms import ModelForm ...

  8. Linux 下编译安装xDebug命令速记

    下载xdebug-2.2.4.tgz软件链接: http://pan.baidu.com/s/1jGHYRMA #解压 xdebugtar -zxvf xdebug-2.2.4.tgz #进入xdeb ...

  9. iOS设备唯一标识的前世今生

    设备唯一标识 估计很多开发都有被要求过获取一下设备的唯一标识,获取设备的唯一标识经常使用在我们做统计或者是在保证一台设备登录亦或者是做IM的时候可能会考虑去使用它,这一次在自己的需求当中就有一个&qu ...

  10. 【OH】Oracle软件安装需要的软件包(官方文档)

    1  安装12c 1.1  Table 3 x86-64 Supported Linux 7 Operating System Requirements Item Requirements SSH R ...