传送门:点击打开链接
题意:告诉你n个关系,关系(u,v)表示v是u的父亲,现在A是节点1,B是节点2,问A和B的辈分关系
思路:这肯定是树型结构,先建立(u,v)的有向边,找到A的祖先root,然后再从root出发看A和B的深度是多少
然后比较A和B的深度。
trick:A和B可能并不是一个祖先,也就是说A和B可能没有血缘关系,给出的数据并不是一棵树,而是森林,此时应该输出You are my brother
#include<map> #include<set> #include<cmath> #include<stack> #include<queue> #include<cstdio> #include<string> #include<vector> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #define FIN freopen("input.txt","r",stdin) using namespace std; typedef long long LL; typedef pair<int, int> PII; const int MX = 6e3 + 5; const int INF = 0x3f3f3f3f; int Head[MX], Next[MX], rear; struct Edge { int u, v, cost; } E[MX], S[MX]; void edge_init() { rear = 0; memset(Head, -1, sizeof(Head)); } void edge_add(int u, int v, int cost = 0) { E[rear].u = u; E[rear].v = v; E[rear].cost = cost; Next[rear] = Head[u]; Head[u] = rear++; } int DFS(int u) { for(int i = Head[u]; ~i; i = Next[i]) { int v = E[i].v; return DFS(v); } return u; } void solve(int u, int d, int &a, int &b) { if(u == 1) a = d; if(u == 2) b = d; for(int i = Head[u]; ~i; i = Next[i]) { int v = E[i].v; solve(v, d + 1, a, b); } } int main() { int n, u, v; //FIN; while(~scanf("%d", &n)) { edge_init(); for(int i = 1; i <= n; i++) { scanf("%d%d", &u, &v); S[i].u = u; S[i].v = v; edge_add(u, v); } int root = DFS(1); edge_init(); for(int i = 1; i <= n; i++) { edge_add(S[i].v, S[i].u); } int a = -1, b = -1; solve(root, 0, a, b); if(a == -1 || b == -1) { printf("You are my brother\n"); continue; } if(a == b) { printf("You are my brother\n"); } else if(a > b) { printf("You are my elder\n"); } else { printf("You are my younger\n"); } } return 0; }