锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Description Farmer
John has decided to give each of his cows a cell phone in hopes to
encourage their social interaction. This, however, requires him to set
up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so they can all communicate. Exactly N-1 pairs of pastures are adjacent, and for any two pastures A and B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B) there is a sequence of adjacent pastures such that A is the first pasture in the sequence and B
is the last. Farmer John can only place cell phone towers in the
pastures, and each tower has enough range to provide service to the
pasture it is on and all pastures adjacent to the pasture with the cell
tower. Help him determine the minimum number of towers he must install to provide cell phone service to each pasture. Input * Line 1: A single integer: N Output * Line 1: A single integer indicating the minimum number of towers to install Sample Input Sample Output Bob enjoys playing computer games, especially strategic
games, but sometimes he
cannot find the solution fast enough and then he is very sad. Now he
has the
following problem. He must defend a medieval city, the roads of which
form a
tree. He has to put the minimum number of soldiers on the nodes so
that they
can observe all the edges. Can you help him? the solution is one soldier ( at the node 1). 4 1 棰樻剰錛氱粰鍑烘爲鐨勭粨鐐逛箣闂寸殑鍏崇郴,濡?:(2) 2 3
錛岃〃紺?鍜?鐨勭埗浜查兘鏄?.鍦?鐐硅鐐癸紝鍒欏叾閭繪帴鐐?鍜?閮藉彲琚鐩栵紝姹傚嚭鏈灝戣澶氬皯涓偣鎵嶈兘瑕嗙洊鏁存5鏍戙傚嵆鏈灝忛《鐐硅鐩栥?/p>
鍒嗘瀽錛氭爲鐨勫悇鐐逛箣闂村彧鏈変竴鏉¤竟錛岃屼笖鏍戞槑鏄劇殑鐗規ф槸鏈夊眰嬈$殑鏁版嵁緇撴瀯銆俧[u][0]琛ㄧず浠涓烘牴涓攗涓嶅姞鍏ヨ鐩栭泦鐨勬渶灝忛《鐐硅鐩栵紝f[u][1]琛ㄧず浠涓烘牴涓攗鍔犲叆瑕嗙洊闆嗙殑鏈灝忛《鐐硅鐩栥傛瘡涓偣閮芥湁涓や釜鐘舵侊紝灝辨槸鍔犲叆涓庝笉鍔犲叆瑕嗙洊闆嗐?/p>
鏈浼樺瓙緇撴瀯錛氳鐭ラ亾f[u][0]=鍙渶鐭ラ亾u鐨勫瓙鑺傜偣涓暟錛涜鐭ラ亾f[u][1]錛屽垯瑕佺煡閬撳垎鍒互u鐨勫瀛恦浠負鏍圭殑鏈灝忛《鐐硅鐩栵紝瀵硅繖縐嶆儏鍐碉紝姣忎釜瀛╁瓙鍙姞鍏ヤ篃鍙笉鍔犲叆瑕嗙洊闆嗭紙鍙栨渶浼樼殑錛夛紝浠ヤ負鏍戞湁灞傛錛屾墍浠ヨ鐭ラ亾鏍歸儴錛屽氨瑕佺煡閬撳瀛愮殑鏈灝忛《鐐硅鐩栵紝瀛╁瓙鐨勬渶灝忛《鐐硅鐩栬窡鐖朵翰娌℃湁鍏崇郴銆?/p>
瀛愰棶棰橀噸鍙狅細榪欓噷娌℃湁瀛愰棶棰橀噸鍙犮?/p>
杈圭晫闂錛氭瘡涓妭鐐瑰搴斾袱縐嶈竟鐣岄棶棰橈紝鍗沖綋澶勭悊涓涓妭鐐規槸錛屾湁鍙栧拰涓嶅彇涓ょ鎯呭喌銆?/p>
瀛愰棶棰樼嫭绔嬶細瀵逛簬u鐨勫瀛恦1錛寁2錛歷1鐨勫瀛愯窡v2鐨勫瀛愭鏃犲叧緋匯傝繖縐嶆ц川寤朵幾鍒皍鐨勬墍鏈夊瀛愪笂錛屽嵆姣忎釜瀛╁瓙鐨勮В浜掔浉娌℃湁鍏崇郴銆?/p>
浠g爜錛?/p>
* Lines 2..N: Each line specifies a pair of adjacent pastures with two space-separated integers: A and B5
1 3
5 2
4 3
3 52
棰樻剰錛氬啘姘戠殑n涓湴鏂歸渶瑕佸緩鐢佃瘽浜紝A鏈夌數璇濓紝鍒橝鐩擱偦鐨勭偣鍙互瑕佺數璇濅涵涔熷彲浠ヤ笉闇瑕佺數璇濅涵銆?/span>
鍒嗘瀽錛氭渶灝戠偣瑕嗙洊銆?/span>
鍒嗕笁縐嶆儏鍐碉細
dp[u][0]u寤轟釜浜?/span>
dp[u][1]u涓嶅緩浜紝涓嶄緷璧栫埗鑺傜偣錛屼絾u鐨勫瀛愬繀欏繪湁涓涓湁浜?/span>
dp[u][2]u涓嶅緩浜紝渚濊禆浜庣埗鑺傜偣錛堝彧瑕佺埗鑺傜偣鑳藉緩涓涵錛夈?/span>
浠g爜錛?/span>
#include <stdlib.h>
#define inf (1 << 29)
#define Min(a, b) (a) < (b) ? (a) : (b)
#define maxn 21000
struct T
{
int v, next;
}fn[maxn];
int th;
int g[maxn], dp[maxn][3], degree[maxn];
void add(int u, int v)
{
fn[th].v = v, fn[th].next = g[u], g[u] = th++;
}
void dfs(int u, int f)
{
if (u != 1 && degree[u] == 1)
{
dp[u][0] = 1;
dp[u][1] = inf;
dp[u][2] = 0;
return;
}
dp[u][0] = 1;
int i, v, sign, min, del;
for (i = g[u], sign = 0, min = inf; i != -1; i = fn[i].next)
{
v = fn[i].v;
if (v == f)
{
continue;
}
dfs(v, u);
//濡傛灉鍙杣錛屽垯瀛╁瓙v鍙朚IN(鐨勫彲鍙朳v琚嚜宸辮鐩朷錛屼緷璧栧瀛愮殑瀛╁瓙[琛ㄧずv涓嶇敤宸茬粡琚鐩栵紝涓嶄緷璧杣]錛屼緷璧栫埗鑺傜偣u[鍥犱負鐖惰妭鐐圭幇鍦ㄥ彇浜哴)
dp[u][0] += Min(Min(dp[v][0], dp[v][1]), dp[v][2]);
if (dp[v][0] <= dp[v][1])//u涓嶅彇涔熶笉渚濊禆浜庣埗鑺傜偣錛屽垯u渚濊禆浜庡瀛愶紝淇濊瘉涓涓瀛愯鍙栥?/span>
{
dp[u][1] += dp[v][0];
sign = 1;//閫変簡涓涓湁鐢佃瘽鐨勶紝鏍囧織1
}
else//閫変簡涓涓瀛愭病鐢佃瘽鐨勶紝瑕佷繚瀛榤in錛屽鏋渟ign娌℃爣蹇楄繃錛屽氨寰楀埄鐢ㄦ湁鐢佃瘽鐨勬渶灝忓煎緱閭d釜浜恒?/span>
{
dp[u][1] += dp[v][1];
if (min > dp[v][0])
{
min = dp[v][0];
del = dp[v][1];
}
}
dp[u][2] += Min(dp[v][0], dp[v][1]);//u涓嶅彇錛屽垯u渚濊禆浜庣埗鑺傜偣錛屽垯鍙杤涓嶄緷璧栦笌u鐨勬渶浼樻儏鍐?/span>
}
if (!sign)
{
dp[u][1] += min - del;
}
}
int main()
{
int n, u, v, i, j;
while (scanf("%d", &n) - EOF)
{
th = 0;
for (i = 0; i <= n; i++)
{
degree[i] = 0;
g[i] = -1;
dp[i][0] = dp[i][1] = dp[i][2] = 0;
}
for (i = 1; i < n; i++)
{
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
degree[u]++;
degree[v]++;
}
if (n == 1)
{
printf("0\n");
continue;
}
dfs(1, 0);
printf("%d\n", Min(dp[1][0], dp[1][1]));
}
return 0;
}
]]>
Your program should find the minimum number of soldiers that Bob has
to put
for a given tree.
The input file contains several data sets in text format. Each data
set represents
a tree with the following description:
the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2
... node_identifier
or
node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n
nodes (0 <
n <= 1500). Every edge appears only once in the input data.
For example for the tree:
The output should be printed on the standard output. For each given
input data
set, print one integer number in a single line that gives the result
(the minimum
number of soldiers). An example is given in the following table:
Input
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)
Output
2
#define Min(a, b) a < b ? a : b
#define maxn 1510
int up[maxn], f[maxn][2], n;
void dp(int u)
{
if (f[u][1] != -1)
{
return;
}
int i, v;
for (v = 0, f[u][1] = 1, f[u][0] = 0; v < n; v++)//瀵逛簬姣忎釜鐐癸紝閫掑綊鍒版瘡涓瀛愯В鍐抽棶棰樺悗鎵嶈兘瑙e喅鑷韓闂
{
if (up[v] == u)
{
dp(v);
f[u][1] += Min(f[v][0], f[v][1]);
f[u][0] += f[v][1];
}
}
}
int main()
{
int i, u, v, m, root;
while (scanf("%d", &n) != EOF)
{
for (i = 0; i < n; i++)
{
up[i] = -1;
f[i][0] = f[i][1] = -1;
}
for (i = 0; i < n; i++)
{
scanf("%d:(%d)", &u, &m);
while (m--)
{
scanf("%d", &v);
up[v] = u;
}
}
root = 0;
while (up[root] != -1)
{//鏌ユ壘鏍圭粨鐐?nbsp;
root = up[root];
}
dp(root);
printf("%d\n", Min(f[root][1], f[root][0]));
}
return 0;
}
]]>