#P5406. [THUPC 2019] 找树

[THUPC 2019] 找树

Description

Define 1,2,3\otimes_1, \otimes_2, \otimes_3 as bitwise AND, bitwise OR, and bitwise XOR, respectively. Let aia_i denote the ii-th binary bit of aa from low to high. Define a new operation \oplus on ww-bit binary numbers such that for each bit (ab)i(a\oplus b)_i of the result aba\oplus b, we have (ab)i=aioibi(a\oplus b)_i = a_i \otimes_{o_i} b_i. It is not hard to verify that the operation \oplus is associative and commutative.

Given an undirected graph with nn vertices and mm edges, each edge weight is a ww-bit binary number (i.e., a non-negative integer less than 2w2^w). Please find a spanning tree of the original graph. Suppose the edge weights in your spanning tree are v1,,vn1v_1,\cdots,v_{n-1}. Please maximize v1v2vn1v_1\oplus v_2\oplus\cdots\oplus v_{n-1}.

Input Format

The first line contains two positive integers n,mn,m.

The second line contains a string of length ww. Each character in the string is one of &, |, ^ (representing AND, OR, XOR, respectively), indicating each oi\otimes_{o_i}.

The next mm lines each contain three non-negative integers x,y,vx,y,v, representing an edge connecting xx and yy with weight vv. It is guaranteed that 1x,yn1\leq x,y\leq n, 0v<2w0\le v < 2^w.

For all testdata, 1n70,1m5000,1w121\le n\le 70,1\le m\le 5000,1\le w \le 12.

Output Format

Output one line with one number, indicating the answer. If the graph is disconnected, output -1.

3 3
^
1 2 1
2 3 1
1 3 0
1

Hint

About the testdata

For some reasons, the testdata only keeps the last 2020 vertices.

From THUPC (THU Programming Contest, Tsinghua University Programming Contest) 2019.

Resources such as solutions can be found at https://github.com/wangyurzee7/THUPC2019.

Translated by ChatGPT 5