Блог пользователя up79

Автор up79, история, 6 лет назад, По-английски
  • Проголосовать: нравится
  • -10
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

complete all connected components, then unite all capitalless components to largest component which has government home

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

There are K nodes corresponding to governments and none of them are initially connected by any path and you should never connect any of them by some path. So basically there are some connected components initially. You find each of those connected components by dfs. You will not connect any two nodes of different connected components because of the condition in the first line. For each connected component maximum number of edges that you can put is n*(n-1)/2 where n is the number of nodes in that connected component. Some edges were already in the connected component so you can just subtract their amount to get the number of "new" edges that you can put.

Now some connected components might not contain any government node. You can actually merge these connected components into one large connected component and this way you may put more edges into the graph. And also you can merge all of them with a connected component that already has a government node. This way you can form one large connected component and you want that to be largest (greedy thinking). So pick the largest connected component with a government node and merge all of the "connected components without government node" with this component to make one large component. And now you can calculate total number of edges you can insert into graph.

  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    7 3 3 these are goverment roads? 1 5 6 these are edges 1 2 1 3 6 7 can you please tell me how the answer is four?

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      https://imgur.com/oxPzp9K — you can see that graph here. Now you have 4 components and 1 of those components is only with node 4 and that does not contain any government nodes (1,5 or 6). So we can merge this with another connected component, either with (1,2,3) or with (6,7). We want the component to be as large as possible so we merge it with (1,2,3) and we have 3 components (1,2,3,4) , (6,7), (5). And now there are no more merging possible, else you will end up creating path between 2 government nodes. Now you just insert edges. A connected component of n nodes can have n*(n-1)/2 edges at max. So

      (1,2,3,4) — 6 edges

      (6,7) — 1 edge

      (5) — 0 edge

      But some of these edges were already given to you and the number of edges that were already given to you is given as input that is "m". So your total possible edge = 6 + 1 + 0 = 7. Given edges were m = 3. So you can add 4 "new" edges.

  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    Just did read the editorial. How can one come up with that solution given that problem statement? The statement says nothing about countries, and/or the way how cities belong to countries.