Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.82 KB | None | 0 0
  1. 1. student 1 and 2 become friends
  2.  
  3. 1-2 3 4 5, we then sum the number of friends that each student has
  4. to get 1 + 1 + 0 + 0 + 0 = 2.
  5.  
  6. 2. Student 2 and 3 become friends:
  7.  
  8. 1-2-3 4 5, we then sum the number of friends that each student has to get
  9. 2 + 2 + 2 + 0 + 0 = 6.
  10.  
  11. 3. Student 4 and 5 become friends:
  12.  
  13. 1-2-3 4-5, we then sum the number of friends that each student has to get
  14. 2 + 2 + 2 + 1 + 1 = 8.
  15.  
  16. 4. Student 1 and 3 become friends: (we hold to add 1 and 3 until 4 and 5 are added to maximize the value.)
  17.  
  18. 1-2-3 4-5, we then sum the number of friends that each student has to get
  19. 2 + 2 + 2 + 1 + 1 = 8.
  20.  
  21. Total is 2 + 6 + 8 + 8 = 24.
  22.  
  23. using System;
  24. using System.Collections.Generic;
  25. using System.Diagnostics;
  26. using System.IO;
  27. using System.Linq;
  28.  
  29. class Solution
  30. {
  31.  
  32. public class Group
  33. {
  34. public int totalNumberNodes = 1;
  35. }
  36.  
  37. /*
  38. * GraphNode class
  39. */
  40. public class GraphNode
  41. {
  42. /* Julia added the variable: name, so she can track GraphNode and
  43. * work on the test case with 5 nodes: 0, 1, 2, 3, 4
  44. */
  45. public string name;
  46.  
  47. // variables are defined by original author, study code from
  48. // one of submissions with maximum score
  49. public Group group;
  50. public List<Edge> edges = new List<Edge>();
  51.  
  52. public GraphNode(string s)
  53. {
  54. this.name = s;
  55. }
  56.  
  57. /*
  58. * Work on the test case, illustrate the process using specific examples.
  59. *
  60. * Use name to track with edge, which node in the test case:
  61. * 1-2-3 4-5
  62. * 5 nodes in the graph, 4 connections, 1-3 is redundant.
  63. *
  64. * Go over each edge, and then remove friend node from nodes
  65. * HashSet, edge from edges HashSet, and share the group to
  66. * friend node, add one more to variable n, push friendNode
  67. * to the stack.
  68. */
  69. public void Connect(
  70. HashSet<GraphNode> nodes,
  71. HashSet<Edge> edges,
  72. Stack<GraphNode> neighbours)
  73. {
  74. foreach (var edge in this.edges)
  75. {
  76. var friendNode = (edge.id_1 != this) ? edge.id_1 : edge.id_2;
  77.  
  78. if (friendNode.group == null)
  79. {
  80. nodes.Remove(friendNode);
  81. edges.Remove(edge);
  82.  
  83. friendNode.group = group;
  84. group.totalNumberNodes++;
  85. neighbours.Push(friendNode);
  86. }
  87. }
  88. }
  89. }
  90.  
  91. public class Edge
  92. {
  93. public GraphNode id_1;
  94. public GraphNode id_2;
  95. }
  96.  
  97. static void Main(string[] args)
  98. {
  99. ProcessInput();
  100. //RunSampleTestcase();
  101. }
  102.  
  103. /*
  104. * Need to work on the sample test case
  105. * 1. student 1 and 2 become friends
  106. * 1-2 3 4 5, we then sum the number of friends that each student has
  107. * to get 1 + 1 + 0 + 0 + 0 = 2.
  108. * 2. Student 2 and 3 become friends:
  109. * 1-2-3 4 5, we then sum the number of friends that each student has to get
  110. * 2 + 2 + 2 + 0 + 0 = 6.
  111. * 3. Student 4 and 5 become friends:
  112. * 1-2-3 4-5, we then sum the number of friends that each student has to get
  113. * 2 + 2 + 2 + 1 + 1 = 8.
  114. * 4. Student 1 and 3 become friends: (we hold to add 1 and 3 until 4 and 5
  115. * are added to maximize the value.)
  116. * 1-2-3 4-5, we then sum the number of friends that each student has to get
  117. * 2 + 2 + 2 + 1 + 1 = 8.
  118. * Total is 2 + 6 + 8 + 8 = 24.
  119. */
  120. public static void RunSampleTestcase()
  121. {
  122. int queries = 1;
  123. string[][] datas = new string[1][];
  124. datas[0] = new string[2];
  125. datas[0][0] = "5";
  126. datas[0][1] = "4";
  127.  
  128. string[][] allFriendships = new string[1][];
  129. allFriendships[0] = new string[4];
  130.  
  131. allFriendships[0][0] = "1 2";
  132. allFriendships[0][1] = "2 3";
  133. allFriendships[0][2] = "1 3";
  134. allFriendships[0][3] = "4 5";
  135.  
  136. IList<long> result = MaximizeValues( queries, datas, allFriendships);
  137. Debug.Assert(result[0] == 24);
  138. }
  139.  
  140. /*
  141. * code review:
  142. * Everything is in one function, should break down a few of pieces
  143. * 1. Input
  144. * 2. Set up multiple graphes
  145. * 3. minimum edges to connect the graph
  146. * 4. extra edges to hold for maximum output, added by descending order.
  147. * 4. Output
  148. */
  149. public static void ProcessInput()
  150. {
  151. int queries = Convert.ToInt32(Console.ReadLine());
  152. string[][] graphData = new string[queries][];
  153. string[][] allFriendships = new string[queries][];
  154.  
  155. for (int query = 0; query < queries; query++)
  156. {
  157. string[] data = Console.ReadLine().Split(' ');
  158. int totalNodes = Convert.ToInt32(data[0]);
  159. int friendships = Convert.ToInt32(data[1]);
  160.  
  161. graphData[query] = new string[] { totalNodes.ToString(), friendships.ToString() };
  162.  
  163. allFriendships[query] = new string[friendships];
  164.  
  165. for (int i = 0; i < friendships; i++)
  166. {
  167. allFriendships[query][i] = Console.ReadLine();
  168. }
  169. } // end of process input
  170.  
  171. IList<long> result = MaximizeValues(queries, graphData, allFriendships);
  172. foreach(long value in result)
  173. {
  174. Console.WriteLine(value);
  175. }
  176. }
  177.  
  178. /*
  179. * Maximum value to add the friendship
  180. * 3 rules to follow - check editorial notes:
  181. * The graph is comprised of several components.
  182. * Each component has its own size.
  183. * 1. At first if a component has S nodes, you just need to add S - 1
  184. * edges to make the component connected (a subtree of the component),
  185. * add the rest of the edges at the end when all the components
  186. * are connected in themselves.
  187. * 2. At the end, when all of the components are connected, add
  188. * the extra edges.
  189. * 3. But what about the order of the components? Its better to
  190. * add larger components first
  191. * so that larger numbers are repeated more.
  192. * 4. What about a component in itself? Try to make a tree from that component.
  193. */
  194. public static IList<long> MaximizeValues(
  195. int queries,
  196. string[][] datas,
  197. string[][] allFriendships)
  198. {
  199. IList<long> output = new List<long>();
  200.  
  201. for (int query = 0; query < queries; query++)
  202. {
  203. string[] data = datas[query];
  204. int totalNodes = Convert.ToInt32(data[0]);
  205. int friendships = Convert.ToInt32(data[1]);
  206.  
  207. var map = new GraphNode[totalNodes];
  208. var nodes = new HashSet<GraphNode>();
  209.  
  210. for (int node = 0; node < totalNodes; node++)
  211. {
  212. map[node] = new GraphNode(node.ToString());
  213. nodes.Add(map[node]);
  214. }
  215.  
  216. var edges = new HashSet<Edge>();
  217.  
  218. var friendship = allFriendships[query];
  219.  
  220. for (int i = 0; i < friendships; i++)
  221. {
  222. string[] relationship = friendship[i].Split(' ');
  223.  
  224. var edge = new Edge();
  225.  
  226. edge.id_1 = map[Convert.ToInt32(relationship[0]) - 1];
  227. edge.id_2 = map[Convert.ToInt32(relationship[1]) - 1];
  228.  
  229. edges.Add(edge);
  230.  
  231. edge.id_1.edges.Add(edge);
  232. edge.id_2.edges.Add(edge);
  233. }
  234. // end of process input
  235.  
  236. var groups = new List<Group>();
  237.  
  238. // use stack - how to understand the stack's functionality here?
  239. // write down something here - go over a test case to understand
  240. // the code
  241. while (nodes.Count > 0)
  242. {
  243. var node = nodes.First();
  244. nodes.Remove(node);
  245.  
  246. groups.Add(node.group = new Group());
  247.  
  248. var neighbours = new Stack<GraphNode>();
  249. node.Connect(nodes, edges, neighbours);
  250.  
  251. while (neighbours.Count > 0)
  252. {
  253. GraphNode current = neighbours.Pop();
  254. current.Connect(nodes, edges, neighbours);
  255. }
  256. }
  257.  
  258. long result = 0;
  259. long sum = 0;
  260.  
  261. foreach (var edge in groups.OrderByDescending(g => g.totalNumberNodes))
  262. {
  263. for (int i = 1; i < edge.totalNumberNodes; i++)
  264. {
  265. result += (i + 1) * (long)i + sum;
  266. }
  267.  
  268. sum += (long)edge.totalNumberNodes * (edge.totalNumberNodes - 1);
  269. }
  270.  
  271. output.Add(result + edges.Count * sum);
  272. }
  273.  
  274. return output;
  275. }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement