Cutting knife and stone cloth games

Source: Internet
Author: User

Question 4 of the first round of the 2006 Baidu STAR Program Design Competition

N children are playing a scissors and stone cloth game with you. One of the N children is a referee, And the other children are divided into three groups (it is not ruled out that some groups do not have any members), but you do not know who is a referee, I do not know the group of children. Then, the children started to play the scissors, stone, and cloth game for a total of m times. Every time they select two children for a round, you will be notified of the results, that is, the outcome of the two children, however, you won't know whether the children are scissors, stone, or cloth. It is known that each group of children has only one gesture (so the two children in the same group are always in harmony), and the referee chooses one gesture at a time, no one knows what the referee will do. Please guess who is the referee after the game is over. If you can guess who is the referee, you can determine who is the referee after the first few games.

Input Format:

The input file contains multiple groups of test data. The first behavior of each group of test data is two integers, N and M (1 ≤ n ≤ 500, 0 ≤ m ≤ 2000), which are the number of children and the number of times the game was played. In the next m row, each line has two integers separated by a symbol. The two integers are the numbers of the two children playing the game respectively. They are non-negative integers less than N. The possible values of the symbol are "=", ">", and "<", indicating the victory of the game, the victory of the first child, and the victory of the second child, respectively.

Output Format:

Each group of test data outputs a row. If you can guess who is the referee, the number of the child who is the referee is output. After the first few games are over, you can determine who is the referee. If you cannot determine who the referee is, or find that the game's victory or defeat is unreasonable (that is, no matter who the referee is), the corresponding information is output. For specific output formats, see the output sample.

Input example

3 3

0 <1

1 <2

2 <0

3 5

0 <1

0> 1

1 <2

1> 2

0 <2

4

0 <1

0> 1

2 <3

2> 3

1 0

Output example

Can not determine

PLAYER 1 can be determined to be the judge after 4 lines

Impossible

Player 0 can be determined to be the judge after 0 lines

Note:

There are five test datasets, each of which is an input file containing multiple groups of test data. Each test dataset is divided into 5, 10, 15, 30, and 40 points from easy to difficult, and each test dataset is executed once, each time, you must end the program within 3 seconds and output the correct answer to score.

