True Liars POJ - 1417 (并查集 + dp + 记录路径)

 

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 i

nclude sufficient information to identify all the divine members, print no in a line.

 

思路:若yes则两个为同类,no则两个为异类。种类并查集来分组

int find(int x){
	if(par[x] == x){
		return x;  
	}
	int fa = par[x];
	par[x] = find(par[x]);
	v[x] = (v[x] + v[fa] + 2) % 2; //种类并查集常规操作,用偏移量来表示节点与根节点的关系, 1为异类,0为同类
	return par[x];
}

void unit(int x, int y, int temp){
	int fx = find(x);
	int fy = find(y);

	if(fx == fy){
		return ;
	}

	par[fy] = fx;
	v[fy] = (v[x] + v[y] + temp + 2) % 2; 
}

 最后会得到好几个集合,每个集合的节点记录了与根节点之间的关系,我们用map将这些集合提取到数组里,

void insert(int x, int temp){ //x为根节点, temp为与根节点之间的关系
	if(mmap[x] == 0){ // 若改父节点未出现过,则++cnt,新得到的cnt为以x为根节点的集合再数组中的下标
		mmap[x] = ++cnt;
	}
	bag[mmap[x]][temp]++;
}



		for(int i = 1; i <= p1 + p2; i++){
			int fa = find(i);
			insert(fa, v[i]);
		}

然后就是背包了,dp[i][j]记录前i个集合和为j的情况的数量,注意这里的集合是不能单选的,必须和前面选的加起来

        dp[0][0] = 1;
		for(int i = 1; i <= cnt; i++){
			for(int j = 0; j <= p1; j++){
				if(j >= bag[i][0]){
					dp[i][j] = dp[i - 1][j - bag[i][0]];
				}
				if(j >= bag[i][1]){
					dp[i][j] += dp[i - 1][j - bag[i][1]];
				}
			}
		}

dp完得到和为p1的数量,若dp[cnt][p1]  == 1,说明可以区分,则反向寻找路径, 下面为ac代码

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <map>
#include <string.h>
using namespace std;

const int MAX = 1e3 + 50;
int par[MAX];
int v[MAX];
map<int, int> mmap;
int bag[MAX][2];
int cnt = 0;
int dp[MAX][MAX];
void insert(int x, int temp){ //x为根节点, temp为与根节点之间的关系
	if(mmap[x] == 0){ // 若改父节点未出现过,则++cnt,新得到的cnt为以x为根节点的集合再数组中的下标
		mmap[x] = ++cnt;
	}
	bag[mmap[x]][temp]++;
}

int find(int x){
	if(par[x] == x){
		return x;  
	}
	int fa = par[x];
	par[x] = find(par[x]);
	v[x] = (v[x] + v[fa] + 2) % 2; //种类并查集常规操作,用偏移量来表示节点与根节点的关系, 1为异类,0为同类
	return par[x];
}

void unit(int x, int y, int temp){
	int fx = find(x);
	int fy = find(y);

	if(fx == fy){
		return ;
	}

	par[fy] = fx;
	v[fy] = (v[x] + v[y] + temp + 2) % 2; 
}

int main(int argc, char const *argv[])
{
	int n, p1, p2;
	while(1){
		cnt = 0;
		memset(dp, 0, sizeof(dp));
		mmap.clear();
		memset(bag, 0, sizeof(bag));
		scanf("%d%d%d", &n, &p1, &p2);
		if(n == 0 && p1 == 0 && p2 == 0){
			break;
		}
		for(int i = 1; i <= p1 + p2; i++){
			par[i] = i;
			v[i] = 0;
		}
		for(int i = 0; i < n; i++){
			int a, b, temp;
			char s[10];
			scanf("%d%d%s", &a, &b, s);
			if(s[0] == 'y'){
				temp = 0;
			} else{
				temp = 1;
			}
			unit(a, b, temp);
		}

		for(int i = 1; i <= p1 + p2; i++){
			int fa = find(i);
			insert(fa, v[i]);
		}

		dp[0][0] = 1;
		for(int i = 1; i <= cnt; i++){
			for(int j = 0; j <= p1; j++){
				if(j >= bag[i][0]){
					dp[i][j] = dp[i - 1][j - bag[i][0]];
				}
				if(j >= bag[i][1]){
					dp[i][j] += dp[i - 1][j - bag[i][1]];
				}
			}
		}
		if(dp[cnt][p1] == 1){
			int choose[MAX];
			for(int i = 1; i <= cnt; i++){
				choose[i] = -1;
			}

			int j = p1;
			for(int i = cnt; i > 0; i--){
				if(dp[i][j] == dp[i - 1][j - bag[i][0]]){ // 注意!因为dp[cnt][p1] = 1,所以只存在一种情况到达p1
					choose[i] = 0;
					j -= bag[i][0];
				} else if(dp[i][j] == dp[i - 1][j - bag[i][1]]){
					choose[i] = 1;
					j -= bag[i][1];
				}
			}

			for(int i = 1; i <= p1 + p2; i++){
				int fa = find(i);
				int num = mmap[fa];
				if(v[i] == choose[num]){
					printf("%d\n", i);
				}
			}
			printf("end\n");
		} else{
			printf("no\n");
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值