Surveyors' Altitude Log

medium union-find graphs simulation 1500 ms · 256 MB

Surveyors' Altitude Log

A survey team is mapping n peaks in a mountain range, numbered 1 to n. No one has measured any peak's absolute altitude, but surveyors keep radioing in claims about how two peaks compare, and base camp keeps asking about pairs of peaks whose difference it wants to know.

The log contains q lines, each one of two kinds:

  • CLAIM a b d — a surveyor reports that peak a stands exactly d meters above peak b. In other words, height(a) - height(b) = d. The value d may be negative (meaning a is below b) or zero (meaning they are at the same altitude). Peaks a and b may be equal.
  • QUERY a b — base camp asks for height(a) - height(b).

Base camp accepts a claim only if it is consistent with every claim accepted so far (together with everything those claims imply by combining with each other). Note that a claim is still accepted, with no further comment, if it says nothing new (it already follows from what is known). If instead a new claim contradicts the accepted claims — implying a different value for some pair's height difference than what is already known — base camp radios back CONFLICT and discards that claim entirely: it has no effect on anything that follows.

For each QUERY a b, base camp reports height(a) - height(b) if that value is pinned down by the claims accepted so far, and UNKNOWN otherwise.

Note that CLAIM lines that are accepted produce no output at all — only rejected (CONFLICT) claims and QUERY lines produce a line of output, in the order they appear in the log.

Input

The first line contains two integers n and q (1 ≤ n ≤ 2·10^5, 1 ≤ q ≤ 2·10^5).

Each of the next q lines contains one log entry, in one of the two forms above:

  • CLAIM a b d with 1 ≤ a, b ≤ n and -10^9 ≤ d ≤ 10^9.
  • QUERY a b with 1 ≤ a, b ≤ n.

Output

For every CLAIM that is rejected, print a line containing CONFLICT.

For every QUERY, print a line containing either the integer value of height(a) - height(b) (it is guaranteed to fit in a 64-bit signed integer), or UNKNOWN if it cannot be determined from the claims accepted so far.

Print nothing for a CLAIM that is accepted. The total number of output lines therefore equals the number of QUERY entries plus the number of rejected CLAIM entries, in the order the corresponding entries appear in the log.

Notes

In the first example, the claim CLAIM 1 2 5 says peak 1 is 5 meters above peak 2, and CLAIM 2 3 3 says peak 2 is 3 meters above peak 3, so peak 1 must be 8 meters above peak 3 — the query confirms this. The later claim CLAIM 1 3 10 contradicts that (it would require a difference of 10, not 8), so it is reported as CONFLICT and ignored; the final query shows the original value of 8 still holds.

Example 1

Input

3 5
CLAIM 1 2 5
CLAIM 2 3 3
QUERY 1 3
CLAIM 1 3 10
QUERY 1 3

Output

8
CONFLICT
8

Example 2

Input

4 6
QUERY 1 2
CLAIM 1 2 4
QUERY 1 2
CLAIM 3 3 0
CLAIM 3 3 5
QUERY 3 4

Output

UNKNOWN
4
CONFLICT
UNKNOWN

Hints · reveal one at a time if you're stuck

Hint 1

Treat peaks as nodes and each claim as an edge that fixes the height difference between two peaks. When does a query have a determined answer, and when does a claim contradict what is already known?

Hint 2

Use union-find in which each node also stores its height offset relative to its parent, so that the difference between any two members of a component can be read off by walking to their common root.

Hint 3

A claim between two different components fixes the offset between their roots, so merge them. A claim within one component is either already implied, or it is a conflict to discard. Remember to fold the offsets correctly while compressing paths.

report an issue Prefer your own editor? sooks prep 77 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.