All data is read from the standard input device (stdin/CIN) and written to the standard output device (stdout/cout.

Input N in the five test data sets is no greater than 20, 50, 100, 200, and 500, respectively, and each has 10 groups of test data.

// Note: The minimum four digits indicate the status of the child group, 0: Not group, 1/2/3: three groups, and 4: The referee // The minimum four digits indicate the status of the child check, 0: not in the previous data, // 1: it has already appeared once. You can check and judge the second time it appears. 2: it may be the referee after the check, 3: it cannot be the referee // using System; using System. collections. generic; using System. collections; using System. collections. specialized; using System. threading; using System. IO; public class childclqr {int n; int m; int [,] data; int [] child; int [] maybecps; int cpps =-1; string result; public string Result {get {Done (); return result ;}} public childclqr (int n, int m, string [] ss) {this. n = n; this. m = m; data = new int [m, 4]; child = new int [n]; maybecps = new int [n + 1]; for (int k = 0; k <m; k ++) {data [k, 0] = 0; sws (ss [k], k) ;}} public childclqr (string infile, string outfile) {FileStream fs = new FileStream (infile, FileMode. open, FileAccess. read); StreamReader sr = new StreamReader (fs); FileStream fo = new Fi LeStream (outfile, FileMode. Create, FileAccess. Write); StreamWriter sw = new StreamWriter (fo); string s = sr. ReadLine (); while (s! = Null) {string [] t = s. split (); n = int. parse (t [0]); m = int. parse (t [1]); t = new string [m]; for (int j = 0; j <m; j ++) t [j] = sr. readLine (); childclqr c = new childclqr (n, m, t); sw. writeLine (c. result); s = sr. readLine ();} sr. close (); sw. close ();} void sws (string s, int n) {data [n, 1] = 0; data [n, 3] = 0; int k = 0; while (s [k] <= '9' & s [k]> = '0') data [n, 1] = 10 * data [n, 1] + s [k ++]-'0'; da Ta [n, 2] = s [k ++]; while (k <s. length) data [n, 3] = 10 * data [n, 3] + s [k ++]-'0'; if (data [n, 2] = '<') {data [n, 2] = data [n, 1]; data [n, 1] = data [n, 3]; data [n, 3] = data [n, 2]; data [n, 2] = '>';} bool maybecp (int c, int k) // assume that c is cp in the first k data Group, and there is no conflict {int j = 0; for (; j <n; j ++) // clear the group status child [j] & = 0xf0; int left = k + 1, begin = 0; for (j = 0; j <= k; j ++) // set data with c to 1, and other values to 0 {if (data [j, 1] = c | Data [j, 3] = c) {data [j, 0] = 1; left --;} else data [j, 0] = 0 ;} child [c] | = 4; // c is set to cp while (left> 1) // left is the residual data. If there are at least two remaining data entries, the calculation is performed {while (data [begin, 0] = 1) begin ++; // locate the first uncomputed data child [data [begin, 1] + = 1; // assume that the first child group is for (j = begin; j <= k; j ++) if (data [j, 0] = 0) {int za = child [data [j, 1] % 16, zb = child [data [j, 3] % 16; int sign = data [j, 2]; data [j, 0] = 1; left --; if (za> 0 & Zb> 0) // if both parties are grouped, the system determines whether there is a conflict. if yes, an error is returned {if (sign = '& za! = Zb) | (za! = (Zb + 1) % 3 + 1) return false;} // one party has been grouped and one party has been unknown. Determine the group of the unknown party, return begin and recalculate else if (za = 0 & zb> 0) {if (sign = ') child [data [j, 1] + = zb; else child [data [j, 1] + = (zb + 1) % 3 + 1; j = begin-1 ;} else if (za> 0 & zb = 0) {if (sign = ') child [data [j, 3] + = za; else child [data [j, 3] + = za % 3 + 1; j = begin-1;} both sides of else // are not grouped. The original begin has been calculated, then it is redefined as the current value {data [j, 0] = 0; left ++; if (data [beg In, 0] = 1) begin = j ;}} return true; // no conflict, returns true} void readytomaybe (int B, int k) {if (child [B]> 4 = 1) {if (maybecp (B, k) {child [B] + = 0x10; maybecps [++ maybecps [0] = B; if (maybecps [0] = 1) cpps = k; return ;} else {child [B] + = 0x20; return ;}} void toleft (int mlen) {if (mlen! = Maybecps [0] & maybecps [0]> 0) {int r = 1; while (maybecps [r]! =-1) r ++; int p = r + 1; while (r <= maybecps [0]) {while (maybecps [p] =-1) p ++; maybecps [r ++] = maybecps [p]; maybecps [p ++] =-1 ;}} void anay (int k) {int a = data [k, 1]; int B = data [k, 3]; if (child [a]> 4 = 0 | child [B]> 4 = 0) // if one of the AB statements does not appear before {int t =; a = child [a]> 4 = 0? A: B; B = t! =? T: B; child [a] | = 0x10; // set a to ready if (child [B]> 4 = 0) {child [B] | = 0x10; return;} else readytomaybe (B, k); // If B is ready, check return ;} else {int t =-1, mlen = maybecps [0]; if (mlen = 1) t = maybecps [1]; for (int j = 1; j <= mlen; j ++) // check if (! Maybecp (maybecps [j], k) {child [maybecps [j] + = 0x10; maybecps [j] =-1; maybecps [0] --;} toleft (mlen); // adjust maybecps readytomaybe (a, k); readytomaybe (B, k); if (maybecps [0] = 1 & t! = Maybecps [1]) // if there is any change, write down cpps = k ;}} void done () {if (m = 0) {result = "Player 0 can be determined to be the judge after 0 lines"; return ;}for (int k = 0; k <m; k ++) anay (k); if (maybecps [0] = 0) result = "Impossible! "; Else if (maybecps [0] = 1) result = string. format ("Child {0} is judge, he can be judged after {1} lines", maybecps [1], cpps + 1); else result = "Can not determine! ";}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.