using System; using System.Collections.Generic; using System.Text; namespace Softuniada { class Mafia { private static int[,] graph; private static int[] BFSDist; private static int[] childCounter; private static int end; private static int[] paths; private static int nodes; static bool hehexd = false; static void Main(string[] args) { var group = 1; var line = Console.ReadLine(); while (line != "end") { nodes = int.Parse(line); graph = new int[nodes + 1, nodes + 1]; end = nodes; BFSDist = new int[nodes + 1]; childCounter = new int[nodes + 1]; paths = new int[graph.Length]; int testout; line = Console.ReadLine(); while (!int.TryParse(line, out testout) && line != "end") { var edgeParameters = line .Split(new char[] { '-', ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse) .ToArray(); var parent = edgeParameters[0]; var child = edgeParameters[1]; var capacity = edgeParameters[2]; graph[parent, child] = capacity; graph[child, parent] = capacity; line = Console.ReadLine(); } var maxFlow = Dinic(1, end); Console.WriteLine($"Group {group}: {maxFlow}"); group++; } } static int Dinic(int source, int target) { int result = 0; while (BFS(source, target)) { for (int i = 0; i < childCounter.Length; i++) { childCounter[i] = 0; } int delta; do { delta = DFS(source, int.MaxValue); result += delta; } while (delta != 0); } return result; } static int DFS(int source, int flow) { if (source == end) { return flow; } for (int i = childCounter[source]; i < graph.GetLength(0); i++, childCounter[source]++) { var child = i; if (graph[source, child] > 0) { if (BFSDist[child] == BFSDist[source] + 1) { int augPath = DFS(child, Math.Min(flow, graph[source, child])); if (augPath > 0) { if (source < nodes && source != 1) { paths[source] = child; } graph[source, child] -= augPath; graph[child, source] += augPath; return augPath; } } } } return 0; } static bool BFS(int start, int end) { for (int i = 0; i < BFSDist.Length; i++) { BFSDist[i] = -1; } BFSDist[start] = 0; Queue q = new Queue(); q.Enqueue(start); while (q.Count > 0) { var curr = q.Dequeue(); for (int i = 0; i < graph.GetLength(0); i++) { if (graph[curr, i] > 0 && BFSDist[i] == -1) { BFSDist[i] = BFSDist[curr] + 1; q.Enqueue(i); } } } return BFSDist[end] >= 0; } } }