POJ 1463 Strategic game( 樹形DP )
來源:程序員人生 發(fā)布時(shí)間:2014-09-29 13:26:30 閱讀次數(shù):3205次
題意:一顆 N 個(gè)節(jié)點(diǎn)的樹,在某一個(gè)節(jié)點(diǎn)上放置一個(gè)兵則可以守住與它相鄰的邊。最少放置多少個(gè)兵才可以守住所有的邊。
#include <cstdio>
#include <deque>
using namespace std;
#define ABANDON 0
#define GET 1
deque< int > graph[2010];
int DP[2010][2];
void DFS( int start, int parent ){
DP[start][ABANDON] = 0;
DP[start][GET] = 1;
int target;
if( graph[start].size() == 1 && parent != -1 )
return;
for( int i = 0; i < graph[start].size(); ++i ){
target = graph[start][i];
if( target == parent )
continue;
DFS( target, start );
DP[start][ABANDON] += DP[target][GET];
DP[start][GET] += min( DP[target][GET], DP[target][ABANDON] );
}
}
int main(){
int nodes;
int start, roads, target;
while( scanf( "%d", &nodes ) != EOF ){
for( int i = 0; i <= nodes; ++i )
graph[i].clear();
for( int i = 0; i < nodes; ++i ){
scanf( "%d:(%d)", &start, &roads );
while( roads-- ){
scanf( "%d", &target );
graph[start].push_back( target );
graph[target].push_back( start );
}
}
DFS( 0, -1 );
printf( "%d
", min( DP[0][ABANDON], DP[0][GET] ) );
}
return 0;
}</span>
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)