zoukankan      html  css  js  c++  java
  • CODEVS-1073 家族

    题目描述 Description

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

    输入描述 Input Description

    第一行:三个整数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 Description

    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

    数据范围及提示 Data Size & Hint

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

    弱到爆的并查集,为什么codevs还给它分大师级难度?→_→

     1 #include<iostream>
     2 #include<algorithm>
     3 using namespace std;
     4 
     5 int n,m,p;
     6 int f[5001]={0};
     7 
     8 int find(int x)
     9 {
    10     while(f[x]!=0) x=f[x];
    11     return x;
    12 }
    13 
    14 int main()
    15 {
    16     cin>>n>>m>>p;
    17     for(int i=1;i<=m;i++)
    18     {
    19         int mi,mj,a,b;
    20         cin>>mi>>mj;
    21         a=find(mi);
    22         b=find(mj);
    23         if(a!=b) f[a]=b;
    24     }
    25     for(int i=1;i<=p;i++)
    26     {
    27         int pi,pj;
    28         cin>>pi>>pj;
    29         if(find(pi)==find(pj))
    30             cout<<"Yes"<<endl;
    31         else cout<<"No"<<endl;
    32     }
    33     return 0;
    34 }
  • 相关阅读:
    新autoJS写淘宝福年种福果
    autoJS写淘宝福年种福果
    简七学理财知识
    python一键搭ftp服务器
    域名伪装
    [SWPU2019]Web1
    [网鼎杯 2020 朱雀组]phpweb
    Doc
    Docker简单使用教程
    MySQL数据库基本操作
  • 原文地址:https://www.cnblogs.com/InWILL/p/5890970.html
Copyright © 2011-2022 走看看