# [POJ 1014] Dividing【DFS】

• 2018-01-01
• 0
• 0

## Problem:

 Time Limit: 1000MS Memory Limit: 10000K

Description

Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.

Input

Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , n6 , where ni is the number of marbles of value i. So, the example from above would be described by the input-line "1 0 1 2 0 0". The maximum total number of marbles will be 20000.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.

Output

For each collection, output "Collection #k:", where k is the number of the test case, and then either "Can be divided." or "Can't be divided.".
Output a blank line after each test case.

Sample Input

```1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0```

Sample Output

```Collection #1:
Can't be divided.

Collection #2:
Can be divided.```

Source

Mid-Central European Regional Contest 1999

## Solution:

DFS 时从大到小枚举，可以更快地找到答案。

## Code: O(玄学) [320K, 0MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int T = 0, n[7], ave;

inline bool getCollection(){
for(register int i = 1; i <= 6; i++)
if(scanf("%d", n + i) == EOF) return 0;
return 1;
}

inline bool DFS(int tot){
if(tot == ave) return 1;
for(register int i = 6; i >= 1; i--)
if(n[i] && tot + i <= ave){
n[i]--;
if(DFS(tot + i)) return 1;
// Feasibility pruning: not to backdate the n[i], or the program will receive TLE
/*
Reason:
When the attempt of taking value i fails,
we consider giving this value i to the other person,
or we must fail again.
*/
}
return 0;
}

int main(){
while(getCollection() && (n[1] != 0 || n[2] != 0 || n[3] != 0 || n[4] != 0 || n[5] != 0 || n[6] != 0)){
printf("Collection #%d:\n", ++T);
ave = n[1] + 2 * n[2] + 3 * n[3] + 4 * n[4] + 5 * n[5] + 6 * n[6];
if(ave & 1){
puts("Can't be divided.\n");
continue;
}
ave >>= 1;
if(DFS(0)) puts("Can be divided.\n");
else puts("Can't be divided.\n");
}
return 0;
}
```

#### 评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai