Description
Description
The teacher wants to select m from N students as a student overlord, but K of the students are equally powerful. If some of the students are chosen, and others are not, the students will protest. Therefore, the teacher would like to ask you to help him find out the number of students he should choose, so that he will not let the students protest, but as close as possible to the original M.
Train of Thought: Combine the persons with the same strength into a collection to count the number of elements in the collection. Then you can create something similar to the 01 backpack (boolean type) and find the optimal solution.
Code:
# Include <cstdio>
# Include <iostream>
Using namespace STD;
Int sum [30001] = {0}, Fa [30001] = {0}, V [30001] = {0 };
Bool visit [30001] = {0}, F [30001] = {false };
Int Rool (int x)
{
If (Fa [x]! = X) Fa [x] = Rool (Fa [x]);
Return Fa [x];
}
Int main ()
{
Int n, m, K, I, j, A, B, R1, R2, NN = 0, maxn, Minn;
Cin> N> m> K;
For (I = 1; I <= N; ++ I)
{
Fa [I] = I;
Sum [I] = 1;
}
For (I = 1; I <= K; ++ I)
{
Cin> A> B;
R1 = Rool ();
R2 = Rool (B );
If (R1! = R2)
{
Fa [R1] = R2;
Sum [R2] = sum [R2] + sum [R1];
}
}
For (I = 1; I <= N; ++ I)
{
R1 = Rool (I );
If (! Visit [R1])
{
++ Nn;
V [NN] = sum [R1];
Visit [R1] = true;
}
}
F [0] = true;
For (I = 1; I <= nn; ++ I)
For (j = N; j> = V [I]; -- J)
F [J] = f [J] | f [J-V [I];
Maxn = 0;
Minn = N;
For (I = m; I> = 1; -- I)
If (F [I])
{
Maxn = I;
Break;
}
For (I = m + 1; I <= N; ++ I)
If (F [I])
{
Minn = I;
Break;
}
If (m-maxn <Minn-m) cout <maxn <Endl;
If (m-maxn> Minn-m) cout <Minn <Endl;
If (m-maxn = Minn-m) cout <maxn <Endl;
}
(Code is so long that it is despised by the group... Wu Yu)
Codevs3372 selector (easy)