CF51F.Caterpillar

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

An undirected graph is called a caterpillar if it is a connected graph without cycles and it has such a path pp that any vertex is located at a distance of at most 1 from the path pp . The caterpillar can contain loops (edges from a vertex to itself) but cannot contain multiple (parallel) edges.

The picture contains an example of a caterpillar:

You are given an undirected graph GG . You are allowed to do a merging operations, each such operation merges two vertices into one vertex. For that two any vertices aa and bb ( aba≠b ) are chosen. These verteces are deleted together with their edges (which are incident to at least one of the vertices aa or bb ) but a new vertex ww is added together with edges (x,w)(x,w) for each edge ( a,wa,w ) and/or ( b,wb,w ). If there was the edge (a,b)(a,b) it transforms to the loop (w,w)(w,w) . The resulting graph (after the merging operation) may contain multiple (parallel) edges between pairs of vertices and loops. Let us note that this operation decreases the number of vertices of graph by 1 but leaves the number of edges in the graph unchanged.

The merging operation can be informally described as a unity of two vertices of the graph into one with the natural transformation of the graph edges.

You may apply this operation consecutively and make the given graph to be a caterpillar. Write a program that will print the minimal number of merging operations required to make the given graph a caterpillar.

输入格式

The first line contains a pair of integers nn , mm ( 1<=n<=2000;0<=m<=1051<=n<=2000;0<=m<=10^{5} ), where nn represents the number of vertices in the graph and mm is the number of edges in it. Then the following mm lines contain edge descriptions, one edge description per line. Every line contains a pair of integers ai,bia_{i},b_{i} ( 1<=ai,bi<=n;aibi1<=a_{i},b_{i}<=n;a_{i}≠b_{i} ), ai,bia_{i},b_{i} which represent the indices of the vertices connected by the edge. The vertices are numbered from 1 to nn . In the given graph it will be no more than one edge between any pair of vertices. The given graph is not necessarily connected.

输出格式

Print the minimal required number of operations.

输入输出样例

  • 输入#1

    4 4
    1 2
    2 3
    3 4
    4 2
    

    输出#1

    2
    
  • 输入#2

    6 3
    1 2
    3 4
    5 6
    

    输出#2

    2
    
  • 输入#3

    7 6
    1 2
    2 3
    1 4
    4 5
    1 6
    6 7
    

    输出#3

    1
    
首页