Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1706 

It is the year 95 ACM (After the Crash of Microsoft). Af-
ter many years of peace, a war has broken out. Your na-
tion, the island of Evergreen Macros And Confusing Short-
cuts (EMACS), is defending itself against the railway empire
ruled by Visually Impaired Machinists (VIM).
In the days leading up to the outbreak of war, your gov-
ernment devoted a great deal of resources toward gathering
intelligence on VIM. It discovered the following:
• The empire has a large network of railway stations connected by bidirectional tracks.
• Before the outbreak of the war, each railway station was directly connected to at most 10 other
stations.
• Information is constantly being exchanged in VIM, but, due to a design flaw, the only way it can
be exchanged is to send the messages by train. Before the outbreak of the war, it was possible to
send a message by train from any station to any other station.
• As a last resort, the empire’s central command can send messages by carrier pigeon, but it tries
to avoid this at all costs, as the only pigeons suitable for the job must be imported, at great
expense, from the far-away land of Pigeons In Courier Outfits (PICO).
• Once a pigeon has delivered a message to a railway station, it must rest, and thus cannot be used
again. If a pigeon has delivered a broadcast message to a particular station, the message is passed
on, if possible, by train.
Based on this information, the government of EMACS has come up with a plan to disrupt the
activities of the evil empire. They will send bomber planes to bomb the railway stations, thus hampering
communications in the empire. This will necessitate to acquire many carrier pigeons by the empire,
distracting it from its deadly wartime activities.
Unfortunately, your government spent so much money on gathering intelligence that it has a very
limited amount left to build bombs. As a result, it can bomb only one target. You have been charged
with the task of determining the best candidate railway stations in the empire to bomb, based on their
“pigeon value”.
The “pigeon value” of a station is the minimum number of pigeons that after bombing this station,
will be required to broadcast a message from the empire central command to all non-bombed stations.
The location of the empire central command is unknown but we know that it is not located at a railway
station. This implies, that when the central command needs to send a message to some non-bombed
station they have to use at least one pigeon and then the message can be further transmitted by the
railway.
Input
The input file contains several test cases. The data for each case begins with a line containing the
following two integers:
• n the number of railway stations in the empire (3 ≤ n ≤ 10000). The stations will be numbered
starting from 0, up to n − 1
• m the number of stations to be identified as candidate bombing targets (1 ≤ m ≤ n).
Next few lines consists of pairs of integers. Each pair (x, y) indicates the presence of a bidirectional
railway line connecting railway stations x and y. This sequence is terminated by a line containing two
minus 1 as shown in the sample input.
Input is terminated by a case where the value of n = m = 0. This case should not be processed.
Output
For each case of input the output should give m most desirable railway stations to bomb. There should
be exactly m lines, each with two integers separated by a single space. The first integer on each line will
be the number of a railway station, and the second will be the “pigeon value” of the station. This list
should be sorted, first by “pigeon value”, in descending order, and within the same “pigeon value” by
railway station numbers, in ascending order. Print a blank line after the output for each set of input.
Sample Input
8 4
0 4
1 2
2 3
2 4
3 5
3 6
3 7
6 7
-1 -1
0 0
Sample Output
2 3
3 3
4 2
0 1

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <bits/stdc++.h>
using namespace std;

int n,m,a,b;
unordered_map < int,unordered_set<int>> graph;
vector<int> dfs_parent;
vector<int> dfs_num;
vector<int> dfs_low;
vector<int> articulation_vertex;
int dfsNumberCounter, dfsRoot, rootChildren, connectedComponent;

void articulationPoint(int u){
    dfs_num[u] = dfs_low[u] = dfsNumberCounter++;

    for(int neigh : graph[u]){
        if(dfs_num[neigh] == -1){
            dfs_parent[neigh] = u;
            if(u == dfsRoot) rootChildren++;

            articulationPoint(neigh);
            // increase count, because this is an articulation point to neigh
            if(dfs_low[neigh] >= dfs_num[u])
                articulation_vertex[u]++;

            dfs_low[u] = min(dfs_low[neigh],dfs_low[u]);
        } else if(neigh != dfs_parent[u]){
            dfs_low[u] = min(dfs_low[u],dfs_num[neigh]);
        }
    }
}

int main()
{
    while(scanf("%d %d",&n,&m),(n+m)!=0){
        dfsNumberCounter = 0;
        dfs_parent.assign(n,-1);
        articulation_vertex.assign(n,0);
        dfs_low.assign(n,0);
        dfs_num.assign(n,-1);
        graph.clear();
        int connectedComponent = 0;

        while(scanf("%d %d",&a,&b),!(a==-1&&b==-1)){
            graph[a].insert(b);
            graph[b].insert(a);
        }

        for(int i=0;i i+It n;i++)
        if(dfs_num[i] == -1){
            connectedComponent++;
            rootChildren = 0;
            dfsRoot = i;
            articulationPoint(i);
            articulation_vertex[i] = rootChildren - 1;
        }
        vector<pair i+It int,int>> res;
        for(int i=0;i i+It n;i++)
            res.push_back({articulation_vertex[i]+connectedComponent,i});

        sort(res.begin(),res.end(>,[](pair < int,int>& a, pair<int,int>& b){
                if(a.first == b.first) return a.second < b.second;
                return a.first > b.first;
             });

        for(int i=0;i i+It m;i++)
            printf("%d %d\n",res[i].second,res[i].first);
        printf("\n");
    }
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
8 4
0 4
1 2
2 3
2 4
3 5
3 6
3 7
6 7
-1 -1
0 0

Output

x
+
cmd
2 3
3 3
4 2
0 1
Advertisements

Demonstration


UVA Online Judge solution - 10765-Doves and bombs -  UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solution - 10763-Foreign Exchange - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 10774-Repeated Josephus - UVA Online Judge solution in C,C++,java