#P15936. [TOPC 2021] A Hard Problem

[TOPC 2021] A Hard Problem

题目描述

You are given a simple undirected graph consisting of nn nodes and mm edges. The nodes are numbered from 1 to nn, and the edges are numbered from 1 to mm. Node ii has a non-negative integer value ViV_i and the weight Wu,vW_{u,v} of edge {u,v}\{u, v\} is defined as VuVv\| V_u \oplus V_v \| where \oplus is the exclusive-or operator (equivalent to ^ in C) and x\| x \| is the number of set bits in the binary representation of non-negative integer xx.

The node values V1,V2,,VnV_1, V_2, \dots, V_n must satisfy qq constraints. Each of the constraints can be represented as a 5-tuple (t,u,i,v,j)(t, u, i, v, j).

  • if t=0t = 0, then getBit(Vu,i)=getBit(Vv,j)getBit(V_u, i) = getBit(V_v, j)
  • if t=1t = 1, then getBit(Vu,i)getBit(Vv,j)getBit(V_u, i) \neq getBit(V_v, j)

where the function getBit(x,i)getBit(x, i) returns the (i+1)(i + 1)-th least significant bit of xx. For examples, getBit(11,0)getBit(11, 0) is 1 and getBit(11,2)=0getBit(11, 2) = 0. In the C programming language, getBit(x,i)getBit(x, i) can be computed by ((x >> i) & 1U) if xx is a 32-bit unsigned integer and ii is a non-negative integer at most 31.

Unfortunately, some node values are missing now. Your task is to assign new values to them to minimize {u,v}EWu,v\sum_{\{u,v\} \in E} W_{u,v} without violating any given constraint. Please write a program to help yourself to complete this task.

输入格式

The input consists of five parts. The first part contains one line, and that line contains two positive integers nn and mm. nn is the number of nodes, and mm is the number of edges. The second part contains mm lines. Each of them contains two integers uu and vv, indicating an edge {u,v}\{u, v\} of the given graph. The third part contains one line. That line consists of nn space-separated integers x1,x2,,xnx_1, x_2, \dots, x_n. For any k{1,2,,n}k \in \{1, 2, \dots, n\}, if the node value VkV_k is missing, xkx_k will be -1; otherwise, VkV_k is xkx_k. The fourth part contains one integer qq, indicating the number of constraints. The fifth part contains qq lines, and each of them contains five space-separated integers t,u,i,v,jt, u, i, v, j indicating that (t,u,i,v,j)(t, u, i, v, j) is a constraint.

输出格式

Output an integer which is the minimum value under the qq constraints. If it is not possible to satisfy all the constraints, output -1.

4 4
1 3
1 2
3 2
0 3
-1 -1 60091 51514
2
1 2 0 1 5
0 2 6 0 15
13
3 2
0 1
1 2
-1 -1 -1
2
1 2 0 1 5
0 1 5 2 0
-1

提示

  • 1n10001 \leq n \leq 1000
  • 1m50001 \leq m \leq 5000
  • 1Vi<216-1 \leq V_i < 2^{16}
  • 0q80 \leq q \leq 8
  • t{0,1}t \in \{0, 1\}
  • 0u,v<n0 \leq u, v < n
  • 0i,j<160 \leq i, j < 16