Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.09 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())+1;
  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.             int tem;
  71.             for (int i = 0; i < tree - 1; i++)
  72.             {
  73.                 tem = 0;
  74.                 string[] temp = sr.ReadLine().Split(' ');
  75.                 for (int j = 0; j < tree - 1; j++)
  76.                 {
  77.                     inp[i, j] = int.Parse(temp[j]);
  78.                     tem += inp[i, j];
  79.                     if (inp[i, j] == 1) count++;
  80.                 }
  81.                 if (tem==0){
  82.                     inp[i, tree - 1] = 1;
  83.                     count++;
  84.                 }
  85.             }
  86.             int[,] vec = new int[count, 2];
  87.             int c = 0;
  88.             for (int i = 0; i < tree; i++)
  89.             {
  90.                 for (int j = 0; j < tree; j++)
  91.                 {
  92.                     if (inp[i, j] == 1)
  93.                     {
  94.                         vec[c, 0] = i;
  95.                         vec[c, 1] = j;
  96.                         c++;
  97.                     }
  98.                 }
  99.             }
  100.  
  101.             //берем любую точку за точку нуля и определяем уровни остальных относительно первой.
  102.             lvls(0, count, ref lvl, vec);
  103.             //теперь мы знаем уровни вершин относительно вершины 0. Сделаем из этого абсолютные:
  104.             c = 0;
  105.             for (int i = 0; i < tree; i++) if (c > lvl[i]) c = lvl[i];
  106.             //ищем минимальный уровень
  107.             for (int i = 0; i < tree; i++) lvl[i] -= c;
  108.             //нормализуем уровни
  109.             //ищем максимум
  110.             for (int i = 0; i < tree; i++) if (lvl[i] > c) c = lvl[i];
  111.             por(c, count, tree, ref req, lvl, vec);
  112.             /////////
  113.             int[] newi = new int[tree];
  114.             int tt = tree;
  115.             while (c >= 0)
  116.             {
  117.                 for (int i = 0; i < tree; i++)
  118.                 {
  119.                     if (lvl[i] == c) newi[i] = tt--;
  120.                 }
  121.                 c--;
  122.             }
  123.             ////////
  124.             c = tree;
  125.             int g = workers;
  126.             int[] pt = new int[tree];
  127.             int min = 0;
  128.             int day = 0;
  129.             while (c != 0)
  130.             {
  131.                 g = workers;
  132.                 for (int i = 0; i < tree; i++) pt[i] = 10000;
  133.                 for (int i = 0; i < tree; i++)
  134.                 {
  135.                     if (req[i] == 0)
  136.                     {
  137.                         pt[i] = 0;
  138.                         for (int j = 0; j < count; j++) if (vec[j, 0] == i) pt[i] = req[vec[j, 1]];
  139.                     }
  140.                 }
  141.                 while (g > 0)
  142.                 {
  143.                     min = 0;
  144.                     for (int i = 0; i < tree; i++) if (pt[i] < pt[min]) min = i;
  145.                     if (pt[min] == 10000)
  146.                     {
  147.                         g = -1;
  148.                     } else
  149.                     {
  150.                         ans[workers - g, day] = min;
  151.                         g--;
  152.                         c--;
  153.                         pt[min] = 10000;
  154.                         dec(min, count, ref req, vec);
  155.                         req[min] = 10000;
  156.                     }
  157.                 }
  158.                 day++;
  159.             }
  160.  
  161.             //переведем ответы в дни и выведем их
  162.             for (int i = 0; i < workers; i++)
  163.             {
  164.                 string s = "";
  165.                 for (int j = 0; j < day; j++)
  166.                 {
  167.                     if (ans[i, j] == -1) { s += "-1 "; }
  168.                     else s += newi[ans[i, j]].ToString() + "  ";
  169.                 }
  170.                 sw.WriteLine(s);
  171.             }
  172.             sw.Close();
  173.             sr.Close();
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement