当前位置: 代码迷 >> 综合 >> You are my brother
  详细解决方案

You are my brother

热度:29   发布时间:2023-10-18 04:51:35.0

You are my brother

时间限制: 1 Sec 内存限制: 128 MB


题目描述

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

题意概括

给出数字之间的关系,问1和2的辈分关系;

解题思路

根据所给数据,搜索计算出1和2分别到祖先的距离,根据到祖先的距离然后判断出1和2的辈分关系、

代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<algorithm>
using namespace std;int line[2010][2010];
int dfs(int x,int num)
{int i,j;num++;for(i=0;i<=2000;i++){if(line[x][i]){return dfs(i,num);}}return num;
}
int main()
{int n;int i,j,a,b;while(scanf("%d",&n)!=EOF){memset(line,0,sizeof(line));for(i=0;i<n;i++){scanf("%d %d",&a,&b);line[a][b]=1;}a=dfs(1,0);b=dfs(2,0);if(a<b){printf("You are my younger\n");}else if(a==b){printf("You are my brother\n");}else{printf("You are my elder\n");}}return 0;
}