Facebook
From Crimson Wolf, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 108
  1. StreamWriter sw = new StreamWriter("output.txt");
  2.         public int n;
  3.         public string[] color;
  4.         public int[,] Adj;
  5.         List<int> visitationList = new List<int>();
  6.  
  7.         public Graph(int n, int[,] g)
  8.         {
  9.            this.Adj = g;
  10.            this.n = n;
  11.            color = new string[n+1];
  12.  
  13.             for (int i = 1; i <= n; i++)
  14.             {
  15.                 color[i] = "bialy";
  16.             }
  17.         }
  18.        
  19.  
  20.         public void DFS(int g)
  21.         {
  22.             DFSVISIT(g);
  23.             for (int i = 1; i<=n; i++)
  24.             {
  25.                 if (ShouldVisit(g, i))
  26.                 {
  27.                     DFSVISIT(i);
  28.                 }
  29.             }
  30.         }
  31.  
  32.  
  33.         public void DFSVISIT(int u)
  34.         {
  35.             visitationList.Add(u);
  36.             color[u] = "szary";
  37.             for(int i = 1; i<=n; i++)
  38.             {
  39.                 if (ShouldVisit(u, i))
  40.                 {
  41.                     DFSVISIT(i);
  42.                 }
  43.             }
  44.             color[u] = "czarny";
  45.         }
  46.  
  47.  
  48.          public void CheckIfSpojny()
  49.    
  50.          {
  51.             bool tt = false;
  52.             for(int i = 0; i<=n; i++)
  53.             {
  54.                 if (color[i] != "czarny" && i > 0)
  55.                 {
  56.                     tt = true;
  57.                     DFSVISIT(i);
  58.                 }
  59.             }
  60.             if (tt)
  61.             {
  62.                 sw.WriteLine("niespojny");
  63.             }
  64.             else
  65.             {
  66.                 sw.WriteLine("spojny");
  67.             }
  68.             foreach (int i in visitationList)
  69.             {
  70.                 sw.Write(i + " ");
  71.             }
  72.             sw.Close();
  73.         }
  74.  
  75.         private bool ShouldVisit(int sourceV, int targetV)
  76.         {
  77.             if (Adj[sourceV, targetV] != 1 || sourceV == targetV)
  78.             {
  79.                 return false;
  80.             }
  81.             return color[targetV] == "bialy";
  82.         }