StreamWriter sw = new StreamWriter("output.txt"); public int n; public string[] color; public int[,] Adj; List visitationList = new List(); public Graph(int n, int[,] g) { this.Adj = g; this.n = n; color = new string[n+1]; for (int i = 1; i <= n; i++) { color[i] = "bialy"; } } public void DFS(int g) { DFSVISIT(g); for (int i = 1; i<=n; i++) { if (ShouldVisit(g, i)) { DFSVISIT(i); } } } public void DFSVISIT(int u) { visitationList.Add(u); color[u] = "szary"; for(int i = 1; i<=n; i++) { if (ShouldVisit(u, i)) { DFSVISIT(i); } } color[u] = "czarny"; } public void CheckIfSpojny() { bool tt = false; for(int i = 0; i<=n; i++) { if (color[i] != "czarny" && i > 0) { tt = true; DFSVISIT(i); } } if (tt) { sw.WriteLine("niespojny"); } else { sw.WriteLine("spojny"); } foreach (int i in visitationList) { sw.Write(i + " "); } sw.Close(); } private bool ShouldVisit(int sourceV, int targetV) { if (Adj[sourceV, targetV] != 1 || sourceV == targetV) { return false; } return color[targetV] == "bialy"; }