Problem Description
Problem Analysis
This is another problem where I was stuck for a few days because of OJ issues.
Let me copy some thoughts I wrote down at the time.
When there is no contradiction,
if x and y are the same kind, then xA-yA, xB-yB, xC-yC can be merged.
if x eats y, then xA-yB, xB-yC, xC-yA can be merged.
Union-Find is reflected in
merging relations that can hold.
Then by checking whether two elements are in the same set,
we can infer whether this relation can hold.
“The current statement conflicts with some previous true statements, so it is false;”
If a contradictory set relation has already been established for the current statement
(they are already in the same set)
then this statement is naturally false.
So we can solve it using the idea of Union-Find.
Union-Find is used here because of its two operations:
“merge” can turn them into one set relation,
“check” whether they are in the same set.
So based on the above thinking,
we only need to consider each animal as three cases A, B, and C,
and then perform Union-Find operations on the whole.
After all, we are finding false statements,
so as long as a statement cannot be judged wrong,
we can treat it as true.
Therefore we can consider all cases together.
(That is, the “false statements” determined at the end are those that are definitely wrong.)
Here we set
1N as possible A cases,N+N as possible B cases,
N+1
N+N+1~N+N+N as possible C cases.
Note that A, B, and C are not fixed species in a narrow sense.
Rather, since A, B, and C are all assumed,
there is no ordering issue.
That is, it is also fine to understand it internally as
1N as possible B cases,N+N as possible C cases,
N+1
N+N+1~N+N+N as possible A cases,
that is also acceptable.
The key is just to satisfy the A->B->C->A triangular relation.
This is also why the two
ifs about D==1 D==2 in the program
only check X
and do not need to correspondingly check X+N X+2N.
For this problem, note that on POJ, if you usewhile(scanf("%d %d",&N,&K)!=EOF) { }
that is, if multiple inputs are allowed, it will be WA.
So the AC code was changed to allow only one input of N K.
| |
Problem address:
POJ 1182 Food Chain
Because of the judging issue pointed out above on POJ,
I also attach a NYOJ address.
Food Chain
For this problem on NYOJ, writing it as multiple input groups will not cause WA.