Party at Hali-Bula-----트리 dp

Party at Hali-Bula
Time Limit: 2000MS
 
Memory Limit: 65536K
Total Submissions: 2687
 
Accepted: 999
Description
Dear Contestant,
I'm going to have a party at my villa at Hali-Bula to celebrate my retirement from BCM. I wish I could invite all my co-workers, but imagine how an employee can enjoy a party when he finds his boss among the guests! So, I decide not to invite both an employee and his/her boss. The organizational hierarchy at BCM is such that nobody has more than one boss, and there is one and only one employee with no boss at all (the Big Boss)! Can I ask you to please write a program to determine the maximum number of guests so that no employee is invited when his/her boss is invited too? I've attached the list of employees and the organizational hierarchy of BCM.
Best,--Brian Bennett
P.S. I would be very grateful if your program can indicate whether the list of people is uniquely determined if I choose to invite the maximum number of guests with that condition.
Input
The input consists of multiple test cases. Each test case is started with a line containing an integer n (1 ≤ n ≤ 200), the number of BCM employees. The next line contains the name of the Big Boss only. Each of the following n-1 lines contains the name of an employee together with the name of his/her boss. All names are strings of at least one and at most 100 letters and are separated by blanks. The last line of each test case contains a single 0.
Output
For each test case, write a single line containing a number indicating the maximum number of guests that can be invited according to the required condition, and a word Yes or No, depending on whether the list of guests is unique in that case.
Sample Input
6
Jason
Jack Jason
Joe Jack
Jill Jason
John Jack
Jim Jill
2
Ming
Cho Ming
0

Sample Output
4 Yes
1 No

Source
Tehran 2006
#include #include #define Max(a, b) (a > b ? a : b) int son[205][205], unique[205][2], ans[205][2], last, flag; char name[205][205]; int get_id(char str[]) { int i; for (i = 0; i < last; i++) { if(strcmp(name[i],str) ==0) { return i; } }//지금까지 나타나지 않았는데name에 저장된if(i==last) {strcpy(name [i],str);last++;return i;}return 0; } void dfs(int father) {int i,sum,temp;sum=son[father][0]; for(i=1;i<=sum;i+) {temp=son[father][i]; dfs(temp);/상태 전이 방정식 ans[father][1]+=ans[temp][0];ans[father][0]+=Max(ans[temp][0],ans[temp][1]);//유일한 고려 [father][1] & & ans[temp][0]== ans[temp][1]) {flag=0;}/}int main() {//freopen("1.txt", "r", stdin); int i, n, a, b; char str[105], eme[105], boss[105]; while (scanf("%d", &n)&&n) { last = 0; flag = 1; for (i = 0; i < n; i++) { ans[i][1] = 1; ans[i][0] = 0; } memset(son, 0, sizeof(son)); memset(name, '/0', sizeof(name)); memset(unique, 1, sizeof(unique)); scanf("%s", str); get_id(str);//son으로 데이터를 저장하는 것은 체인 테이블의 형식이다.for (i = 1; i < n; i++) { scanf("%s%s", eme, boss); a = get_id(eme); b = get_id(boss); son[b][++son[b][0]] = a; }//for(i=0;i ans[0][1]) { printf("%d %s/n", ans[0][0], flag ? "Yes": "No"); } else if (ans[0][0] < ans[0][1]) { printf("%d %s/n", ans[0][1], flag ? "Yes": "No"); } else { printf("%d No/n", ans[0][0]); } } return 0; }

좋은 웹페이지 즐겨찾기