codeforces 1065B Vasya and Isolated Vertices C语言答案

Vasya has got an undirected graph consisting of nn vertices and mm 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)(1,2) and (2,1)(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 nn vertices and mm edges.

Input

The only line contains two integers nn and m (1≤n≤105,0≤m≤n(n−1)2)m (1≤n≤105,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 minmin and maxmax — the minimum and maximum number of isolated vertices, respectively.

Examples

input

Copy

4 2

output

Copy

0 1

input

Copy

3 1

output

Copy

1 1

Note

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

In the second example the graph will always contain exactly one isolated vertex.

这道题目求最多与最少的孤立顶点,我没有学过图论。只是个人最朴素的简单想法。首先,最少的孤立顶点很好求,只要每一条边连接两个顶点就可以。min=n-2*m。最多的孤立顶点是这样的:一个完全无向图最多有n*(n-1)/2条边,那m条边的图最少的顶点数b就可以利用这个公式算出来,b*(b-1)=2*m。要注意它可能不是个整数。

边界情况

              注意m取值会超过int范围,再一个当m=0时,我的算法会陷入死循环。

附上代码

#include<stdio.h>
#include<math.h>


int main()
{
    long long temp,m,n,min,max;
    double x;
    while(scanf("%lld %lld",&n,&m)!=EOF)
    {
        if(m==0)
           {
              min=max=n;
              printf("%lld %lld\n",min,max);
              continue;
           }   
        if(2*m>n)
              min=0;
        else
              min=n-2*m;
        if(n*(n-1)/2==m)
              max=0;
        else
        {
            x=m;
            x=sqrt(x*2+0.25)+0.5;
            temp=(long long)x;
            temp=temp+1;
            while(temp*(temp-1)/2>=m)//若m==0就会无限循环
                 temp--;
            temp++; 
            max=n-temp; 
        }
        printf("%lld %lld\n",min,max);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sinat_37372543/article/details/88012215