Http://poj.org/problem? Id = 1463
There are two methods: Binary Graph and tree DP. Both of them are done here.
Of the bipartite graph.
This is the minimum point coverage of a classic bipartite graph. To associate all edges with these vertices, We need to select the smallest vertex (one edge can be associated with two vertices for the sake of need)
Then we built a undirected edge, because it can be associated with each other, or we can use the classic splitting idea,
I and J have edges in the source image. We need to connect I to J + N, J to I + N, but the effect of this connection is the same as that of I and J. So to save space, we can connect
Then HungaryAlgorithm, Minimum vertex overwrite = maximum number of matches
View code
# Include <iostream> # Include < String . H> # Include <Stdio. h> # Include <Algorithm> # Include <Vector># Define Maxn 2000 Using Namespace STD; vector < Int > Node [maxn]; Int Mm [maxn]; Int Visit [maxn]; Int N; Int DFS ( Int FA ){ For ( Int I = 0 ; I <node [fa]. Size (); I ++ ){ Int V = Node [fa] [I]; If (! Visit [v]) {visit [v] = 1 ; If (Mm [v] =- 1 | DFS (Mm [v]) {mm [v] =Fa; Return 1 ;}}} Return 0 ;} Void Solve (){ Int CNT = 0 ; Memset (mm, - 1 , Sizeof (Mm )); For ( Int I = 0 ; I <n; I ++ ) {Memset (visit, 0 , Sizeof (Visit )); If (DFS (I) CNT ++ ;} Cout <CNT/ 2 < Endl; // Because the two-way edge is connected, the number of matching results is twice the number of required answers. } Int Main (){ Int U, num; Int V; While (CIN> N ){ For ( Int I = 0 ; I <= N; I ++ ) Node [I]. Clear (); For ( Int I = 0 ; I <n; I ++) {Scanf ( " % D :( % d) " , & U, & num ); // Note that While (Num -- ) {CIN > V; node [u]. push_back (v ); // Bidirectional EDGE connection Node [v]. push_back (u) ;}} solve ();} Return 0 ;}
Tree DP
DP [I] [0] indicates the minimum number of troops required for the subtree With This vertex as the root.
DP [I] [1] indicates the minimum number of troops required for the child tree with this vertex as the root.
Then the state equation can be obtained.
DP [fa] [0] + = DP [son] [1]; // if the father is not allowed, the son must
DP [fa] [1] + = min (DP [son] [0], DP [son] [1]); // if the father puts, the son selects the smallest value between putting and not putting.
During access, because the son's information needs to be first known, it is similar to subsequent traversal.
View code
# Include <iostream> # Include < String . H> # Include <Stdio. h> # Include <Algorithm> # Include <Vector># Define Maxn 100000 Using Namespace STD; Int DP [maxn] [ 2 ]; Int N; Int Root; vector < Int > Node [maxn]; Void Init (){ Int U, V, num; root =- 1 ; For ( Int I = 0 ; I <= N; I ++ ) Node [I]. Clear (); For ( Int I = 0 ; I <n; I ++ ) {Scanf ( " % D :( % d) " , & U, & num );// Note that If (Root =- 1 ) Root = u; // The first point is the root While (Num -- ) {CIN > V; node [u]. push_back (v ); // Directed EDGE connection }}} Int Solve ( Int FA) {DP [fa] [ 0 ] = 0 ; DP [fa] [ 1 ] = 1 ; For ( Int I = 0 ; I <node [fa]. Size (); I ++ ){ Int Son = Node [fa] [I]; solve (son ); // In fact, I think it is similar to subsequent traversal, because my father needs to use his son's information, // So I want to know my son first. DP [fa] [ 0 ] + = DP [son] [ 1 ]; // If the father does not, the son must DP [fa] [ 1 ] + = Min (DP [son] [ 0 ], DP [son] [ 1 ]); // If the father puts it, the son selects the smallest value between the father and the child. } Return Min (DP [fa] [0 ], DP [fa] [ 1 ]);} Int Main (){ While (CIN> N) {Init (); printf ( " % D \ n " , Solve (Root )); // } Return 0 ;}