static Star getMajorityGalaxyStarCandidate(List <Star> candidateGalaxy) { long loopCount = candidateGalaxy.Count - 1; List <Star> toReturn = new List <Star>(); Star y = null; if (candidateGalaxy.Count == 0) { return(null); } else if (candidateGalaxy.Count == 1) { return(candidateGalaxy[0]); } //else if(candidateGalaxy.Count == 2) //{ // //candidateGalaxy.Pop(); // return candidateGalaxy.Pop(); //} else { if (candidateGalaxy.Count % 2 != 0) { loopCount--; //toReturn.Add(candidateGalaxy[0]); y = candidateGalaxy[candidateGalaxy.Count - 1]; } for (int i = 0; i <= loopCount; i += 2) { Star a = candidateGalaxy[i]; Star b = candidateGalaxy[i + 1]; if (a.inGalaxy(b)) { //toReturn.Push(a);//TODO: Fix this, as pushing both on can form an infinite loop, but only pushing one can cause a false negative toReturn.Add(b); } } Star x = getMajorityGalaxyStarCandidate(toReturn); if (x == null) { if (candidateGalaxy.Count % 2 != 0) { return(hasMajorityGalaxy(y, candidateGalaxy)); } else { //Console.WriteLine("NO"); return(null); } } else { return(hasMajorityGalaxy(x, candidateGalaxy)); } } }
static void Main(string[] args) { string variables = Console.ReadLine(); string[] tokens = variables.Split(' '); Star.distance = long.Parse(tokens[0]); long numStars = long.Parse(tokens[1]); //Stack<Star> candidateGalaxy = new Stack<Star>(); List <Star> PU = new List <Star>(); //if(numStars % 2 != 0) //{ // loopCount--; //} for (int i = 0; i < numStars; i++) { string star = Console.ReadLine(); string[] starCoordinates = star.Split(' '); PU.Add(new Star(long.Parse(starCoordinates[0]), long.Parse(starCoordinates[1]))); } //Star CandidateStar = getMajorityGalaxyStarCandidate(PU); //Console.WriteLine("Candidate Star - x=" + CandidateStar.x + " y=" + CandidateStar.y); //hasMajorityGalaxy(CandidateStar, PU); Star result = getMajorityGalaxyStarCandidate(PU); if (result == null) { Console.WriteLine("NO"); } else { HashSet <Star> count = new HashSet <Star>(); foreach (Star s in PU) { if (result.inGalaxy(s)) { count.Add(s); } } if (count.Count > PU.Count / 2) { Console.WriteLine(count.Count); } else { Console.WriteLine("NO"); } } Console.Read(); }
static Star hasMajorityGalaxy(Star Candidate, List <Star> PU) { List <Star> toReturn = new List <Star>(); //toReturn.Push(Candidate); foreach (Star s in PU) { if (Candidate.inGalaxy(s)) { toReturn.Add(s); } } if (toReturn.Count > PU.Count / 2) { //Console.WriteLine(toReturn.Count); return(Candidate); } else { //Console.WriteLine("NO"); return(null); } }