解题思路:有点像并查集,但是这道题更简单,用数组形式表示树,然后比较深度即可。
注意事项:
参考代码:
#include<iostream> using namespace std; int p[2005]; void init() { for (int i = 0; i < 2005; i++) p[i] = i; } int depth(int i) { int rtn = 0; while (p[i] != i) { rtn++; i = p[i]; } return rtn; } int main() { int n; int a, b; while (cin >> n) { init(); for (int i = 0; i < n; i++) { cin >> a >> b; p[a] = b; } int d1, d2; d1 = depth(1); d2 = depth(2); if (d1 < d2) cout << "You are my younger\n"; else if (d1 > d2) cout << "You are my elder\n"; else cout << "You are my brother\n"; } return 0; }
0.0分
1 人评分
C二级辅导-计负均正 (C语言代码)浏览:556 |
C语言训练-字符串正反连接 (C语言代码)浏览:664 |
C语言训练-数字母 (C语言代码)浏览:610 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:940 |
printf基础练习2 (C语言代码)浏览:826 |
简单的a+b (C语言代码)浏览:626 |
Cylinder (C语言描述,蓝桥杯)浏览:1279 |
水仙花 (C语言代码)浏览:1163 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:594 |
IP判断 (C语言代码)浏览:592 |