cf Educational Codeforces Round 52 B. Vasya and Isolated Vertices

原題:
B. Vasya and Isolated Vertices
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn’t contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1,2) and (2,1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.

Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of
n vertices and m edges.

Input
The only line contains two integers n and m (1≤n≤10^5,0≤m≤n(n−1)/2).

It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.

Output
In the only line print two numbers min and max — the minimum and maximum number of isolated vertices, respectively.

Examples
input
4 2
output
0 1
input
3 1
output
1 1

Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges
(1,2) and (3,4). To get one isolated vertex, we may construct a graph with edges (1,2) and (1,3).
In the second example the graph will always contain exactly one isolated vertex.

中文:
給你兩個數n和m,問你構造成無向圖,這個無向圖最少和最多能有多少個沒有任何邊連接的頂點。(沒有邊指向自身,沒有重邊)

代碼:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
ll n,m;
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        if(m==0)
        {
            cout<<n<<" "<<n<<endl;
            continue;
        }
        ll ans1,ans2;


        ans1=n-2*m;

        ll tmp;

        tmp=0;
        ans2=0;
        for(ll i=2;i<=n;i++)
        {
            tmp=(i*(i-1))/2;
            if(tmp>=m)
            {
                ans2=n-i;
                break;
            }
        }

        if(ans1<0)
            ans1=0;
        if(ans2<0)
            ans2=0;
        cout<<ans1<<" "<<ans2<<endl;


    }
    return 0;
}

思路:

n個定點構造一個有m條邊的無向圖,要求孤立頂點(沒有任何邊相連的頂點)最少,由於一條邊可以連接兩個定點,所以儘量讓每一條邊都連接兩個沒有任何邊的頂點,可以知道答案就是定點數除以2.

要求孤立頂點最多,那麼儘量把邊連刀已經有邊相連的頂點上,無向圖x個邊最多可以連接x(x-1)/2個節點,(題裏都給你公式了-_-),把m條邊使用完,判斷最後剩下多少頂點就是最多頂點數。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章