zoukankan      html  css  js  c++  java
  • 【CODEVS1073】家族

    Description

    若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。 规定:x和y是亲戚,y和z是亲戚,那么x和z也是亲戚。如果x,y是亲戚,那么x的亲戚都是y的亲戚,y的亲戚也都是x的亲戚。

    Input

    第一行:三个整数n,m,p,(n<=5000,m<=5000,p<=5000),分别表示有n个人,m个亲戚关系,询问p对亲戚关系。 以下m行:每行两个数Mi,Mj,1<=Mi,Mj<=N,表示Ai和Bi具有亲戚关系。 接下来p行:每行两个数Pi,Pj,询问Pi和Pj是否具有亲戚关系。

    Output

    P行,每行一个’Yes’或’No’。表示第i个询问的答案为“具有”或“不具有”亲戚关系。

    Sample Input

    6 5 3

    1 2

    1 5

    3 4

    5 2

    1 3

    1 4

    2 3

    5 6

    Sample Output

    Yes

    Yes

    No

    HINT

    n<=5000,m<=5000,p<=5000

    #include<iostream> 
    #include<cstdio>
    using namespace std;
    int ff[5001];
    int root(int l)
    {
        if (ff[l]!=l) ff[l]=root(ff[l]);
        return (ff[l]);
    }
    int unionx(int c,int d)//root过程已经保证ff[x]=root(x),所以直接合并就行 
    {
        if(c!=d) ff[c]=d;
    }
    int main()
    {
        int n,m,p,x,y;
        scanf("%d%d%d",&n,&m,&p);
        for (int i=1;i<=n;i++) ff[i]=i;
        for (int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            if (root(x)!=root(y)) unionx(ff[x],ff[y]);
        }
        for (int i=1;i<=p;i++)
        {
            scanf("%d%d",&x,&y);
            if (root(x)==root(y)) cout<<"Yes
    ";
                else cout<<"No
    ";
        }
        return 0;
    }
  • 相关阅读:
    Linux下安装confluence汉化破解版
    某种可以解决一切问题的方法
    普通平衡树(treap)
    文艺平衡树(splay模板)
    [CQOI2015]任务查询系统
    [NOIP2016]天天爱跑步
    NOI2018_Day1_T1_归程
    Picture
    bzoj3524 Couriers
    bzoj2588 counting on a tree
  • 原文地址:https://www.cnblogs.com/liumengyue/p/5186830.html
Copyright © 2011-2022 走看看