To solve this problem, we need to ensure that all children can drink doogh without any overlapping paths (chords) between them. The key idea is to arrange the children in a circular order such that no two passing paths cross, thereby guaranteeing that each child gets to drink doogh without causing any issues.
Here is the step-by-step reasoning and approach:
-
Representation as a Circle:
- Imagine the children arranged in a circle, labeled from 1 to
n in a clockwise manner starting with Kareem.
-
Chords and Non-Crossing Condition:
- When a child passes the doogh to another child, it creates a chord between them on the circle.
- Two chords
[a, b] and [c, d] do not cross if:
- One chord entirely lies within the other without interleaving (e.g.,
[a, b] and [c, d] with a < c < d < b).
- They share a common endpoint without overlapping otherwise (e.g.,
[a, b] and [a, c]).
-
Greedy Construction of the Path:
- Start from any node (e.g., node 1).
- Use a greedy approach to select the next child such that the chord formed does not cross any existing chords.
- Sort friends of each child in ascending order to prioritize connections that are less likely to cause crossings.
-
Checking for Crossings:
- Whenever you add a new chord
[a, b] to your path, make sure it does not violate the non-crossing condition with any of the existing chords.
-
Edge Cases:
- If you cannot extend the path further without causing a crossing, the solution is invalid, and you should return
-1.
-
Final Path Construction:
- If you successfully construct a path visiting all children without any crossing chords, output the sequence.
- If not, output
-1.
Given the constraints (n ≤ 1000), we can implement this approach efficiently by maintaining a list of chords and checking for crossings as we build the path.
Below is the Java implementation based on this reasoning:
import java.io.*;
import java.util.*;
public class Main {
static class Chord implements Comparable<Chord> {
int a;
int b;
Chord(int a, int b){
this.a = a;
this.b = b;
}
public int compareTo(Chord other){
if(this.a != other.a){
return this.a - other.a;
}
return this.b - other.b;
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int n = Integer.parseInt(br.readLine());
int m = Integer.parseInt(br.readLine());
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for(int i=0;i<=n;i++) adj.add(new ArrayList<>());
for(int i=0;i<m;i++){
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
adj.get(u).add(v);
adj.get(v).add(u);
}
// Sort each adjacency list in ascending order
for(int i=1;i<=n;i++) {
Collections.sort(adj.get(i));
}
// Try starting from every node
// To speed up, you can try starting from node1
// Implement a greedy approach
// Initialize path
ArrayList<Integer> path = new ArrayList<>();
boolean[] visited = new boolean[n+1];
ArrayList<Chord> chords = new ArrayList<>();
// Choose starting node, try all possibilities
boolean found = false;
for(int start=1; start<=n && !found; start++){
path.clear();
Arrays.fill(visited, false);
chords.clear();
path.add(start);
visited[start] = true;
// Try to build the path
if(buildPath(start, adj, visited, path, chords, n)){
found = true;
break;
}
}
if(found){
StringBuilder sb = new StringBuilder();
for(int num: path){
sb.append(num).append("\n");
}
System.out.print(sb.toString());
}
else{
System.out.println("-1");
}
}
static boolean buildPath(int current, ArrayList<ArrayList<Integer>> adj, boolean[] visited, ArrayList<Integer> path, ArrayList<Chord> chords, int n){
if(path.size() == n) return true;
for(int friend : adj.get(current)){
if(!visited[friend]){
int a = Math.min(current, friend);
int b = Math.max(current, friend);
Chord newChord = new Chord(a, b);
// Check for crossing
boolean cross = false;
for(Chord existing : chords){
if(crossChords(existing, newChord)){
cross = true;
break;
}
}
if(!cross){
// Add to path
path.add(friend);
visited[friend] = true;
chords.add(newChord);
// Recursively build
if(buildPath(friend, adj, visited, path, chords, n)){
return true;
}
// Backtrack
path.remove(path.size()-1);
visited[friend] = false;
chords.remove(chords.size()-1);
}
}
}
return false;
}
static boolean crossChords(Chord c1, Chord c2){
// Two chords [a,b] and [c,d] cross if:
// a < c < b < d or c < a < d < b
return (c1.a < c2.a && c2.a < c1.b && c1.b < c2.b) ||
(c2.a < c1.a && c1.a < c2.b && c2.b < c1.b);
}
}