24h購物| | PChome| 登入
2013-07-05 15:21:55| 人氣1,373| 回應0 | 上一篇 | 下一篇

[UVA][greedy] 12428 - Enemy at the Gates

推薦 0 收藏 0 轉貼0 訂閱站台

 

BUET Inter-University Programming Contest

Problem E – Enemy At The Gates

 

Problem

The kingdom of ByteLand is in trouble. The enemies are going to attack ByteLand. The enemies know that ByteLand has exactly N cities and exactly M bidirectional roads and it is possible to go from any city to every other city directly or via other cities. They also know that any pair of cities can be directly connected by at most one road. But they do not have any information about which road connects which two cities.  They are planning to destroy all critical roads of ByteLand. A road is critical if after destroying that road only at least one pair of cities become disconnected. They are very optimistic so they expect to destroy maximum number of critical roads. What is the maximum number of critical roads that can be present in ByteLand according only to the information the enemies have about ByteLand?

 

Input

The first line of input contains T  which is the number of tests cases. Each case contains two integers N which is the number of cities and M which is the number of roads .

 

Output

For each test case output one integer the maximum number of critical roads that could be present in ByteLand.

Sample Input

Output for Sample Input

2

4 4

4 3

1

3

 

 

Explanation of Sample Cases

https://lh6.googleusercontent.com/e9e63zL2sx8aVpb2UJFAMxnPFI2U4mUvOyENOl4gp1Gr13xnHF3D9yTEjX_mE2B0JgQ_xDU3zejzly5aQdg8jjI_w0Azd8yOTg_oTKfBxvu4PoB3--c


Problemsetter:

Tasnim Imran Sunny

Special Thanks:

Shahriar Rouf Nafi

題目描述:

有 m 條邊,n 個點,一個重要道路被設定為如果這條道路消失了,會有點失聯。
問最多有幾條這種邊?

題目解法:

窮舉有多少個邊(重要道路),而這個邊只連接一個點到 1。
接著剩餘的點,做一個完全圖,把所有邊都用完。
很簡單的一個 greedy 策略



#include <stdio.h>

int main() {
    int testcase;
    long long n, m, i;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%lld %lld", &n, &m);
        if(m < n)
            printf("%lld\n", m);
        else {
            for(i = n-1; i >= 0; i--) {
                long long a = n-i;
                if(a*(a-1)/2 >= m-i) {
                    printf("%lld\n", i);
                    break;
                }
            }
        }
    }
    return 0;
}

台長: Morris
人氣(1,373) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][BIT] 1513 - Movie collection
此分類上一篇:[UVA] 12435 - Consistent Verdicts

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文