Little Wish~ lyrical step~
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 417 Accepted Submission(s): 109
Problem Description
N children are living in a tree with exactly N nodes, on each node there lies either a boy or a girl.
A girl is said to be protected, if the distance between the girl and her nearest boy is no more than D.
You want to do something good, so that each girl on the tree will be protected. On each step, you can choose two nodes, and swap the children living on them.
What is the minimum number of steps you have to take to fulfill your wish?
Input
The first line has a number T (T <= 150) , indicating the number of test cases.
In a case, the first line contain two number n (1 <= n <= 50), D (1 <= D <= 10000000), Which means the number of the node and the distance between the girls and boys.
The next lines contains n number. The i
th number means the i
th node contains a girl or a boy. (0 means girl 1 means boy), The follow n - 1 lines contains a, b, w, means a edge connect a
th node and b
th node, and the
length of the edge is w (1 <= w <= 10000000).
Output
For every case, you should output "Case #t: " at first, without quotes. The
t is the case number starting from 1.
Then follows the answer, -1 meas you can't comlete it, and others means the minimum number of the times.
Sample Input
1
3 1
0 0 1
1 2 1
1 3 1
Sample Output
Case #1: 1
Source
2013 ACM/ICPC Asia Regional Chengdu Online
一棵樹n個節點,每一個節點或者有男孩,或者有女孩,每一個男孩可以保護距離他D范圍之內的男孩,問最少通過多少次交換可以使得所有女孩受到保護。
男孩與男孩,女孩與女孩之間的交換顯然毫無意義,只用考慮男孩與女孩之間的交換,可以DP做,也可以DLX爆搜,只考慮男孩,女孩毫無意義,可以先DLX爆搜得到解,然後判斷需要多少次交換,當搜到一組解時,裡面的女孩個數就是需要交換的次數,我們只需要把女孩全部移走,換成男孩,就可以保證把所有點覆蓋。
代碼:
/* ***********************************************
Author :_rabbit
Created Time :2014/5/1 22:56:26
File Name :12.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include