Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace tree
  9. {
  10.     class Program
  11.     {
  12.         static void lvls(int id, int count, ref int[] lvl, int[,] vec)
  13.         {
  14.            
  15.             for (int i = 0; i< count; i++)
  16.             {
  17.                 if ((vec[i, 0] == id) && (lvl[vec[i, 1]] == 10000))
  18.                 {
  19.                     lvl[vec[i, 1]] = lvl[id] - 1;
  20.                     lvls(vec[i, 1], count, ref lvl, vec);
  21.                 }
  22.                 if ((vec[i, 1] == id) && (lvl[vec[i, 0]] == 10000))
  23.                 {
  24.                     lvl[vec[i, 0]] = lvl[id] + 1;
  25.                     lvls(vec[i, 0], count, ref lvl, vec);
  26.                 }
  27.             }
  28.         }
  29.         static void por(int max, int count, int tree, ref int[] req, int[] lvl, int[,] vec)
  30.         {
  31.             for (int i = 0; i < tree; i++)
  32.             {
  33.                 if (lvl[i] == max)
  34.                 {
  35.                     for (int j = 0; j < count; j++)
  36.                     {
  37.                         if (vec[j, 1] == i) req[i] += req[vec[j, 0]] + 1;
  38.                     }
  39.                 }
  40.             }
  41.             if (max > 0) por(max - 1, count, tree, ref req, lvl, vec);
  42.         }
  43.         static void dec(int id, int count, ref int[] req, int[,] vec)
  44.         {
  45.             for (int i = 0; i < count; i++)
  46.             {
  47.                 if (vec[i, 0] == id)
  48.                 {
  49.                     req[vec[i, 1]]--;
  50.                     dec(vec[i, 1], count, ref req, vec);
  51.                 }
  52.             }
  53.         }
  54.         static void Main(string[] args)
  55.         {
  56.             string path = "input.txt";
  57.             StreamReader sr = new StreamReader(path);
  58.             string pathout = "output.txt";
  59.             StreamWriter sw = new StreamWriter(pathout);
  60.             int tree = int.Parse(sr.ReadLine());
  61.             int workers = int.Parse(sr.ReadLine());
  62.             int[] lvl = new int[tree];
  63.             for (int i = 0; i<tree; i++) lvl[i] = 10000;
  64.             lvl[0] = 0;
  65.             int[,] ans = new int[workers, tree];
  66.             for (int i = 0; i < workers; i++) for (int j = 0; j < tree; j++) ans[i, j] = -1;
  67.             int[] req = new int[tree];
  68.             int[,] inp = new int[tree, tree];
  69.             int count = 0;
  70.             for (int i = 0; i < tree; i++)
  71.             {
  72.                 string[] temp = sr.ReadLine().Split(' ');
  73.                 for (int j = 0; j < tree; j++)
  74.                 {
  75.                     inp[i, j] = int.Parse(temp[j]);
  76.                     if (inp[i, j] == 1) count++;
  77.                 }
  78.             }
  79.             int[,] vec = new int[count, 2];
  80.             int c = 0;
  81.             for (int i = 0; i < tree; i++)
  82.             {
  83.                 for (int j = 0; j < tree; j++)
  84.                 {
  85.                     if (inp[i, j] == 1)
  86.                     {
  87.                         vec[c, 0] = i;
  88.                         vec[c, 1] = j;
  89.                         c++;
  90.                     }
  91.                 }
  92.             }
  93.             //берем любую точку за точку нуля и определяем уровни остальных относительно первой.
  94.             lvls(0, count, ref lvl, vec);
  95.             //теперь мы знаем уровни вершин относительно вершины 0. Сделаем из этого абсолютные:
  96.             c = 0;
  97.             for (int i = 0; i < tree; i++) if (c > lvl[i]) c = lvl[i];
  98.             //ищем минимальный уровень
  99.             for (int i = 0; i < tree; i++) lvl[i] -= c;
  100.             //нормализуем уровни
  101.             //ищем максимум
  102.             for (int i = 0; i < tree; i++) if (lvl[i] > c) c = lvl[i];
  103.             por(c, count, tree, ref req, lvl, vec);
  104.             /////////
  105.             int[] newi = new int[tree];
  106.             int tt = tree;
  107.             while (c >= 0)
  108.             {
  109.                 for (int i = 0; i < tree; i++)
  110.                 {
  111.                     if (lvl[i] == c) newi[i] = tt--;
  112.                 }
  113.                 c--;
  114.             }
  115.             ////////
  116.             c = tree;
  117.             int g = workers;
  118.             int[] pt = new int[tree];
  119.             int min = 0;
  120.             int day = 0;
  121.             while (c != 0)
  122.             {
  123.                 g = workers;
  124.                 for (int i = 0; i < tree; i++) pt[i] = 10000;
  125.                 for (int i = 0; i < tree; i++)
  126.                 {
  127.                     if (req[i] == 0)
  128.                     {
  129.                         pt[i] = 0;
  130.                         for (int j = 0; j < count; j++) if (vec[j, 0] == i) pt[i] = req[vec[j, 1]];
  131.                     }
  132.                 }
  133.                 while (g > 0)
  134.                 {
  135.                     min = 0;
  136.                     for (int i = 0; i < tree; i++) if (pt[i] < pt[min]) min = i;
  137.                     if (pt[min] == 10000)
  138.                     {
  139.                         g = -1;
  140.                     } else
  141.                     {
  142.                         ans[workers - g, day] = min;
  143.                         g--;
  144.                         c--;
  145.                         pt[min] = 10000;
  146.                         dec(min, count, ref req, vec);
  147.                         req[min] = 10000;
  148.                     }
  149.                 }
  150.                 day++;
  151.             }
  152.  
  153.             //переведем ответы в дни и выведем их
  154.             for (int i = 0; i < workers; i++)
  155.             {
  156.                 string s = "";
  157.                 for (int j = 0; j < day; j++)
  158.                 {
  159.                     if (ans[i, j] == -1) { s += "-1 "; }
  160.                     else s += newi[ans[i, j]].ToString() + "  ";
  161.                 }
  162.                 sw.WriteLine(s);
  163.             }
  164.             sw.Close();
  165.             sr.Close();
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement