Title link: HDU3290 The magic apple tree
Question: Give you a tree. The value of the starting leaf node is its number itself. After the update is started, if a non-leaf node has K child nodes with values, then its value is updated to the (kth) of these values. +1)/2 is small, find the final value of the root node;
Analysis: From the root node down to DFS, to the leaf node and back up, naked questions;
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int maxn=2e4+7;
vector<int> g[maxn];
int n,rt;
int ind[maxn],oud[maxn],num[maxn];
void dfs(int x)
{
for(auto i:g[x]) dfs(i);
int k=0,tmp[maxn];
for(auto i:g[x])
if(num[i]) tmp[++k]=num[i];
if(!k) {num[x]=x;return;}
sort(tmp+1,tmp+1+k);
num[x]=tmp[(1+k)>>1];
}
void rua()
{
for(int i=1;i<=n;i++) ind[i]=oud[i]=num[i]=0,g[i].clear();
for(int i=1;i<=n;i++)
{
int x,y;scanf("%d",&x);
oud[i]=x;
while(x--) scanf("%d",&y),g[i].pb(y),ind[y]++;
}
for(int i=1;i<=n;i++) if(!oud[i]) num[i]=i;
for(int i=1;i<=n;i++)
if(!ind[i]) {rt=i;dfs(i);break;}
printf("%d\n",num[rt]);
}
int main()
{
while(~scanf("%d",&n)) rua();
return 0;
}