NBUT 1218 You are my brother 2010辽宁省赛

Time limit 1000 ms

Memory limit 131072 kB

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.

 

Input

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.

Output

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.

Sample Input

5
1 3
2 4
3 5
4 6
5 6
6
1 3
2 4
3 5
4 6
5 7
6 7

Sample Output

You are my elder
You are my brother


签到题,树的基本概念,代码如下:

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<stack>
 6 #include<map>
 7 #include<set>
 8 #include<queue>
 9 #include<cmath>
10 #include<string>
11 using namespace std;
12 int n,t;
13 int fa[2005];
14 int x,y;
15 int main()
16 {
17     while(~scanf("%d",&n))
18     {
19         memset(fa,0,sizeof(fa));
20         for(int i=1;i<=n;i++)
21         {
22             scanf("%d%d",&x,&y);
23             fa[x]=y;
24         }
25         int la=0,lb=0;
26         int k=1;
27         while(fa[k]!=k)
28         {
29             la++;
30             k=fa[k];
31         }
32         k=2;
33         while(fa[k]!=k)
34         {
35             lb++;
36             k=fa[k];
37         }
38         if (la==lb) printf("You are my brother
");
39         else
40         if (lb>la) printf("You are my younger
");
41         else
42         if (lb<la) printf("You are my elder
");
43     }
44     return 0;
45 }
原文地址:https://www.cnblogs.com/Annetree/p/6641396.html