Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. function processData(input) {
  2. var temp = input.split('\n')
  3. var [ N, I ] = temp.shift().split(' ').map( Number )
  4.  
  5. // generate graph
  6. var graph = [] // store in an array of Set [ 0 : [ 0, 1 ], 1 : [ 3, 2 ] ] (to prevent duplicate)
  7. var relationalMap = {} // use to remember the map { 0 : 0, 1 : 0, 2 : 1, 3 : 1 }
  8.  
  9. for(let i = 0; i < I;i++)
  10. {
  11. var [ ast1, ast2 ] = temp.shift().split(' ').map( Number )
  12.  
  13. // if both of the ast1 and ast2 found in the different map => group two map
  14. if ( relationalMap.hasOwnProperty( ast1 ) && relationalMap.hasOwnProperty( ast2 ) )
  15. {
  16. // console.log('[both found]', ast1, ast2, relationalMap)
  17. var graphIndex = relationalMap[ ast1 ]
  18. var graphIndex2 = relationalMap[ ast2 ]
  19.  
  20. // continue if there are already in the graph
  21. if ( graphIndex === graphIndex2 ) continue
  22. // update the set from 2 to 1, and clear set 2
  23. for( let i of graph[ graphIndex2 ] )
  24. {
  25. graph[ graphIndex ].add( i )
  26. relationalMap[ i ] = graphIndex
  27. }
  28. graph[ graphIndex2 ].clear()
  29. }
  30. // if one of ast1 and ast2 found in the map
  31. else if ( relationalMap.hasOwnProperty( ast1 ) || relationalMap.hasOwnProperty( ast2 ) )
  32. {
  33. // console.log('[one found]', ast1, ast2, relationalMap)
  34. let graphIndex = relationalMap[ ast1 ] || relationalMap[ ast2 ]
  35. graph[ graphIndex ].add( ast1 ).add( ast2 )
  36. relationalMap[ ast1 ] = graphIndex
  37. relationalMap[ ast2 ] = graphIndex
  38. }
  39. // if ast1 ast2 not found in the map
  40. else if ( !relationalMap.hasOwnProperty( ast1 ) && !relationalMap.hasOwnProperty( ast2 ) )
  41. {
  42. // console.log('[not found]', ast1, ast2, relationalMap)
  43. let graphSet = new Set()
  44. graphSet.add( ast1 ).add( ast2 )
  45. let graphIndex = ( graph.push( graphSet ) - 1 ) + ''
  46. relationalMap[ ast1 ] = graphIndex
  47. relationalMap[ ast2 ] = graphIndex
  48. }
  49. }
  50. // console.log('[graph]', graph);
  51. // console.log('[relationalMap]', relationalMap)
  52.  
  53. // denotes the number of permissible ways to choose a pair of astronauts.
  54. var ways = 0;
  55. for( let i = 0;i < graph.length - 1; i++)
  56. {
  57. let waysi = graph[i].size
  58. for( let j = i + 1; j < graph.length; j++ )
  59. {
  60. let waysj = graph[j].size
  61. ways += waysi * waysj
  62. }
  63. }
  64.  
  65. // and those who is alone in their country
  66. let alone = N - Object.keys( relationalMap ).length
  67. ways += Object.keys(relationalMap).length * ( N - Object.keys(relationalMap).length )
  68. ways += c(alone, 2)
  69.  
  70. console.log(ways)
  71. }
  72.  
  73. function c(n, x)
  74. {
  75. let numerator = n
  76. for(let i = n-1;i>(n-x);i--)
  77. {
  78. numerator *= i
  79. }
  80.  
  81. let denominator = x
  82. for(let i = x - 1 ;i>=2;i--)
  83. {
  84. denominator *= i
  85. }
  86.  
  87. return numerator/denominator
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement