-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ-1694-An Old Stone Game.txt
72 lines (58 loc) · 1.08 KB
/
POJ-1694-An Old Stone Game.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//POJ - 1694 - An Old Stone Game
#include "stdio.h"
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <assert.h>
using namespace std;
//max num of nodes
const int MAX = 205;
struct node{
int num;
int childnum;
int childs[MAX];
};
int cmp(const void*a, const void*b)
{
return *(int*)b - *(int*)a;
}
node trees[MAX];
int m, n, p, k;
int getmin(node &snode){
if(snode.childnum == 0)
return 1;
int tchildk[MAX];
int tchildnum = snode.childnum;
for(int i=1; i<=tchildnum; i++){
tchildk[i] = getmin(trees[snode.childs[i]]);
}
qsort(tchildk+1, tchildnum, sizeof(int), cmp);
int ans = tchildk[1];
for(int i=2; i<=tchildnum; i++){
if(ans-i+1 < tchildk[i])
ans += tchildk[i]-(ans-i+1);
}
return ans;
}
int main()
{
scanf("%d", &m);
while(m--)
{
k = MAX;
scanf("%d", &n);
int r;
for(int i=0; i<n; i++){
scanf("%d%d", &p, &r);
trees[p].childnum = r;
for(int j=1; j<=r; j++){
scanf("%d", &trees[p].childs[j]);
}
}
k = getmin(trees[1]);
printf("%d\n", k);
}
return 0;
}