Towards the Capital

In Sheldor's empire, the cities form a tree with cities as vertices and roads as edges. He wishes to choose a capital among his cities and to do that, his ministers come up with the following definition:

  • The eccentricity of a city v v is the maximum distance from v v to any city.
  • A city is central if it's eccentricity is the least.

Which is the only central city in this empire?

Input File: Link

Input Constraints and Details:

  • The empire contains of 100 cities.
  • Each line in the Input File contains two integers u u and v v . This refers to the fact that there is a road connecting u u and v v .


The answer is 1.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Nicola Mignoni
May 3, 2018

Step 1. Let's call map N 98 × 2 \text{map} \in \mathbb{N}^{98 \times 2} the input file. Let's call M N 100 × 100 M \in \mathbb{N}^{100 \times 100} a matrix in which m i j M m_{ij} \in M is the distance from city i i to city j j . It's clear that m i j = m j i m_{ij}=m_{ji} . So, each row of map \text{map} represents cities that are directly connected. For example,

map ( 1 , : ) = [ 1 2 ] m 12 = m 21 = 1 \displaystyle \text{map}(1,:)=[1\hspace{5pt} 2] \hspace{5pt} \Longrightarrow \hspace{5pt} m_{1 2}=m_{2 1}=1

In other terms, I'm giving value 1 1 to m i j m_{ij} if [ i j ] [i \hspace{5pt} j] is a row of map \text{map} . We can continue this process for all 98 98 rows of map \text{map} . For the sake of coherence, m i j = 0 m_{ij}=0 if i = j i=j .

Now, let's look at M ( 1 , : ) M(1,:) . We see that m 12 = m 14 = m 15 = m 19 = m 1 , 11 = m 1 , 12 = m 1 , 42 = m 1 , 48 = 1 ) m_{12}=m_{14}=m_{15}=m_{19}=m_{1,11 }=m_{1,12}=m_{1,42}=m_{1,48}=1) . This means that 1 1 is directly connected with 2 , 4 , 5 , 9... 2, 4, 5, 9 ... and so on. Let's look at M ( 2 , : ) M(2,:) . We'll find that m 21 = m 23 = m 2 , 19 = m 2 , 39 = m 2 , 77 = 1 ) m_{21}=m_{23}=m_{2,19}=m_{2,39}=m_{2,77 }=1) . This means that 2 2 is directly connected with 1 1 (we already knew that) 3 , 19 , 39 3,19,39 and 77 77 . But this means that city 1 1 has distance 2 2 from cities 3 , 19 , 39 3,19,39 , namely

m 13 = m 1 , 19 = m 1 , 39 = m 1 , 77 = 2 m_{13}=m_{1,19}=m_{1,39}=m_{1,77}=2 .

So, this is the algorithm main structure after Step 1 :

Step 2.

  • Let be m i j = d m_{ij}=d . We consider M ( i , : ) M(i,:) . We put in an array called connection \text{connection} every m i j m_{ij} such that m i j = d m_{ij}=d . Let's call c c elements of connection \text{connection} .
  • We go in M ( c , : ) M(c,:) for each c connection c \in \text{connection} . We put in an array called connectionII \text{connectionII} every m c , k m_{c,k} such that m c , k = 1 m_{c,k}=1 . Let's call c I I cII elements of connectionII \text{connectionII}
  • We come back in M ( i , : ) M(i,:) and write m i , c I I = d + 1 m_{i,cII}=d+1 for each c I I connectionII cII \in \text{connectionII}

We do this procedure for 1 i 100 1 \leq i \leq 100 . After i = 100 i=100 we check if there are any m i j = 0 M m_{ij}=0 \in M for i j i \neq j . If there aren't any, Step 2. ends. If there are still some, we repeat Step 2. for d = d + 1 d=d+1 .

At the end we'll have a matrix M M in which only the elements on the diagonal are zero. Eventually we find the maximum value of each row of M M

max ( M ( i , : ) ) = [ max ( m 1 , j ) max ( m 2 , j ) max ( m 100 , j ) ] \displaystyle \max(M(i,:))=\begin{bmatrix} \max(m_{1,j}) \\ \max(m_{2,j}) \\ \vdots \\ \max(m_{100,j}) \end{bmatrix}

and than we seek for the row in which there's the minimum of max ( M ( i , : ) ) \max(M(i,:)) . It cames out to be 1 \boxed{1} .

Here's the MATLAB code for this algorithm.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
clear
M=zeros(100,100);
d=1;
%% map (input file)
map=[1 2;
2 3;
1 4;
1 5;
5 6;
3 7;
...and so on];   
%% Step 1.
   for i=1:size(map,1)
       M(map(i,1),map(i,2))=d;
       M(map(i,2),map(i,1))=d;
   end
   %% Step 2.
       while size(find(M==0),1)>100
            d=d+1;
   for h=1:100
       connectionI=find(M(h,:)==d-1);
       for k=1:size(connectionI,2)
          connectionII=find(M(connectionI(k),:)==1);
          for n=1:size(connectionII,2)
              if connectionII(n)~=h && (M(h,connectionII(n))==0 || M(h,connectionII(n))>d)
                  M(h,connectionII(n))=d;
                  M(connectionII(n),h)=d;
              end
          end
       end
   end
       end
       %% Final Result
       central=find(min(max(M)));

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...