HLJUOJ1002(并查集变形求结点高度)
2014-11-21 20:03
134 查看
1002: You are my brother
Time Limit: 1 Sec Memory Limit:128 MB
Submit: 33 Solved: 16
[Submit][Status][Web Board]
Description
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 hiselder, 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和结点2向上回溯并记录高度,最后高度作比较即可
完整代码:
#include <functional> #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <numeric> #include <cstring> #include <climits> #include <cassert> #include <complex> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") typedef long long LL; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL; /** Constant List .. **/ //{ const int MOD = int(1e9)+7; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const DB PI = acos(-1.0); //M_PI; const int maxn= 100001; int f[maxn]; void init() { for(int i = 0 ; i < maxn ; i ++) f[i] = i; } int main() { #ifdef DoubleQ freopen("in.txt","r",stdin); #endif int n; while(~scanf("%d",&n)) { init(); for(int i = 0 ; i < n ; i ++) { int a , b; scanf("%d%d",&a,&b); f[a] = b; } int cnt1 = 0 , cnt2 = 0; int k1 = 1 , k2 = 2; while(k1 != f[k1]) { cnt1 ++; k1 = f[k1]; } while(k2 != f[k2]) { cnt2 ++; k2 = f[k2]; } if(cnt1 > cnt2) { printf("You are my elder\n"); } else if(cnt1 < cnt2) { printf("You are my younger\n"); } else printf("You are my brother\n"); } }
相关文章推荐
- 2013杭电warm up1 1002 Pet 求树结点的高度(bfs,dfs两种求法)
- 2013杭电warm up1 1002 Pet 求树结点的高度
- 2013杭电warm up1 1002 Pet 求树结点的高度(bfs,dfs两种求法)
- You are my brother (并查集之变形——查找步数)
- 生成一棵最少结点,高度为height的AVL树C语言
- 算法导论6.3-3 证明完全二叉树高度为h的结点个数上限「n/2^(h+1)」
- (hdu step 5.1.5)Dragon Balls(求并查集的根节点、节点数和个结点的移动次数)
- 2017百度之星资格赛 1002 度度熊的王国战略(并查集 or 无向图最小割)
- HDOJ 4496 D-City(并查集变形,逆序 删边)
- Aizu 2170 Marked Ancestor(并查集变形)
- 变形课杭电1181——深搜,广搜,并查集
- 7-10 社交网络图中结点的“重要性”计算(30 point(s)) 【并查集+BFS】
- 并查集变形 - 敌人
- 父母结点数组表示的树的高度
- Android图片自适应屏幕高度,拒绝拉伸变形
- 【HDU1181】变形课(并查集)
- 蒟蒻对并查集的实际应用2:求无向图最大连通块结点数
- 计算二叉树的高度和结点数
- lightoj 1002 - Country Roads(最短路变形)
- 【并查集变形】POJ 2236——Wireless Network