锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 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 棰樻剰錛氱粰鍑烘爲(wèi)鐨勭粨鐐逛箣闂寸殑鍏崇郴,濡?:(2) 2 3
錛岃〃紺?鍜?鐨勭埗浜查兘鏄?.鍦?鐐硅鐐癸紝鍒欏叾閭繪帴鐐?鍜?閮藉彲琚鐩栵紝姹傚嚭鏈灝戣澶氬皯涓偣鎵嶈兘瑕嗙洊鏁存5鏍?wèi)銆傚嵆鏈灝忛《鐐硅鐩栥?/p>
鍒嗘瀽錛氭爲(wèi)鐨勫悇鐐逛箣闂村彧鏈変竴鏉¤竟錛岃屼笖鏍?wèi)鏄庢槃勭壱?guī)ф槸鏈夊眰嬈$殑鏁版嵁緇撴瀯銆俧[u][0]琛ㄧず浠涓烘牴涓攗涓嶅姞鍏ヨ鐩栭泦鐨勬渶灝忛《鐐硅鐩栵紝f[u][1]琛ㄧず浠涓烘牴涓攗鍔犲叆瑕嗙洊闆嗙殑鏈灝忛《鐐硅鐩栥傛瘡涓偣閮芥湁涓や釜鐘舵侊紝灝辨槸鍔犲叆涓庝笉鍔犲叆瑕嗙洊闆嗐?/p>
鏈浼樺瓙緇撴瀯錛氳鐭ラ亾f[u][0]=鍙渶鐭ラ亾u鐨勫瓙鑺傜偣涓暟錛涜鐭ラ亾f[u][1]錛屽垯瑕佺煡閬撳垎鍒互u鐨勫瀛恦浠負(fù)鏍圭殑鏈灝忛《鐐硅鐩栵紝瀵硅繖縐嶆儏鍐碉紝姣忎釜瀛╁瓙鍙姞鍏ヤ篃鍙笉鍔犲叆瑕嗙洊闆嗭紙鍙栨渶浼樼殑錛夛紝浠ヤ負(fù)鏍?wèi)鏈夊眰娆″Q屾墍浠ヨ鐭ラ亾鏍歸儴錛屽氨瑕佺煡閬撳瀛愮殑鏈灝忛《鐐硅鐩栵紝瀛╁瓙鐨勬渶灝忛《鐐硅鐩栬窡鐖朵翰娌℃湁鍏崇郴銆?/p>
瀛愰棶棰橀噸鍙狅細(xì)榪欓噷娌℃湁瀛愰棶棰橀噸鍙犮?/p>
杈圭晫闂錛氭瘡涓妭鐐瑰搴斾袱縐嶈竟鐣岄棶棰橈紝鍗沖綋澶勭悊涓涓妭鐐規(guī)槸錛屾湁鍙栧拰涓嶅彇涓ょ鎯呭喌銆?/p>
瀛愰棶棰樼嫭绔嬶細(xì)瀵逛簬u鐨勫瀛恦1錛寁2錛歷1鐨勫瀛愯窡v2鐨勫瀛愭鏃犲叧緋匯傝繖縐嶆ц川寤朵幾鍒皍鐨勬墍鏈夊瀛愪笂錛屽嵆姣忎釜瀛╁瓙鐨勮В浜掔浉娌℃湁鍏崇郴銆?/p>
浠g爜錛?/p>
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;
}
]]>