zoukankan      html  css  js  c++  java
  • 5929 亲戚

    5929 亲戚

     

    时间限制: 1 s
    空间限制: 128000 KB
    题目等级 : 黄金 Gold
     
     
     
    题目描述 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

    题目中已有,不重复解释。

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 int fa[6000];
     5 int find(int x)
     6 {
     7     if(fa[x]!=x)return fa[x]=find(fa[x]);
     8     else return x;
     9 }
    10 int un(int x,int y)
    11 {
    12     fa[x]=y;
    13 }
    14 int main()
    15 {
    16     int n,m;
    17     int q;
    18     
    19     cin>>n>>m; cin>>q;
    20     for(int i=1;i<=n;i++)
    21      {
    22          fa[i]=i;
    23      }
    24      int x,y;
    25      for(int i=1;i<=m;i++)
    26       {
    27           cin>>x>>y;
    28           int r=find(x);
    29           int rr=find(y);
    30         if(r!=rr)
    31          {
    32              un(r,rr);
    33          }
    34      }
    35      int s,d;
    36      for(int i=1;i<=q;i++)
    37       {
    38           cin>>s>>d;
    39           if(find(s)==find(d))
    40            {
    41                cout<<"Yes
    ";
    42            }
    43         else cout<<"No
    ";   
    44       }
    45       return 0;
    46 }
  • 相关阅读:
    P2704 [NOI2001]炮兵阵地[状压dp]
    【CRT】中国剩余定理简介
    乘法逆元
    P2921 [USACO08DEC]在农场万圣节[SCC缩点]
    P1967 货车运输[生成树+LCA]
    P2746 P2812 [USACO5.3]校园网Network of Schools[SCC缩点]
    Debian初始化配置
    Linux网桥配置
    Jenkins与Gitlab集成
    Gitlab搭建
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/6700473.html
Copyright © 2011-2022 走看看