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.
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 N 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 ith and jth 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?
InputThe first line contains an integer T(1 <= T <= 50), indicating the number of test cases.
Each test case contains several lines.
The first line contains two integers N,M(1 <= N <= 10000, 1 <= M <= 10000), representing how many round they will play and how many restricts are there for Alice.
The next line contains N integers B
1,B
2, ...,B
N, where B
i represents what item Bob will play in the i
th round. 1 represents Rock, 2 represents Paper, 3 represents Scissors.
The following M lines each contains three integers A,B,K(1 <= A,B <= N,K = 0 or 1) represent a restrict for Alice. If K equals 0, Alice must play the same on A
th and B
th round. If K equals 1, she must play different items on Ath and Bthround.OutputFor each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is "yes" or "no" represents whether Alice has a chance to win.Sample 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
Sample Output
Case #1: no Case #2: yes
1 #include<stack> 2 #include<cstdio> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #define mem(array) memset(array,0,sizeof(array)) 7 using namespace std; 8 9 const int maxn = 60005; 10 const int maxm = 999999; 11 12 stack<int> S; 13 14 struct node { 15 int to, next; 16 }e[maxm]; 17 18 int k, n, m, tot, index; 19 int Low[maxn], Dfn[maxn], head[maxn], cmp[maxn]; 20 bool use[maxn]; 21 22 void Inite() { 23 k = tot = index = 0; 24 mem(Dfn); mem(use); mem(cmp); 25 memset(head, -1, sizeof(head)); 26 } 27 28 void addedge(int u, int v) { 29 e[tot].to = v; 30 e[tot].next = head[u]; 31 head[u] = tot++; 32 } 33 34 void Tarjan(int u) { 35 Low[u] = Dfn[u] = ++index; 36 S.push(u); 37 use[u] = 1; 38 for (int i = head[u]; i != -1; i = e[i].next) { 39 int v = e[i].to; 40 if (!Dfn[v]) { 41 Tarjan(v); 42 Low[u] = min(Low[u], Low[v]); 43 } 44 else if (use[v]) { 45 Low[u] = min(Low[u], Dfn[v]); 46 } 47 } 48 int v; 49 if (Dfn[u] == Low[u]) { 50 k++; 51 do { 52 v = S.top(); 53 S.pop(); 54 cmp[v] = k; 55 use[v] = 0; 56 } while (v != u); 57 } 58 } 59 60 int main() 61 { 62 int kase; 63 scanf("%d", &kase); 64 for (int t = 1; t <= kase; t++) { 65 scanf("%d%d", &n, &m); 66 Inite(); 67 for (int i = 0; i < n; i++) { 68 addedge(i * 6 + 0, i * 6 + 3); 69 addedge(i * 6 + 0, i * 6 + 5); 70 addedge(i * 6 + 2, i * 6 + 1); 71 addedge(i * 6 + 2, i * 6 + 5); 72 addedge(i * 6 + 4, i * 6 + 1); 73 addedge(i * 6 + 4, i * 6 + 3); 74 } 75 for (int i = 0; i < n; i++) { 76 int v; 77 scanf("%d", &v); 78 if (v == 1) { 79 addedge(i * 6 + 3, i * 6 + 0); 80 addedge(i * 6 + 1, i * 6 + 2); 81 } 82 else if (v == 2) { 83 addedge(i * 6 + 5, i * 6 + 2); 84 addedge(i * 6 + 3, i * 6 + 4); 85 } 86 else { 87 addedge(i * 6 + 5, i * 6 + 0); 88 addedge(i * 6 + 1, i * 6 + 4); 89 } 90 } 91 for (int i = 0; i < m; i++) { 92 int a, b, c; 93 scanf("%d%d%d", &a, &b, &c); 94 a--; b--; 95 if (c) { 96 addedge(a * 6 + 0, b * 6 + 1); 97 addedge(a * 6 + 2, b * 6 + 3); 98 addedge(a * 6 + 4, b * 6 + 5); 99 addedge(b * 6 + 0, a * 6 + 1); 100 addedge(b * 6 + 2, a * 6 + 3); 101 addedge(b * 6 + 4, a * 6 + 5); 102 } 103 else { 104 addedge(a * 6 + 0, b * 6 + 0); 105 addedge(a * 6 + 2, b * 6 + 2); 106 addedge(a * 6 + 4, b * 6 + 4); 107 addedge(b * 6 + 0, a * 6 + 0); 108 addedge(b * 6 + 2, a * 6 + 2); 109 addedge(b * 6 + 4, a * 6 + 4); 110 } 111 } 112 bool flag = false; 113 printf("Case #%d: ", t); 114 for (int i = 0; i < 6 * n; i++) if (!Dfn[i]) Tarjan(i); 115 for (int i = 0; i < 3 * n; i++) if (cmp[i << 1] == cmp[(i << 1) | 1]) flag = true; 116 if (flag) printf("no "); 117 else printf("yes "); 118 } 119 return 0; 120 }