标签:
# include <iostream> # include <fstream> # include <cstring> # include <cstdio> # include <algorithm> # include <vector> using namespace std; vector <int> map[20001]; int is[20001], father[20001]; int dfs(int); char c;//网上查的输入外挂 inline void f(int & x) { while(c = getchar(), c < ‘0‘ || c > ‘9‘); x = c - ‘0‘; while(c = getchar(), c >= ‘0‘ && c <= ‘9‘) x = x * 10 + c - ‘0‘; } int main() { //freopen("aaa.txt", "r", stdin); int n, t; while(scanf("%d", &n) == 1) { memset(is, 0, sizeof(is)); for(int i = 1; i <= n; i++) map[i].clear(); for(int i = 1; i <= n; i++) { f(father[i]); // 直接子节点的父节点 for(int j = 0; j < father[i]; j++) { f(t); map[i].push_back(t); is[t] = 1; // 判断根节点, } } int root; for(int i = 1; i <= n; i++ ) if(is[i] == 0) root = i; printf("%d\n", dfs(root)); } return 0; } int dfs(int root) { vector <int> tep; if(father[root] == 0) // 当子节点为0时, 直接返回号码 return root; for(int i = 0; i < father[root]; i++) tep.push_back(dfs(map[root][i])); sort(tep.begin(), tep.end()); return tep[(father[root] + 1) / 2 - 1]; }
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5427674.html