(简单)搜索 HOJ 1022 Spacecraft Malfunction

来源:互联网 发布:网络购票岫岩到沈阳 编辑:程序博客网 时间:2024/06/04 19:38

Spacecraft Malfunction

My Tags  (Edit)
Source : ACM ICPC Tehran Regional Contest 2002Time limit : 5 secMemory limit : 32 M

Submitted : 410, Accepted : 149

It is said that court intrigues started with people lying about other people, and then lying about other people’s lying, and so it went. The intriguers constantly looked for scapegoat who inevitably proved to be someone with the least power, though not always the least morality.

We have faced a similar problem, but this time, in our malfunctioning spacecraft! There are a number of units in the spacecraft. The units are so reliable that it would surprise us very much if more than one unit were faulty. If more than one is faulty, we would lose the probe, so we are sure that exactly one unit is faulty in our spacecraft.

We know that each unit checks exactly two others, and each unit will be checked by at least one other unit. A good unit will give accurate diagnosis of the units it checks. For example, if unit X is good and it says that Y is faulty and Z is good, then, in fact, Y is faulty and Z is good. However, a bad unit is unreliable. So, if unit X is faulty and makes the same statements, then Y may or may not be good, and Z may or may not be good either. Note that a unit cannot check itself.

Now suppose that you have the reports from all units and your duty is to find which unit is indeed faulty.

Input
The first line of the input file contains a single integer t (1 <= t <=10), the number of test cases, followed by the input data for each test case. The first line of each test case contains an integer n (3 <= n <= 100), the number of units, followed by n lines each describing a unit and the result of its checks. The line starts with a positive integer number which shows the identification number of the unit. After the id number, there are two pairs of checked unit id's and check results. A check result is a single character which is either Y or N, showing whether the result of checking is good or faulty respectively. As an example, the fourth line in the Sample Input section shows that unit 16 has checked unit 8 saying it is good, and has checked unit 32 saying it is faulty.

Output
There should be one line per test case containing either the id number of the faulty unit, or the word impossible (with lower-case letters), if it is impossible to find the faulty unit from the input data.

Sample Input

152 16 Y 32 N16 8 Y 32 N32 8 N 4 Y8 4 Y 2 Y4 2 Y 16 Y
Sample Output
32

题意:给出n个人,其中有一个人说的话不一定是真,其他人说的都是真的,确定哪一个人是不一定说真话的
思路:这题可以枚举每一个人作为faulty看是否有矛盾.
矛盾的情况有:
1.其他人之中有人认为另外一个人是faulty
2.其他人之中有人认为这个人是good
3.能够枚举出两个不同的人作为faulty都没有矛盾 
4.如果有人认为有两个人是faulty,那么显然其他人不可能是faulty

代码:
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
#define MAX 1000+10
#define MOD 100000000
const int inf = 0x7fffffff;
vector<int> id;
bool have_id[MAX];
bool check[MAX][MAX][2];
int check_N_num[MAX];
const int Yes = 0;
const int No = 1;

bool is_lyer(int lyer)
{
for (int i = 0 ; i < id.size() ; ++i) if (id[i]!=lyer)
{
int ID = id[i];
if (check[ID][lyer][Yes]) return false;
if (check_N_num[ID]>=2) return false;
if (check_N_num[ID]==1 && !check[ID][lyer][No]) return  false;
}
return true;
}
int main()
{
int T;
cin>>T;
int n;
while (T--)
{
scanf("%d",&n);
int X,Y,Z;
char YorN[5];
id.clear();
memset(have_id,0,sizeof(have_id));
memset(check,0,sizeof(check));
memset(check_N_num,0,sizeof(check_N_num));
for (int i = 0 ; i < n ; ++i)
{
scanf("%d%d%s%d",&X,&Y,YorN,&Z);
if (!have_id[X]) id.push_back(X);
if (YorN[0]=='Y')
check[X][Y][Yes] = true;
else 
{
check[X][Y][No] = true;
check_N_num[X]++;
}
scanf("%s",YorN);
if (YorN[0]=='Y')
check[X][Z][Yes] = true;
else 
{
check_N_num[X]++;
check[X][Z][No] = true;
}
}

int ans = -1;
for (int i = 0 ; i < id.size() ; ++i)
{
if (is_lyer(id[i]))
{
if (ans==-1)
ans = id[i];
else 
{
ans = -2;
printf("impossible\n");
break;
}
}
}

if (ans>0)
printf("%d\n",ans);
}
}
0 0