DFS, some nodes have a parent-child relationship, and the node with a parent-child relationship must not appear at the same time
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Define N 6001
Struct node // The right sibling method of the second left child
{
Int parent;
Int child;
Int brother;
Int attend;
Int notattend;
Int Max ()
{
Return attend> notattend? Attend: notattend;
}
Void init ()
{
Parent = 0;
Child = 0;
Brother = 0;
Notattend = 0;
}
} P [N];
Void dfs (int x)
{
Int child;
Child = p [x]. child;
While (child)
{
Dfs (child );
P [x]. attend + = p [child]. notattend;
P [x]. notattend + = p [child]. Max ();
Child = p [child]. brother;
}
}
Int main ()
{
Int I, n, a, B;
While (scanf ("% d", & n )! = EOF)
{
For (I = 1; I <= n; I ++)
{
P [I]. init ();
Scanf ("% d", & p [I]. attend );
}
While (scanf ("% d", & a, & B), a | B)
{
P [a]. parent = B;
P [a]. brother = p [B]. child;
P [B]. child =;
}
For (I = 1; I <= n; I ++)
If (p [I]. parent = 0)
{
Dfs (I );
Printf ("% d \ n", p [I]. Max ());
Break;
}
}
Return 0;
}
By Cambridgeacm