Reachable Pairs

시간 제한2초메모리 제한2048 MB

요약
매 시점마다 1..t-1번 노드를 지운 뒤(1번 노드는 이웃들을 서로 연결) 서로 도달 가능한 노드 쌍의 수를 구한다.
난이도

어려움10점 중 8점

유형
그래프, 유니온 파인드, 분할 정복, 백트래킹
정답자
아직 제출이 없습니다

문제

Consider an undirected graph with NN nodes labeled 1…N1\dots N and MM edges (1≤N≤2⋅105,0≤M≤4⋅1051\le N\le 2\cdot 10^5, 0\le M\le 4\cdot 10^5). You're given a binary string s_1s_2…s_Ns\_1s\_2\dots s\_N. At time tt for each t∈\[1,N]t\in \[1,N],

  • If s_t=0s\_t=0, node tt is removed from the graph.
  • If s_t=1s\_t=1, node tt is removed from the graph, and edges are added between every pair of neighbors that node tt had just before removal.

Note that in both cases, when a node is removed from the graph all of its incident edges are removed as well.

Count the number of pairs of nodes that can reach each other via some sequence of edges just before each of timesteps 1…N1\ldots N.

입력

The first line contains NN and MM.

The second line contains the bit string ss of length NN.

The next MM lines each contain two integers denoting an edge of the graph.

출력

NN lines, the number of pairs before each timestep.

예제3

  1. 예제 1

    입력
    3 2
    111
    1 2
    1 3
    
    예상 출력
    3
    1
    0
    
  2. 예제 2

    입력
    3 2
    000
    1 2
    1 3
    
    예상 출력
    3
    0
    0
    
  3. 예제 3

    입력
    7 8
    1101101
    6 2
    1 2
    2 3
    6 3
    1 3
    1 7
    4 5
    2 7
    
    예상 출력
    11
    7
    4
    2
    1
    1
    0