poj1417 with right and check set + backpack + record path

Source: Internet
Author: User
Tags x2 y2

True Liars
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2713 Accepted: 868

Description

After have drifted about in a small boat for a couple of days, Akira Crusoe Maeda is finally cast ashore on a foggy ISL and. Though He was exhausted and despaired, he is still fortunate to remember a legend of the Foggy Island, which he had heard From patriarchs on his childhood. This must is the island in the legend. In the legend, the tribes has inhabited the island, one is divine and the other are devilish, once members of the Divine T Ribe Bless you, your The future is bright and promising, and your soul would eventually go to Heaven, in contrast, once members Of the Devilish tribe curse you, your future is bleak and hopeless, and your soul would 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 was Truth-tellers, that's, they always tell the truth and those of the devilish tribe was Liars, that's, they always tell a lie.

He asked some of them whether or not some is 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 for Ever when he does not is like the question. He had another piece of useful informationf the legend tells the populations of both. These numbers in the legend be trustworthy since everyone living on this island is immortal and none has ever been born At least these millennia.

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

Input

The input consists of multiple data sets, each of 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 is the populations of the divine and devilish tribes, respectively, in the legend. Each of the following n lines have a 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 "was you a member of the Divine Tribe?" was a valid question. Note also that both lines may have the same X ' s and Y's since Akira was very upset and might has asked the same question T o the same one more than once.

You may assume, that N was less than, and that P1 and P2 were less than 300. A line with three zeros, i.e., 0 0 0, represents the end of the input. You can assume this each data set is consistent and no contradictory answers is 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 with 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.

Sample Input

2 1 2 NO2 1 NO3 2 1 yes2 2 Yes3 3 yes2 2 One 2 yes2 3 No5 4 2 yes1 3 No4 5 yes5 6 yes6 7 no0 0 0

Sample Output

No
No
1
2
End
3
4
5
6
End Test Instructions: There are n people, p1 a good person p2 a bad person. So if a person says another person is a good person, then if this person is a good person, that the other side is really a good person, if this is a bad person, that the sentence is false, the other side is bad. If a person says another person is bad, then if this person is a good person, that the other side is a bad person, if this is a bad person, indicating that the other is a good person. That is, if the condition is yes, the two are the same set, otherwise two different sets. Here you can use the Take-right and check-set processing. Then there is now a set of K, each set of bad people and good people, how to combine to meet the requirements. You can use a backpack to handle it. DP[I][J] Indicates if there is a J person after the first set. DP[K][P1] = 1 means existence and uniqueness, dp[k][p1] = 0 means no, dp[k][p1]>1 represents more than one. Then the key is how to output all the satisfied people. At this time, you can record the path in the backpack, the status of the current satisfaction is the state of the above push over.
#include <Set>#include<map>#include<queue>#include<stack>#include<cmath>#include<string>#include<time.h>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#defineINF 1000000001#definell Long Long#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespacestd;Const intMAXN =1010;intPa[maxn],n,p1,p2,rel[maxn],vis[maxn];vector<int>b[maxn][2];inta[maxn][2],DP[MAXN][MAXN],PRE[MAXN][MAXN];voidInit () { for(inti =0; I <= p1 + p2; i++) {Pa[i]=i; b[i][0].clear (); b[i][1].clear (); a[i][0] =0; a[i][1] =0; } memset (rel,0,sizeof(rel));}intFindintx) {    if(X! =Pa[x]) {        intFX =find (pa[x]); REL[X]= (Rel[x] + rel[pa[x])%2; PA[X]=FX; }    returnpa[x];}intMain () { while(~SCANF ("%d%d%d",&n,&p1,&p2)) {        if(!n &&!p1 &&!p2) Break;        Init (); intx, Y, Z Chars[Ten];  for(inti =0; I < n; i++) {scanf ("%d%d%s",&x,&y,s); if(s[0] =='y') {Z=0; }            Else{z=1; }            intFX =find (x); intFY =find (y); if(FX! =FY) {PA[FX]=fy; REL[FX]= (2-REL[X] + z + rel[y])%2; }} memset (Vis,0,sizeof(VIS)); intCNT =1;  for(inti =1; I <= p1 + p2; i++){            if(!Vis[i]) {                intTP =find (i);  for(intj = i; J <= P1 + p2; J + +){                    intFP =find (j); if(fp = =TP) {Vis[j]=1;                        B[cnt][rel[j]].push_back (j); A[CNT][REL[J]]++; }                }                //cout<<a[cnt][0]<< ' <<a[cnt][1]<<endl;CNT + +; }} memset (Vis,0,sizeof(VIS)); Memset (DP,0,sizeof(DP)); dp[0][0] =1;  for(inti =1; I < CNT; i++){             for(intj = P1; J >=0; j--){                if(j-a[i][0] >=0&& dp[i-1][j-a[i][0]]) {DP[I][J]+ = dp[i-1][j-a[i][0]]; PRE[I][J]= j-a[i][0]; }                if(j-a[i][1] >=0&& dp[i-1][j-a[i][1]]) {DP[I][J]+ = dp[i-1][j-a[i][1]]; PRE[I][J]= j-a[i][1]; }            }        }        if(dp[cnt-1][P1]! =1) {printf ("no\n"); }        Else{vector<int>ans; intL =P1;  for(inti = cnt-1; I >=1; i--){                intTP = L-Pre[i][l]; if(TP = = a[i][0]){                     for(intj =0; J < b[i][0].size (); J + +) {ans.push_back (b[i][0][j]); }                }                Else {                     for(intj =0; J < b[i][1].size (); J + +) {ans.push_back (b[i][1][j]); }} L=Pre[i][l];            } sort (Ans.begin (), Ans.end ());  for(inti =0; I < ans.size (); i++) {printf ("%d\n", Ans[i]); } printf ("end\n"); }    }    return 0;}

poj1417 with right and check set + backpack + record path

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.