PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000079. Các cặp đỉnh còn liên thông (Reachable Pairs)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Các cặp đỉnh còn liên thông (Reachable Pairs)

Reachable Pairs

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected graph with NN vertices and MM edges, along with a binary string ss of length NN. At time ii, vertex ii and its incident edges are removed. If si=1s_i=1, first connect every pair of its current neighbors by an edge; if si=0s_i=0, add no edges. Edges created this way persist for later steps.

Immediately before removing vertex ii, count the unordered pairs of distinct remaining vertices that can reach each other along a path. Output this count for every i=1,…,Ni=1,\ldots,N.

Each operation acts on the graph remaining after previous operations. When si=1s_i=1, immediately before removing vertex ii, a direct edge is added between every pair of its currently present neighbors; these edges remain after vertex ii disappears. When si=0s_i=0, the incident edges disappear without creating replacements.

Count pairs of distinct surviving vertices that belong to the same connected component. Each unordered pair is counted once, regardless of how many paths connect its endpoints. Answer ii corresponds to the state immediately before removal of vertex ii.

Input

The first line contains N,MN,M. The second line is a binary string ss of length NN. The following MM lines describe edges ui,viu_i,v_i.

Output

Print NN lines; line ii contains the number of connected unordered pairs immediately before operation ii.

Subtasks

  • Subtask 1 (20 points): 1≤N≤301\le N\le 30; 0≤M≤1200\le M\le 120; s∈{0,1}Ns\in\{0,1\}^N; 1≤ui,vi≤N1\le u_i,v_i\le N, ui≠viu_i\ne v_i.
  • Subtask 2 (30 points): 1≤N≤30001\le N\le 3000; 0≤M≤60000\le M\le 6000; s∈{0,1}Ns\in\{0,1\}^N; 1≤ui,vi≤N1\le u_i,v_i\le N, ui≠viu_i\ne v_i.
  • Subtask 3 (50 points): 1≤N≤2000001\le N\le 200000; 0≤M≤4000000\le M\le 400000; s∈{0,1}Ns\in\{0,1\}^N; 1≤ui,vi≤N1\le u_i,v_i\le N, ui≠viu_i\ne v_i.

Examples

Input

3 2
111
1 2
1 3

Output

3
1
0

Explanation

Before time 11, all three vertices are connected, yielding inom{3}{2}=3 pairs. Because s1=1s_1=1, vertices 2,32,3 are directly joined before vertex 11 is removed. At time 22, they form the single reachable pair. Before time 33, only vertex 33 remains and there are no pairs.