CF117E.Tree or not Tree

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given an undirected connected graph GG consisting of nn vertexes and nn edges. GG contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off.

You are also given mm queries represented as (v,u)(v,u) — change the state of all edges on the shortest path from vertex vv to vertex uu in graph GG . If there are several such paths, the lexicographically minimal one is chosen. More formally, let us consider all shortest paths from vertex vv to vertex uu as the sequences of vertexes v,v1,v2,...,uv,v_{1},v_{2},...,u . Among such sequences we choose the lexicographically minimal one.

After each query you should tell how many connected components has the graph whose vertexes coincide with the vertexes of graph GG and edges coincide with the switched on edges of graph GG .

输入格式

The first line contains two integers nn and mm ( 3<=n<=1053<=n<=10^{5} , 1<=m<=1051<=m<=10^{5} ). Then nn lines describe the graph edges as aa bb ( 1<=a,b<=n1<=a,b<=n ). Next mm lines contain the queries as vv uu ( 1<=v,u<=n1<=v,u<=n ).

It is guaranteed that the graph is connected, does not have any self-loops or multiple edges.

输出格式

Print mm lines, each containing one integer — the query results.

输入输出样例

  • 输入#1

    5 2
    2 1
    4 3
    2 4
    2 5
    4 1
    5 4
    1 5
    

    输出#1

    3
    3
    
  • 输入#2

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

    输出#2

    4
    3
    

说明/提示

Let's consider the first sample. We'll highlight the switched on edges blue on the image.

  • The graph before applying any operations. No graph edges are switched on, that's why there initially are 5 connected components.

  • The graph after query v=5,u=4v=5,u=4 . We can see that the graph has three components if we only consider the switched on edges.

  • The graph after query v=1,u=5v=1,u=5 . We can see that the graph has three components if we only consider the switched on edges.

Lexicographical comparison of two sequences of equal length of kk numbers should be done as follows. Sequence xx is lexicographically less than sequence yy if exists such ii ( 1<=i<=k1<=i<=k ), so that xi<yix_{i}<y_{i} , and for any jj ( 1<=j<i1<=j<i ) xj=yjx_{j}=y_{j} .

首页