uva 10765 - Doves and bombs(割点&BCC)

Problem G

Doves and Bombs

Input: Standard Input

Output: Standard Output

Time Limit: 2 Seconds

It is the year 95 ACM (After the Crash of Microsoft). After many years of peace, a war has broken out. Your nation, the island of Evergreen Macros And Confusing Shortcuts (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 government 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 ≤ mn).

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 Output for Sample Input

8 4

0 4

1 2

2 3

2 4

3 5

3 6

3 7

6 7

-1 -1

0 0

2 3

3 3

4 2

0 1


Problemsetter: Paul Shelly

一道水题,就是要注意dfs的根节点的鸽子值要特判一下。开始直接套书上模版做,一直wa,后来重敲一遍,去掉模版中这道题用不到的东西,就过了,真尼玛奇葩。
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
#include <cstring>
using namespace std;

const int maxn = 10000 + 5;

int pre[maxn],iscut[maxn],dfs_clock;
vector<int> G[maxn];

int dfs(int u,int fa){
    int lowu = pre[u] = ++dfs_clock;
    int child = 0;
    for(int i = 0;i < G[u].size();i++){
        int v = G[u][i];
        if(!pre[v]){
            child++;
            int lowv = dfs(v,u);
            lowu = min(lowu,lowv);
            if(lowv >= pre[u]){
                iscut[u]++;
            }
        }
        else if(pre[v] < pre[u] && v != fa){
            lowu = min(lowu,pre[v]);
        }
    }
    if(fa < 0 && child == 1) iscut[u] = 0;
    return lowu;
}

struct Node{
    int px,val;
}ans[maxn];

void init(int n){
    for(int i = 0;i < n;i++){
        G[i].clear();
        ans[i].px = i;
    }
    memset(pre,0,sizeof(pre));
    memset(iscut,0,sizeof(iscut));
    dfs_clock = 0;
}

bool cmp(Node a,Node b){
    if(a.val == b.val) return a.px < b.px;
    return a.val > b.val;
}

int main(){
    int u,v,n,m;
    while(scanf("%d%d",&n,&m)){
        if(n == 0 && m == 0) break;
        init(n);
        while(scanf("%d%d",&u,&v)){
            if(u == -1 && v == -1) break;
            G[u].push_back(v);
            G[v].push_back(u);
        }
        dfs(0,-1);
        if(iscut[0] == 0){
            ans[0].val = 1;
        }
        for(int i = 1;i < n;i++){
            if(iscut[i] == 0) ans[i].val = 1;
            else ans[i].val = ++iscut[i];
        }
        sort(ans,ans+n,cmp);
        for(int i = 0;i < m;i++) printf("%d %d\n",ans[i].px,ans[i].val);
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值