Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace tree
- {
- class Program
- {
- static void lvls(int id, int count, ref int[] lvl, int[,] vec)
- {
- for (int i = 0; i< count; i++)
- {
- if ((vec[i, 0] == id) && (lvl[vec[i, 1]] == 10000))
- {
- lvl[vec[i, 1]] = lvl[id] - 1;
- lvls(vec[i, 1], count, ref lvl, vec);
- }
- if ((vec[i, 1] == id) && (lvl[vec[i, 0]] == 10000))
- {
- lvl[vec[i, 0]] = lvl[id] + 1;
- lvls(vec[i, 0], count, ref lvl, vec);
- }
- }
- }
- static void por(int max, int count, int tree, ref int[] req, int[] lvl, int[,] vec)
- {
- for (int i = 0; i < tree; i++)
- {
- if (lvl[i] == max)
- {
- for (int j = 0; j < count; j++)
- {
- if (vec[j, 1] == i) req[i] += req[vec[j, 0]] + 1;
- }
- }
- }
- if (max > 0) por(max - 1, count, tree, ref req, lvl, vec);
- }
- static void dec(int id, int count, ref int[] req, int[,] vec)
- {
- for (int i = 0; i < count; i++)
- {
- if (vec[i, 0] == id)
- {
- req[vec[i, 1]]--;
- dec(vec[i, 1], count, ref req, vec);
- }
- }
- }
- static void Main(string[] args)
- {
- string path = "input.txt";
- StreamReader sr = new StreamReader(path);
- string pathout = "output.txt";
- StreamWriter sw = new StreamWriter(pathout);
- int tree = int.Parse(sr.ReadLine());
- int workers = int.Parse(sr.ReadLine());
- int[] lvl = new int[tree];
- for (int i = 0; i<tree; i++) lvl[i] = 10000;
- lvl[0] = 0;
- int[,] ans = new int[workers, tree];
- for (int i = 0; i < workers; i++) for (int j = 0; j < tree; j++) ans[i, j] = -1;
- int[] req = new int[tree];
- int[,] inp = new int[tree, tree];
- int count = 0;
- for (int i = 0; i < tree; i++)
- {
- string[] temp = sr.ReadLine().Split(' ');
- for (int j = 0; j < tree; j++)
- {
- inp[i, j] = int.Parse(temp[j]);
- if (inp[i, j] == 1) count++;
- }
- }
- int[,] vec = new int[count, 2];
- int c = 0;
- for (int i = 0; i < tree; i++)
- {
- for (int j = 0; j < tree; j++)
- {
- if (inp[i, j] == 1)
- {
- vec[c, 0] = i;
- vec[c, 1] = j;
- c++;
- }
- }
- }
- //берем любую точку за точку нуля и определяем уровни остальных относительно первой.
- lvls(0, count, ref lvl, vec);
- //теперь мы знаем уровни вершин относительно вершины 0. Сделаем из этого абсолютные:
- c = 0;
- for (int i = 0; i < tree; i++) if (c > lvl[i]) c = lvl[i];
- //ищем минимальный уровень
- for (int i = 0; i < tree; i++) lvl[i] -= c;
- //нормализуем уровни
- //ищем максимум
- for (int i = 0; i < tree; i++) if (lvl[i] > c) c = lvl[i];
- por(c, count, tree, ref req, lvl, vec);
- /////////
- int[] newi = new int[tree];
- int tt = tree;
- while (c >= 0)
- {
- for (int i = 0; i < tree; i++)
- {
- if (lvl[i] == c) newi[i] = tt--;
- }
- c--;
- }
- ////////
- c = tree;
- int g = workers;
- int[] pt = new int[tree];
- int min = 0;
- int day = 0;
- while (c != 0)
- {
- g = workers;
- for (int i = 0; i < tree; i++) pt[i] = 10000;
- for (int i = 0; i < tree; i++)
- {
- if (req[i] == 0)
- {
- pt[i] = 0;
- for (int j = 0; j < count; j++) if (vec[j, 0] == i) pt[i] = req[vec[j, 1]];
- }
- }
- while (g > 0)
- {
- min = 0;
- for (int i = 0; i < tree; i++) if (pt[i] < pt[min]) min = i;
- if (pt[min] == 10000)
- {
- g = -1;
- } else
- {
- ans[workers - g, day] = min;
- g--;
- c--;
- pt[min] = 10000;
- dec(min, count, ref req, vec);
- req[min] = 10000;
- }
- }
- day++;
- }
- //переведем ответы в дни и выведем их
- for (int i = 0; i < workers; i++)
- {
- string s = "";
- for (int j = 0; j < day; j++)
- {
- if (ans[i, j] == -1) { s += "-1 "; }
- else s += newi[ans[i, j]].ToString() + " ";
- }
- sw.WriteLine(s);
- }
- sw.Close();
- sr.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement