A group of children play a simple stone cloth game, these children will be divided into three groups (may be no one in the group) + a free person (more reasonable than the translation into the referee), the same groups of children will only be the same gesture (not change), but the referee can make arbitrary gestures, these children can play with each other scissors,a> b for a win B, < on behalf of the loss, = flat, give you these children play the results of the game, asked if you can find the free man, if not find out, on the outputcan not determine, if found impossible to have such a result, unless there is more than one free person that outputImpossible, if you are a free person, then output this free person, and the first line number that can judge the position of this free person ...
///////////////////////////////////similar to the food chain, the search for the free can enumerate everyone, it's easier not to say#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <stack>
usingnamespaceStd
ConstintMAXN =505;
intF[MAXN], VAL[MAXN];
//0 for the same, 1 for win, 2 for the loser.
structnode{intU, V, rel;} data[maxn*4];
intFind (intX
{
intk = F[x];
if(f[x]! = x)
{
F[X] = Find (f[x]);
VAL[X] = (val[x]+val[k])%3;
}
returnF[X];
}
//If K is the referee has a contradiction to produce, there is a return to the number of conflicting lines, no return-1
intSolve (intKintNintM
{//It's like the food chain.
intI, u, V, Ru, rv;
for(i=0; i<n; i++)
F[i] = i, val[i]=0;
for(i=0; i<m; i++)
{
U = data[i].u, v = data[i].v;
if(U! = k && v! = k)
{
RU = Find (u), rv = Find (v);
if(ru = = RV && (val[v]+data[i].rel)%3! = Val[u])
returnI
F[ru] = RV;
Val[ru] = (data[i].rel-val[u]+val[v]+3)%3;
}
}
return-1;
}
intMain ()
{
intI, N, M;
while(SCANF ("%d%d", &n, &m)! = EOF)
{
CharCh
for(i=0; i<m; i++)
{
scanf"%d%c%d", &data[i].u, &ch, &DATA[I].V);
if(ch = ='=')
Data[i].rel =0;
Elseif(ch = ='>')
Data[i].rel =1;
Else
Data[i].rel =2;
}
intp = Solve (-1, N, M);
//There's no contradiction, no judging who's the referee.
if(N! =1&& p = =-1)
printf"Can not determine\n");
Elseif(N = =1)//There's only one person, only the referee.
printf"Player 0 can determined to is the judge after 0 lines\n");
Else
{
intk=0, Q, J;//K Records how many of them can serve as referees
for(i=0; i<n; i++)
{
j = Solve (i, N, M);
if(j = =-1)
k++, q = i;
Else
p = Max (P, J);//because the requirements can be seen in the recent judgment of the place, in fact, is the other point of contradiction in the farthest place
if(k >1)
Break;
}
if(k = =0)
printf"impossible\n");
Elseif(k >1)
printf"Can not determine\n");
Else
printf"Player%d can be determined to is the judge after%d lines\n", Q, p+1);
}
}
return0;
}
k-rochambeau-poj2912 (similar food chain)