[1218] You are my brother

[1218] You are my brother

  • 时间限制: 1000 ms 内存限制: 131072 K
  • 问题描述
  • Little A gets to know a new friend, Little B, recently. One day, they realize that they are family 500 years ago. Now, Little A wants to know whether Little B is his elder, younger or brother.

  • 输入
  • There are multiple test cases.
    For each test case, the first line has a single integer, n (n<=1000). The next n lines have two integers a and b (1<=a,b<=2000) each, indicating b is the father of a. One person has exactly one father, of course. Little A is numbered 1 and Little B is numbered 2.
    Proceed to the end of file.

  • 输出
  • For each test case, if Little B is Little A’s younger, print “You are my younger”. Otherwise, if Little B is Little A’s elder, print “You are my elder”. Otherwise, print “You are my brother”. The output for each test case occupied exactly one line.

  • 样例输入
  • 5
    1 3
    2 4
    3 5
    4 6
    5 6
    6
    1 3
    2 4
    3 5
    4 6
    5 7
    6 7
    
  • 样例输出
  • You are my elder
    You are my brother
  • 提示
  • 来源
  • 辽宁省赛2010

    题目大意:输入N对数a,b,表示b是a的父亲,最后求的是1和2的关系,也就是看1和2与根的关系(注意,若a和b不在同一棵树上,则他们的关系为brother)

  • 思路:题目中说不存在两个父亲,所以我们可以定义一个数组,把一个数存到这个数上,形如a[1]=3,a[3]=5……,这样每个数就都会连到一起,最后循环找一下根深度就好了,比赛的时候数组开大了,一直超时…………

代码:
 

#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll unsigned long long
#define inf 0x3f3f3f3
#define esp 1e-8
#define bug {printf("mmp\n");}
#define mm(a,b) memset(a,b,sizeof(a))
#define T() int test,q=1;scanf("%d",&test); while(test--)
const int maxn=2e7+100;
const double pi=acos(-1.0);
const int N=2e5+10;
const int mod=1e9+7;
int a[N];
int main()
{

    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        for(int i=0; i<n; i++)
        {

            int aa,bb;
            scanf("%d%d",&aa,&bb);
            a[aa]=bb;
        }
        int ans1=0,ans2=0;
        int f1=1,f2=2;
        while(a[f1])
        {
            ans1++;
            f1=a[f1];
        }
        while(a[f2])
        {
            ans2++;
            f2=a[f2];
        }
        if(ans1==ans2)
            printf("You are my brother\n");
        else if(ans1>ans2)
            printf("You are my elder\n");
        else
            printf("You are my younger\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lee371042/article/details/88880097
my
今日推荐