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来实现。
  1 #include 
  2 #include 
  3 #include 
  4 #include 
  5 #include 
  6 #include 
  7 
  8 using namespace std;
  9 
 10 #define ll long long
 11 #define pb push_back
 12 #define fi first
 13 #define se second
 14 
 15 const int N = 700;
 16 struct node
 17 {
 18     int rt, v;
 19 }fa[N];
 20 int dp[N][N];
 21 int a[N][2];//不同树上两种人的人数
 22 vector<int > p[N][2]; //记录编号
 23 bool vis[N];
 24 int n, p1, p2;
 25 
 26 int Find(int x)
 27 {
 28     if(fa[x].rt == x) return x;
 29     else{
 30         int tmp = fa[x].rt;
 31         fa[x].rt = Find(fa[x].rt);
 32         fa[x].v = (fa[x].v + fa[tmp].v) % 2;
 33         return fa[x].rt;
 34     }
 35 }
 36 
 37 void Union(int x, int y, int d)
 38 {
 39     int fax = Find(x);
 40     int fay = Find(y);
 41     if(fax != fay){
 42         fa[fay].rt = fax;
 43         fa[fay].v = (fa[x].v + d - fa[y].v + 2) % 2;
 44     }
 45 }
 46 
 47 void solve()
 48 {   
 49     while(~scanf("%d%d%d", &n, &p1, &p2) && (n + p1 + p2)){
 50         
 51         int m = p1 + p2;
 52         for(int i = 0; i <= m; ++i){
 53             fa[i].rt = i;
 54             fa[i].v = 0;
 55             a[i][0] = 0;
 56             a[i][1] = 0;
 57             p[i][0].clear();
 58             p[i][1].clear();
 59             vis[i] = 0;
 60         
 61             for(int j = 0; j <= m; ++j) dp[i][j] = 0;
 62         }
 63 
 64         int x, y;
 65         char op[5];
 66         for(int i = 1; i <= n; ++i){
 67             scanf("%d%d%s", &x, &y, op);
 68             Union(x, y, op[0] == 'y' ? 0 : 1);
 69         }
 70 
 71         //所有叶子完整接收信息
 72         for(int i = 1; i <= m; ++i) Find(i);
 73 
 74         int cnt = 1;
 75         for(int i = 1; i <= m; ++i){
 76             if(!vis[i]){
 77                 for(int j = 1; j <= m; ++j){
 78                     if(fa[j].rt == fa[i].rt){
 79                         vis[j] = true;
 80                         a[cnt][fa[j].v]++;
 81                         p[cnt][fa[j].v].pb(j);
 82                     }
 83                 }
 84                 cnt++;
 85             }
 86         }
 87 
 88         dp[0][0] = 1;
 89         for(int i = 1; i < cnt; ++i){
 90             for(int j = 0; j <= m; ++j){
 91                 if(j - a[i][0] >= 0 && dp[i - 1][j - a[i][0]]){
 92                     dp[i][j] += dp[i - 1][j - a[i][0]];
 93                 }
 94                 if(j - a[i][1] >= 0 && dp[i - 1][j - a[i][1]]){
 95                     dp[i][j] += dp[i - 1][j - a[i][1]];
 96                 }
 97             }
 98         }
 99         //cout << "ans    \\\\\\" << endl;
100         if(dp[cnt - 1][p1] != 1) printf("no\n");
101         else{
102             vector<int > info;
103             int remains = p1;
104             int x;
105             for(int i = cnt - 1; i >= 1; --i){
106                 x = remains - a[i][0];
107                 if(dp[i - 1][x] == 1){
108                     for(int o = 0; o < a[i][0]; ++o){
109                         info.pb(p[i][0][o]);
110                     }
111                     remains = x;
112                     continue;
113                 }
114                 x = remains - a[i][1];
115                 if(dp[i - 1][x] == 1){
116                     for(int o = 0; o < a[i][1]; ++o){
117                         info.pb(p[i][1][o]);
118                     }
119 
120                     remains = x;
121                 }
122             }
123 
124             sort(info.begin(), info.end());
125             int sum = (int)info.size();
126             for(int i = 0; i < sum; ++i) printf("%d\n", info[i]);
127             printf("end\n");
128             //printf("end\n\n\n");
129         }
130     }
131 }
132 
133 int main()
134 {
135 
136     solve();
137 
138     return 0;
139 }

 

你可能感兴趣的:(True Liars POJ - 1417)