题意:

思路:并查集模板题。

代码:

#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. Oracle操作ORA-02289: 序列不存在

    解决方案:实现创建序列,创建语句如下所示: create sequence employees_seq minvalue maxvalue start increment cache ; 这时候再执行 ...

  2. angularjs地址栏传参

    1:路由定义参数 2.controller 3. 4.目标得到参数值

  3. selenium学习笔记(鼠标事件)

    昨天是简单的操作.之后是复杂的操作 首先是鼠标事件 AcationChains类  鼠标操作的常用方法: 右击 context_click() 双击 double_click() 拖动      dr ...

  4. 【C#笔札】1 string类型(2)

    4> Trim whitespace Trim 也是string的一个方法节点 C#例子如下: C#中有Trim,TrimEnd 和TrimStart三种Trim节点,其中后两者无需介绍. 如上 ...

  5. SPOJ - VLATTICE

    链接 题意:三维平面,找从(0,0,0)看(n,n,n)能看到的点 题解:很明显就是求gcd(i,j,k)==1的(i,j,k)对数,改一下公式即可,记得要算平行坐标轴的三个平面,还有含0的三个坐标 ...

  6. Sunday算法--C#版

    public static int Sunday(string text, string pattern) { int i, j, m, k; i = j = 0; int tl, pl; int p ...

  7. respond.js第六行 SCRIPT5: 拒绝访问。跨域问题

    问题描述:respond.js第六行 SCRIPT5: 拒绝访问.昨天为学弟学妹讲bootstrap,说到对ie78的兼容问题,解决办法中有引入html5shiv.js和respond.js两个文件夹 ...

  8. YCSB benchmark测试cassandra性能——和web服务器测试性能结果类似

    转自:http://www.itdadao.com/articles/c15a531189p0.html http://www.cnblogs.com/bettersky/p/6158172.html ...

  9. 下载并安装Prism5.0库(纯汉语版)

    Prism5.0中包含了文档,WPF代码示例,程序集.本篇告诉你从哪里获取程序集和代码示例,还有NuGet包的内容. 对于新功能,资产,和API的更改信息,请看Prism5.0新内容. 文档 Pris ...

  10. VSCode打开文件总是会覆盖上次打开的标签

    在使用VSCode的时候,打开一个文件之后,如果没有修改的话,那么再打开下一个文件的时候,他总会替换上次打开的标签,那么怎么样才能每次都在新的标签打开文件呢? 实际上,这种情况的出现是因为我们点击文件 ...