3 3
0<1
1<2
2<0
3 5
0<1
0>1
1<2
1>2
0<2
4 4
0<1
0>1
2<3
2>3
1 0
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
//说明:最低四位表示child分组状态,0:未分组,1/2/3:三组,4:裁判
//次低四位表示child检查状态,0:在以前数据中未出现,
//1:已出现一次,第二次出现即可进行检查判断,2:检查后可能是裁判,3:检查后不可能是裁判
//
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 FileStream(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';
data[n, 2] = s[k++];
while(k';
}
}
bool maybecp(int c,int k)//在前k组data中假设c为cp,是否没有矛盾
{
int j=0;
for (; j < n; j++) //清除分组情况
child[j] &= 0xf0;
int left = k + 1,begin=0;
for (j = 0; j <= k; j++) //有c的data置1,其余置0
{
if (data[j, 1] == c || data[j, 3] == c)
{ data[j, 0] = 1; left--; }
else
data[j, 0] = 0;
}
child[c] |= 4; //c置为cp
while (left > 1) //left为剩余data,剩余至少两条时进行计算
{
while (data[begin, 0] == 1) begin++; //找到第一个未计算的data
child[data[begin, 1]] += 1; //假定第一个child的分组
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 ((sign == '=' && za != zb) || (za != (zb + 1) % 3 + 1))return false; }
//一方已分组,一方未知,确定未知方的分组,并返回begin重新计算
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;
}
else //双方都未分组,原begin已计算,则重定为当前值
{ data[j, 0] = 0; left++; if (data[begin, 0] == 1)begin = j; }
}
}
return true; //没有矛盾,返回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) //若ab中有一个以前未出现
{
int t = a;
a = child[a] >> 4 == 0 ? a : b;
b = t != a ? t : b;
child[a] |= 0x10; //将a设为ready
if (child[b] >> 4 == 0)
{ child[b] |= 0x10; return; }
else
readytomaybe(b,k); //若b为ready则进行检查
return;
}
else
{
int t=-1,mlen = maybecps[0];
if (mlen == 1) t = maybecps[1];
for (int j = 1; j <=mlen; j++) //检查maybecps中的每个成员
if (!maybecp(maybecps[j], k))
{
child[maybecps[j]] += 0x10;
maybecps[j] = -1;
maybecps[0]--;
}
toleft(mlen); //调整maybecps
readytomaybe(a,k);
readytomaybe(b,k);
if (maybecps[0] == 1 && t != maybecps[1]) //若有变化,记下cpps
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!";
}
}