#P11978. Railway Network Encoding and Decoding
Railway Network Encoding and Decoding
Railway Network Encoding and Decoding
Two hostile nations, A and B, are at odds. Nation A wants to invade nation B and needs information on B's railway network. However, the only information available is a spanning tree graph representing B's railway network. The tree has the following properties:
- There are \(N\) stations numbered \(1\) to \(N\).
- Any two distinct stations are either directly connected by a railway or connected indirectly by one or more intermediate stations.
- Between any two stations, there is a unique path.
- No station is directly connected to itself.
Since repeated spy attempts yield nothing useful, nation A hires a traitor at B's railway company to obtain the network diagram. To avoid detection, the traitor modifies the diagram as follows:
- Add exactly \(K\) fake railways. Each fake railway is added between two stations that were not originally adjacent (i.e. not directly connected in the tree). All added edges must be distinct and must not duplicate any true railway.
- Mark one station with a special marker.
- Erase all station labels.
After the modifications, the diagram (an undirected graph with \(N+K-1\) edges) is sent to nation A. Note that the receiver only knows which station has the special marker and the total number \(K\) of fake railways. Using these, the receiver must recover the original railway network (i.e. the unique spanning tree with \(N-1\) edges).
Your task is to implement two functions:
// Encodes the railway network by adding fake railways and selecting a special station.
vector< pair<int, int> > encode_map(int N, int K, int &X, vector< pair<int, int> > E);
// Decodes the network diagram (with re‐labeled stations) to recover the original railway network.
vector< pair<int, int> > decode_map(int N, int K, int X, vector< pair<int, int> > E);
Implementation details:
- encode_map:
- The input tree E contains exactly \(N-1\) pairs representing the true railways between stations numbered \(1\) to \(N\).
- You must choose a station \(X\) (by setting the reference parameter) that will be specially marked. The scheme below ensures reversibility:
- Select a leaf station (a station with degree 1 in the tree). (A tree always has at least two leaves, so choose the leaf with the smallest number.)
- Let its unique neighbor be \(y\). Then, among all stations \(v\) (with \(v \neq X\) and \(v \neq y\)), choose some pairs \((X,v)\) which are not in the original tree. Pick exactly \(K\) such pairs (in increasing order of \(v\)). These will be the fake railways.
- decode_map:
- The input diagram E is an unordered list of \(N+K-1\) edges (true railways and fake railways) defined on stations labeled with a new numbering. However, the special marked station is identified by \(X\) (which corresponds to the leaf chosen during encoding) and \(K\) is known.
- Since in the encoding scheme all fake railways are incident to \(X\) (except the one true edge connected to \(X\)), you can recover the original tree as follows:
- Let \(L\) be the set of all edges incident to \(X\) in the diagram. For each edge \((X,v)\) in \(L\), form a candidate spanning tree by keeping this edge and removing all other edges incident to \(X\). Since the true tree has \(N-1\) edges, the candidate with the correct \((X,v)\) will form a spanning tree (i.e. it is connected and acyclic). Return that candidate.
No input/output operations should be performed in the submitted source code. The testing system will call your functions directly. In one run, encode_map is called on several test scenarios and the results are saved. In a later run, decode_map is called on a random selection of scenarios with the encoded diagram as input.
inputFormat
encode_map parameters:
- Integer
N
: number of stations. - Integer
K
: number of fake railways to add. - Reference integer
X
: to be set to the special marked station (between 1 and N). - Vector of pairs
E
: contains \(N-1\) pairs \((a,b)\) representing the original railway network.
decode_map parameters:
- Integer
N
: number of stations. - Integer
K
: number of fake railways added. - Integer
X
: the specially marked station (note: the numbering is re‐labeled, but the special marker is preserved). - Vector of pairs
E
: contains \(N+K-1\) pairs representing the modified diagram (true and fake railways, unordered).
outputFormat
encode_map returns a vector of \(K\) pairs representing the added fake railways (each pair \((a,b)\) must not duplicate any true railway in E
).
decode_map returns a vector of \(N-1\) pairs representing the recovered original railway network.
sample
N = 4, K = 1, E = [(1,2), (2,3), (2,4)]
encode_map sets X = 1 (leaf) and returns fake edge [(1,3)]. The full encoded graph is: [(1,2),(2,3),(2,4),(1,3)]. decode_map recovers the original tree [(1,2),(2,3),(2,4)].