Nader Shah
Time limit2sMemory limit512 MB
From roads and marked Afshari edges, reconstruct the root and capture order consistent with the growth rule, lexicographically minimum, or output Wrong Map!.
- Level
Hard8 of 10
- Topics
- Graph, Greedy, Heap, Implementation
- Solved
- No attempts yet
Problem
Nader Shah Afshar was one of the most powerful rulers in Iran. He won many battles, such as the battle of Herat, Mihmandust, and Kirkuk. During his rule, Mashhad was Iran's capital. Each year, he planned an attack on a new country and annexed it to Iran's territory. Because Nader Shah's past victories were known to everyone, the country under attack would surrender without any combat. Therefore, after some years, Iran's territory was expanded by n new countries.
There was a connected road network between the countries. All roads were two-way. Nader Shah only attacked those countries which had at least one road to Iran's territory at that moment. After capturing each country, Nader Shah chose exactly one road between the captured country and Iran's territory at that moment, and placed an Iranian flag on it at every one kilometer. To keep costs as low as possible, he chose the shortest road since it needed fewer Iranian flags (if several roads were the shortest, he arbitrarily chose one of them). These roads were called Afshari roads. Obviously, choosing roads this way gives exactly k - 1 Afshari roads when Iran's territory consists of k countries.
Several years later, we have received a map of Iran's territory at the end of Nader Shah's life. The map has the entire road network on it with their lengths in kilometers. Afshari roads are marked on the map. There are n countries on the map, m roads connecting them, and n - 1 of those roads are marked as Afshari. Each country is marked by a distinct number 1 ≤ x ≤ n. We do not distinguish between the numbers and the countries.
Given the map described above, we want to know the possible country corresponding to Iran and the order by which Nader Shah captured countries. One possible answer can be shown as a1, a2, ..., an where a1 corresponds to Iran, a2 is the first country that Nader Shah captured, a3 is the second country he captured, and so on. We want to know the lexicographically minimum possible answer. Array x1, x2, ..., xn is lexicographically smaller than y1, y2, ..., yn if and only if there exists a number i (1 ≤ i ≤ n) such that (x1 = y1) ∧ (x2 = y2) ∧ ... ∧ (xi-1 = yi-1) and xi < yi.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 1,000, n - 1 ≤ m ≤ 5,000), where n and m are the number of countries and the number of roads on the map (the road network), respectively. The next m lines describe the road network; each line contains four integers u, v, w, and r (1 ≤ u, v ≤ n, 1 ≤ w ≤ 10^6, and r ∈ {0, 1}), which indicate there is a road between u and v with length w kilometers, and r denotes whether the road is Afshari. The road is Afshari if and only if r = 1. There is at most one road between two countries. It is guaranteed that there are exactly n - 1 Afshari roads in the road network, and all n countries are connected through these n - 1 roads.
Output
If there is no answer to the problem, print Wrong Map!. Otherwise, print the lexicographically minimum answer to the problem.