#P6519. [CEOI 2010] bodyguards (day1)

[CEOI 2010] bodyguards (day1)

Description

In a matrix, you need to place a required number of bodyguards in certain rows and columns. Each cell can contain at most one bodyguard. Determine whether there exists an arrangement such that some rows and columns contain exactly the required number of bodyguards.

Input Format

The first line contains an integer RR, meaning there are RR constraints on rows.

The next RR lines each contain two integers a,ba, b, meaning there are bb rows that must contain exactly aa bodyguards.

The next line contains an integer CC, meaning there are CC constraints on columns.

The next CC lines each contain two integers x,yx, y, meaning there are yy columns that must contain exactly xx bodyguards.

Output Format

Output one number in a single line. If such an arrangement exists, output 1; otherwise output 0.

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

Hint

Explanation of Sample 1

One row must contain two bodyguards, and two rows must contain one bodyguard; two columns must contain two bodyguards. One possible arrangement is:

XX
X.
.X

Here, X represents a bodyguard and . represents an empty cell.

Explanation of Sample 2

Two rows must be filled entirely with bodyguards, but one column is required to contain only one bodyguard, so it is impossible. Therefore, there is no solution.

Constraints

  • For 50%50\% of the testdata, it is guaranteed that R,C2000R, C \le 2000, and there are at most 10610^6 bodyguards.
  • For 100%100\% of the testdata, the total number of bodyguards is at most 101810^{18}. All numbers are positive integers not exceeding 10910^9, and 1R,C2×1051 \le R, C \le 2 \times 10^5.

Notes

Translated from CEOI 2010 day 1 T3 bodyguards.

The translation copyright belongs to the problem provider

https://www.luogu.com.cn/user/45475

Translated by ChatGPT 5