1. 程式人生 > 實用技巧 >True Liars POJ - 1417

True Liars POJ - 1417

True Liars

After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ashore on a foggy island. Though he was exhausted and despaired, he was still fortunate to remember a legend of the foggy island, which he had heard from patriarchs in his childhood. This must be the island in the legend. In the legend, two tribes have inhabited the island, one is divine and the other is devilish, once members of the divine tribe bless you, your future is bright and promising, and your soul will eventually go to Heaven, in contrast, once members of the devilish tribe curse you, your future is bleak and hopeless, and your soul will eventually fall down to Hell.

In order to prevent the worst-case scenario, Akira should distinguish the devilish from the divine. But how? They looked exactly alike and he could not distinguish one from the other solely by their appearances. He still had his last hope, however. The members of the divine tribe are truth-tellers, that is, they always tell the truth and those of the devilish tribe are liars, that is, they always tell a lie.

He asked some of them whether or not some are divine. They knew one another very much and always responded to him "faithfully" according to their individual natures (i.e., they always tell the truth or always a lie). He did not dare to ask any other forms of questions, since the legend says that a devilish member would curse a person forever when he did not like the question. He had another piece of useful informationf the legend tells the populations of both tribes. These numbers in the legend are trustworthy since everyone living on this island is immortal and none have ever been born at least these millennia.

You are a good computer programmer and so requested to help Akira by writing a program that classifies the inhabitants according to their answers to his inquiries.

Input

The input consists of multiple data sets, each in the following format :

n p1 p2
xl yl a1
x2 y2 a2
...
xi yi ai
...
xn yn an

The first line has three non-negative integers n, p1, and p2. n is the number of questions Akira asked. pl and p2 are the populations of the divine and devilish tribes, respectively, in the legend. Each of the following n lines has two integers xi, yi and one word ai. xi and yi are the identification numbers of inhabitants, each of which is between 1 and p1 + p2, inclusive. ai is either yes, if the inhabitant xi said that the inhabitant yi was a member of the divine tribe, or no, otherwise. Note that xi and yi can be the same number since "are you a member of the divine tribe?" is a valid question. Note also that two lines may have the same x's and y's since Akira was very upset and might have asked the same question to the same one more than once.

You may assume that n is less than 1000 and that p1 and p2 are less than 300. A line with three zeros, i.e., 0 0 0, represents the end of the input. You can assume that each data set is consistent and no contradictory answers are included.

Output

For each data set, if it includes sufficient information to classify all the inhabitants, print the identification numbers of all the divine ones in ascending order, one in a line. In addition, following the output numbers, print end in a line. Otherwise, i.e., if a given data set does not include sufficient information to identify all the divine members, print no in a line. 思路:我們可以這麼理解題目意思:有兩個種族的人,他們說自己人是“yes”,說其他種族的人是“no”。 我們可以先根據自己人權值0,其他族人權值1來建立一顆權值線段樹。這樣,我們可能有若干顆樹,分為兩個種族。假設兩個種族分別是x,y,因為不同樹有兩種種族人數,但無法分辨他們是哪個種族的人,題目需要明確其中一個種族人的編號,說明這個情況是唯一的,即樹的人員分配出現p1,p2的情況是唯一的。我們可以先統計不同樹上兩個種族的人數,通過計數dp來實現。
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath> using namespace std; #define ll long long
#define pb push_back
#define fi first
#define se second const int N = ;
struct node
{
int rt, v;
}fa[N];
int dp[N][N];
int a[N][];//不同樹上兩種人的人數
vector<int > p[N][]; //記錄編號
bool vis[N];
int n, p1, p2; int Find(int x)
{
if(fa[x].rt == x) return x;
else{
int tmp = fa[x].rt;
fa[x].rt = Find(fa[x].rt);
fa[x].v = (fa[x].v + fa[tmp].v) % ;
return fa[x].rt;
}
} void Union(int x, int y, int d)
{
int fax = Find(x);
int fay = Find(y);
if(fax != fay){
fa[fay].rt = fax;
fa[fay].v = (fa[x].v + d - fa[y].v + ) % ;
}
} void solve()
{
while(~scanf("%d%d%d", &n, &p1, &p2) && (n + p1 + p2)){ int m = p1 + p2;
for(int i = ; i <= m; ++i){
fa[i].rt = i;
fa[i].v = ;
a[i][] = ;
a[i][] = ;
p[i][].clear();
p[i][].clear();
vis[i] = ; for(int j = ; j <= m; ++j) dp[i][j] = ;
} int x, y;
char op[];
for(int i = ; i <= n; ++i){
scanf("%d%d%s", &x, &y, op);
Union(x, y, op[] == 'y' ? : );
} //所有葉子完整接收資訊
for(int i = ; i <= m; ++i) Find(i); int cnt = ;
for(int i = ; i <= m; ++i){
if(!vis[i]){
for(int j = ; j <= m; ++j){
if(fa[j].rt == fa[i].rt){
vis[j] = true;
a[cnt][fa[j].v]++;
p[cnt][fa[j].v].pb(j);
}
}
cnt++;
}
} dp[][] = ;
for(int i = ; i < cnt; ++i){
for(int j = ; j <= m; ++j){
if(j - a[i][] >= && dp[i - ][j - a[i][]]){
dp[i][j] += dp[i - ][j - a[i][]];
}
if(j - a[i][] >= && dp[i - ][j - a[i][]]){
dp[i][j] += dp[i - ][j - a[i][]];
}
}
}
//cout << "ans \\\\\\" << endl;
if(dp[cnt - ][p1] != ) printf("no\n");
else{
vector<int > info;
int remains = p1;
int x;
for(int i = cnt - ; i >= ; --i){
x = remains - a[i][];
if(dp[i - ][x] == ){
for(int o = ; o < a[i][]; ++o){
info.pb(p[i][][o]);
}
remains = x;
continue;
}
x = remains - a[i][];
if(dp[i - ][x] == ){
for(int o = ; o < a[i][]; ++o){
info.pb(p[i][][o]);
} remains = x;
}
} sort(info.begin(), info.end());
int sum = (int)info.size();
for(int i = ; i < sum; ++i) printf("%d\n", info[i]);
printf("end\n");
//printf("end\n\n\n");
}
}
} int main()
{ solve(); return ;
}