"URAL 1244" gentlemen (dp+ record Path)
Main topic:
n cards, each with value. Given a value of V, ask if there is a unique combination of combinations of V.
Some words output is not in the combination of card number, no words output 0, multi-Solution output-1.
01 Backpack + Record the path.
One corner to turn is that for the current value, only the item number of the first arrival is recorded.
After that, the multiple solutions are no longer recorded.
Since 01 backpacks are enumerated and then recursively obtained, if it is the only solution, Dp[v] must not be 0 or-1. But on this path from 0 to V, there may be some points that have been overwritten by the newly added items, but in fact there is no effect on getting v.
Such as
4 4
1
1
2
5
When doing the backpack, after enumerating the first two cards 1 1, dp[1] has actually become-1. But Dp[2] in the second enumeration becomes 1 (unique solution), and records the next card 2. Therefore, when enumerating the first card, dp[1] Records card 1 is no problem, the only solution to the final answer/multi-solution is only related to Dp[v]
The code is as follows:
#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <climits> #include <ctime> #include <cstring> #include <queue> #include <stack&
Gt #include <list> #include <algorithm> #include <map> #include <set> #define LL Long Long #define Pr pair<int,int> #define FREAD (CH) freopen (CH, "R", stdin) #define FWRITE (CH) freopen (CH, "w", stdout) using namespace s
td
const int INF = 0X3F3F3F3F;
const int mod = 1E9+7;
Const double EPS = 1e-8;
const int MAXM = 112345;
const int MAXN = 233;
int DP[MAXM];
int PRE[MAXM];
int NUM[MAXN];
BOOL VIS[MAXN];
int main () {fread ("in.in");
Fwrite ("Out.out");
int v,n;
scanf ("%d%d", &v,&n);
Dp[0] = 1;
for (int i = 1; I <= n; ++i) {scanf ("%d", &num[i]); for (int j = v-num[i]; j >= 0;--j) {if (Dp[j]) {if (dp[j+num[i]] = = 0 & Amp;& Dp[j]! =-1) {Dp[j+num[i]] = 1;
Pre[j+num[i]] = i;
} else Dp[j+num[i]] =-1;
}}} if (Dp[v] > 0) {int tmp = V;
while (TMP) {vis[pre[tmp]] = 1;
TMP-= num[pre[tmp]];
} bool f = 0;
for (int i = 1; I <= n; ++i) {if (vis[i]) continue;
if (f) putchar (");
else F = 1;
printf ("%d", I);
}} else printf ("%d", dp[v]);
Puts ("");
return 0; }