思路:并查集一套带走。


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. 在vue中优雅地实现简单页面逆传值

    [需求] 要实现的需求很简单,页面从A -> B,用户在B触发操作,将一些数据带回到A页面,在网上找了好久也只看到有人问,但总找不到很好答案.要实现的效果图如下: [联想] 在 ios 开发中, ...

  2. js调用系统虚拟键盘

    <input type="text" id="tt" /> <script language="javascript" t ...

  3. 禁止img图片拖动在新窗口打开

    JS function imgdragstart(){return false;} for(i in document.images)document.images[i].ondragstart=im ...

  4. Unity DoTween 动画使用案例

    这边我就直接放一个标准的Dotween动画的使用demo吧. 这个案例满足应该可以完成你所想实现的几乎所有复杂动画. void PlayTween() { //set tween data float ...

  5. Git分支管理[三]

    标签(linux): git 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 git分支管理命令 git branch #创建分支 git branch -v # ...

  6. jumpserver v0.4.0 基于 CenOS7 的安装详解

    标签(linux): jumpserver 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 笔者已经弃用Jumpserver,并自已开发了shell跳板机. sh ...

  7. Linux三剑客之awk最佳实践

    笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 知识点: 记录与字段 模式匹配:模式与动作 基本的awk执行过程 awk常用内置变量(预定义变量) awk数组 a ...

  8. 6 个 Linux 运维典型问题

    作为一名合格的 Linux 运维工程师,一定要有一套清晰.明确的解决故障思路,当问题出现时,才能迅速定位.解决问题,这里给出一个处理问题的一般思路: 重视报错提示信息:每个错误的出现,都是给出错误提示 ...

  9. Electron 打包Mac安装包代码签名问题解决方案Could not get code signature for running application

    最近一直在做electron应用的打包,集成mac版本的自动更新时出现了问题. Error: Could not get code signature for running application ...

  10. SpringMVC源码情操陶冶-AbstractHandlerMethodMapping

    承接前文SpringMVC源码情操陶冶-AbstractHandlerMapping,本文将介绍如何注册HandlerMethod对象作为handler 类结构瞧一瞧 public abstract ...