#A1002. 【模板】2-SAT

【模板】2-SAT

题目描述

题目源自 hdu 4115。Mars 注:其实英文题面不难。

Conflicts are everywhere in the world, from the young to the elderly, from families to countries. Conflicts cause quarrels, fights or even wars. How wonderful the world will be if all conflicts can be eliminated.

Edward contributes his lifetime to invent a 'Conflict Resolution Terminal' and he has finally succeeded. This magic item has the ability to eliminate all the conflicts. It works like this:

If any two people have conflict, they should simply put their hands into the 'Conflict Resolution Terminal' (which is simply a plastic tube). Then they play 'Rock, Paper and Scissors' in it. After they have decided what they will play, the tube should be opened and no one will have the chance to change. Finally, the winner have the right to rule and the loser should obey it. Conflict Eliminated!

But the game is not that fair, because people may be following some patterns when they play, and if the pattern is founded by others, the others will win definitely.//这四段写石头剪刀布能消除矛盾,但 Bob 出的有规律

Alice and Bob always have conflicts with each other so they use the 'Conflict Resolution Terminal' a lot. Sadly for Bob, Alice found his pattern and can predict how Bob plays precisely. She is very kind that doesn't want to take advantage of that. So she tells Bob about it and they come up with a new way of eliminate the conflict:

They will play the 'Rock, Paper and Scissors' for NN round. Bob will set up some restricts on Alice.

But the restrict can only be in the form of "you must play the same (or different) on the ithi^{th} and jthj^{th} rounds". If Alice loses in any round or break any of the rules she loses, otherwise she wins.

Will Alice have a chance to win?

输入格式

The first line contains an integer T(1T50)T(1 \le T \le 50), indicating the number of test cases.

Each test case contains several lines.

The first line contains two integers N,M(1N10000,1M10000)N,M(1 \le N \le 10000, 1 \le M \le 10000), representing how many round they will play and how many restricts are there for Alice.

The next line contains NN integers B1,B2,...,BNB_1,B_2, ...,B_N, where BiB_i represents what item Bob will play in the ithi^{th} round. 11 represents Rock, 22 represents Paper, 33 represents Scissors.//即 Bob 所出的

The following MM lines each contains three integers A,B,K(1A,BN,K=0 or 1)A,B,K(1 \le A,B \le N,K = 0\ or\ 1) represent a restrict for Alice. If KK equals 00, Alice must play the same on AthA^{th} and BthB^{th} round. If KK equals 11, she must play different items on AthA^{th} and BthB^{th} round.//限制要求

输出格式

For each test case in the input, print one line: 'Case #X: Y', where XX is the test case number (starting with 11) and YY is yes or no represents whether Alice has a chance to win.

样例

Input:

2
3 3
1 1 1
1 2 1
1 3 1
2 3 1
5 5
1 2 3 2 1
1 2 1
1 3 1
1 4 1
1 5 1
2 3 0

Output:

Case #1: no
Case #2: yes

数据范围与提示

Hint:

'Rock, Paper and Scissors' is a game which played by two person. They should play Rock, Paper or Scissors by their hands at the same time. Rock defeats scissors, scissors defeats paper and paper defeats rock. If two people play the same item, the game is tied.

Origin:

2011 Asia ChengDu Regional Contest