1405. Find Spies

Due to the large number of foreign spies, the security of S country is in a high crisis. As an importantmember of the National Security Bureau, SLF was assigned to find out these spies. If spy A has criminal evidence about spy B, it is said that spy A can expose B. Some spies take bribes and arewilling to hand over all the information they have as long as they are given a certain amount ofdollars. So if SLF can buy off some spies, SLF can control every spies in the spy network. Becauseonce SLF arrest a spy, he can get all the information the spy has, which makes it possible to arrestnew spies and master new intelligence.

The National Security Bureau provided SLF a list of all known bribe taking spies and the specificamount they were willing to accept. At the same time, SLF also know which spies have informationabout which spies. Suppose there are N spies in total, and each spy is identified by an integer from 1 to N.

Based on this information, please judge whether SLF can control all the spies. If so, find out theminimum amount of money SLF need to pay. Otherwise, output the identifier of the spy who cannotbe controlled.

输入

The first line containing two integer N \ (1≤N≤3000) and P \ (1≤P≤N), the number of spies whoare willing to take bribes.

In the next line P, there are two integers D_i \ (1≤D_i≤N) and W_i(0≤W_i≤20000) in each line. D_i indicated the identifier of spy who is willing to be bribed, and W_i is the amount he will be bribed.

There is only one integer M \ (1≤M≤8000) following the line. Then next line M, with two positive integers in each line, represents a pair of numbers (A_i, B_i). Spy A_i has the evidence of spy B_i . (1≤A_i, B_i≤N)

输出

If all spies can be controlled, the first line outputs ”YES” and the second line outputs the minimumbribe payment required. Otherwise, outputs ”NO”, and the spy with the smallest identifier who can’t be controlled is output in the second line.

样例

标准输入 复制文本
4 2
2 666
3 777
4
1 3
2 1
2 4
4 1
标准输出 复制文本
YES
666
标准输入 复制文本
5 2
1 12
4 32
5
1 3
2 1
5 3
3 4
4 1
标准输出 复制文本
NO
2

来源

SCNUCPC 2020 现场赛

登录以提交代码。
单点时限 1 秒
内存限制 256 MB
提交 12
通过 5