CF1707C.DFS Trees

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given a connected undirected graph consisting of nn vertices and mm edges. The weight of the ii -th edge is ii .

Here is a wrong algorithm of finding a minimum spanning tree (MST) of a graph:

<pre class="verbatim"><br></br>vis := an array of length n<br></br>s := a set of edges<br></br><br></br>function dfs(u):<br></br>    vis[u] := true<br></br>    iterate through each edge (u, v) in the order from smallest to largest edge weight<br></br>        if vis[v] = false<br></br>            add edge (u, v) into the set (s)<br></br>            dfs(v)<br></br><br></br>function findMST(u):<br></br>    reset all elements of (vis) to false<br></br>    reset the edge set (s) to empty<br></br>    dfs(u)<br></br>    return the edge set (s)<br></br>

Each of the calls findMST(1), findMST(2), ..., findMST(n) gives you a spanning tree of the graph. Determine which of these trees are minimum spanning trees.

输入格式

The first line of the input contains two integers nn , mm ( 2n1052\le n\le 10^5 , n1m2105n-1\le m\le 2\cdot 10^5 ) — the number of vertices and the number of edges in the graph.

Each of the following mm lines contains two integers uiu_i and viv_i ( 1ui,vin1\le u_i, v_i\le n , uiviu_i\ne v_i ), describing an undirected edge (ui,vi)(u_i,v_i) in the graph. The ii -th edge in the input has weight ii .

It is guaranteed that the graph is connected and there is at most one edge between any pair of vertices.

输出格式

You need to output a binary string ss , where si=1s_i=1 if findMST(i) creates an MST, and si=0s_i = 0 otherwise.

输入输出样例

  • 输入#1

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

    输出#1

    01111
  • 输入#2

    10 11
    1 2
    2 5
    3 4
    4 2
    8 1
    4 5
    10 5
    9 5
    8 2
    5 7
    4 6

    输出#2

    0011111011

说明/提示

Here is the graph given in the first example.

There is only one minimum spanning tree in this graph. A minimum spanning tree is (1,2),(3,5),(1,3),(2,4)(1,2),(3,5),(1,3),(2,4) which has weight 1+2+3+5=111+2+3+5=11 .

Here is a part of the process of calling findMST(1):

  • reset the array vis and the edge set s;
  • calling dfs(1);
  • vis[1] := true;
  • iterate through each edge (1,2),(1,3)(1,2),(1,3) ;
  • add edge (1,2)(1,2) into the edge set s, calling dfs(2):
    • vis[2] := true
    • iterate through each edge (2,1),(2,3),(2,4)(2,1),(2,3),(2,4) ;
    • because vis[1] = true, ignore the edge (2,1)(2,1) ;
    • add edge (2,3)(2,3) into the edge set s, calling dfs(3):
      • ...

In the end, it will select edges (1,2),(2,3),(3,5),(2,4)(1,2),(2,3),(3,5),(2,4) with total weight 1+4+2+5=12>111+4+2+5=12>11 , so findMST(1) does not find a minimum spanning tree.

It can be shown that the other trees are all MSTs, so the answer is 01111.

首页