|
|
вернуться в форумTLE#9 C# how make it faster? =( Can someone help how to make C# code faster? Here is my code: using System; using System.Collections.Generic; class Program { static int countCut(List<int>[] nods, List<int> uncheckedNodes) { int next = 0; int count = 0; do { count++; watchCut(nods, uncheckedNodes, next); if (uncheckedNodes.Count > 0) next = uncheckedNodes[0]; else next = -1; } while (next != -1); return count; } static void watchCut(List<int>[] nods, List<int> uncheckedNodes, int node) { uncheckedNodes.Remove(node); for (int i = 0; i < nods[node].Count; i++) { if (uncheckedNodes.Contains(nods[node][i])) watchCut(nods, uncheckedNodes, nods[node][i]); } } static void Main(string[] args) { string[] inp_NM = Console.ReadLine().Split(' '); int N, M; N = Convert.ToInt32(inp_NM[0]); M = Convert.ToInt32(inp_NM[1]); int[,] threads = new int[M, 2]; List<int>[] nods = new List<int>[N]; for (int i = 0; i < N; i++) nods[i] = new List<int>(); for (int i = 0; i < M; i++) { string[] inp_thread = Console.ReadLine().Split(' '); threads[i, 0] = Convert.ToInt32(inp_thread[0]) - 1; threads[i, 1] = Convert.ToInt32(inp_thread[1]) - 1; nods[threads[i, 0]].Add(threads[i, 1]); nods[threads[i, 1]].Add(threads[i, 0]); } int Q; Q = Convert.ToInt32(Console.ReadLine()); int[] cuts = new int[Q]; string[] inp_tear = Console.ReadLine().Split(' '); for (int i = 0; i < Q; i++) { cuts[i] = Convert.ToInt32(inp_tear[i]) - 1; } for (int i = 0; i < Q; i++) { nods[threads[cuts[i], 0]].Remove(threads[cuts[i], 1]); nods[threads[cuts[i], 1]].Remove(threads[cuts[i], 0]); List<int> uncheckedNodes = new List<int>(); for (int l = 0; l < N; l++) uncheckedNodes.Add(l); Console.Write(countCut(nods, uncheckedNodes)+" "); } } } Edited by author 24.11.2020 08:00 Re: TLE#9 C# how make it faster? =( In this problem you should use disjoint-set(система непересекающихся множеств). |
|
|