1726. The Smallest Setting That Works
The input is problem 1725's: n switches, and k rules each demanding that at
least one of two conditions holds, where i means switch i is ON and -i
means switch i is OFF.
Print a setting that satisfies every rule, as a string of n characters where
1 means ON and 0 means OFF. Print No if no setting works.
Many settings may work, so the one wanted is pinned: print the
LEXICOGRAPHICALLY SMALLEST string. Since 0 sorts before 1, that means
switch 1 is OFF if any working setting has it OFF, then switch 2 is OFF if any
working setting with the already-fixed earlier switches has it OFF, and so on.
2 switches, rules (1 or 2), (not 1 or not 2) -> 01
2 switches, rules (1 or 2), (1 or not 2), (not 1 or 2) -> 11
1 switch, no rules -> 0
1 switch, rules (1 or 1), (not 1 or not 1) -> No
The second example is the one to study. Switch 1 OFF forces switch 2 ON through
the first rule and switch 2 OFF through the second, which is a contradiction, so
switch 1 must be ON and the answer starts with 1.
## What most people try first
Taking the setting that falls out of the standard test. Problem 1725's proof
constructs a satisfying setting by comparing, for each switch, the two groups
its conditions lie in and choosing the one whose group comes later in a
topological order. That is a perfectly good setting and it is usually NOT the
lexicographically smallest, so it fails here. The example (1 or 2) alone has
groups that put switch 1 ON, while 01 is smaller and also works.
The second attempt is to run the Yes or No test n times, once per switch,
each time adding a rule that forces the switch the way you hope. That is
correct, and each test is O(n + k), so the whole thing is O(n(n + k)),
which at n = 100000 is 3 * 10^10 steps.
## Decide in order, and propagate
Check satisfiability once, with problem 1725's test. If it fails, print No and
stop. If it holds, walk the switches in order and for each undecided one:
try setting it OFF, and FORCE everything that follows:
follow the implication arrows out of "this switch is OFF",
marking every condition reached as true
if a condition and its opposite both end up true, this choice is dead:
undo every mark this attempt made, and set the switch ON instead,
forcing again
Forcing is a plain search along the implication arrows, so each attempt costs
O(n + k) in the worst case. The reason the whole loop is still fast is that a
condition is only ever marked once SUCCESSFULLY: marks that survive are never
undone and never revisited, so the total work of all the successful attempts is
O(n + k). Only the failed attempts can repeat work, and a switch fails at most
once, so the total is O(n + k) amortised over the successes plus the cost of
the at most n failures, which is what makes this practical rather than
quadratic in practice.
## Why trying OFF first gives the smallest string
Standard exchange reasoning. Suppose the answer has switch 1 OFF. Then trying OFF first either succeeds, matching it, or fails; and it can only fail if setting switch 1 OFF forces a contradiction, which would mean no working setting has switch 1 OFF at all. So the first character is right. Having fixed it, the remaining problem is the same question on the switches left undecided, and induction finishes the argument.
The important part is that the forced propagation never has to be revisited.
Once switch i is settled and its consequences marked, every later choice is
compatible with them, because the formula was satisfiable to begin with and each
settled choice was shown not to destroy that.
## Why the satisfiability check has to come first
Without it, the greedy can get as far as switch j, find that both OFF and ON
contradict, and have no idea whether to backtrack on an earlier switch or report
No. Running problem 1725's test up front rules out the first possibility: if
the formula IS satisfiable, then at each step at least one of the two choices
survives, so the loop never gets stuck and never backtracks.
## Boundaries
k = 0 prints n zeros. A switch no rule mentions stays OFF, because nothing
forces it. A rule naming the same switch twice, like (1 or 1), forces that
switch ON and the greedy discovers it when the OFF attempt contradicts itself
immediately.
The propagation is ITERATIVE. A chain of k rules makes a path through 2n
conditions, and at n = 100000 a recursive forcing routine overflows Python's
recursion limit and the default stack in C, C++ and Java.
## Related
Problem 1725 answers only whether a setting exists. Problem 1727 is the same machinery hidden inside a seating problem. Problem 1718 is where the component ORDER that the non-greedy construction relies on comes from.
Constraints - `1 ≤ n ≤ 100000` - `0 ≤ k ≤ 200000` - `1 ≤ abs(a), abs(b) ≤ n` and neither is zero - the two conditions of a rule may name the same switch, in either sign - the same rule may be listed more than once - a switch may be named by no rule at all
Input
The first line contains two integers n and k, the number of switches and the number of rules.
Each of the next k lines contains two nonzero integers a and b with 1 ≤ abs(a), abs(b) ≤ n, a rule demanding that at least one of the two conditions holds. A positive value i is the condition that switch i is ON and a negative value -i that switch i is OFF.
Output
Print one line holding exactly n characters with no separators, the ith being 1 if switch i is ON and 0 if it is OFF, giving the lexicographically smallest setting that satisfies every rule. If no setting satisfies every rule, print the single word No instead.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.