POJ - 1417 并查集+背包

         思路:很简单的种类并查集,利用并查集可以将所有的人分成几个集合,每个集合又分为好人和坏人集合,直接进行背包dp判断有多少种方法可以在取了所有集合并且人数正好凑足p1个好人的方案。dp(i, j)表示前i和集合有j个好人的方案,如果dp(n, p1)不等于1说明方案不止一种或则根本不存在。注意在dp时,记录路径。

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int> 
typedef long long LL;
const int maxn = 600 + 5;
struct node{
	int par;
	int real;
}a[maxn];
int find(int x, int &r) {
	if(a[x].par == x) {
		r = x;
		return 0;
	}
	int w = find(a[x].par, r);
	a[x].par = r;
	return a[x].real = (a[x].real + w) % 2;
}
void unionset(int x, int y, int real) {
	int r1, r2;
	int rx = find(x, r1), ry = find(y, r2);
	if(r1 != r2) {
		a[r1].par = y;
		a[r1].real = (real + rx) % 2; 
	}
}
int dp[maxn][maxn], pre[maxn][maxn], cnt[maxn][2];
bool vis[maxn]; 
vector<int>pep[maxn][2];


int main() {
	int n, p1, p2;
	while(scanf("%d%d%d", &n, &p1, &p2) == 3) {
		if(!p1 && !n && !p2) break;
		
		int num = p1 + p2;
		for(int i = 1; i <= num; ++i) {
			a[i].par = i;
			a[i].real = 0;
		}
		int x, y;
		char s[5];
		for(int i = 0; i < n; ++i) {
			scanf("%d%d%s", &x, &y, s);
			int  f = s[0] == 'y' ? 0 : 1; 
			unionset(x, y, f);
		}
		memset(cnt, 0, sizeof(cnt));
		memset(vis, 0, sizeof(vis));
		int r, r1, c = 1;
		for(int i = 1; i <= num; ++i) {
			if(!vis[i]) {
				pep[c][0].clear();
				pep[c][1].clear();
				find(i, r);
				for(int j = i; j <= num; ++j) {
					int k = find(j, r1);
					if(r1 == r) {
						vis[j] = 1;
						pep[c][k].push_back(j);
						cnt[c][k]++;
					}
				}
				++c;
			}
		}
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1; //边界 
		for(int i = 1; i < c; ++i)
			for(int j = p1; j >= 0; --j) {
				if(j-cnt[i][0] >= 0 && dp[i-1][j-cnt[i][0]]) {
					dp[i][j] += dp[i-1][j-cnt[i][0]];
					pre[i][j] = j - cnt[i][0];
				}
				if(j-cnt[i][1] >= 0 && dp[i-1][j-cnt[i][1]]) {
					dp[i][j] += dp[i-1][j-cnt[i][1]];
					pre[i][j] = j - cnt[i][1];
				}
			}
		if(dp[c-1][p1] != 1) {
			printf("no\n");
			continue;
		}
		vector<int>ans;
		ans.clear();
		int t = p1;
		for(int i = c-1; i >= 1; --i) {
			int tmp = t -  pre[i][t];
			if(cnt[i][0] == tmp) {
				for(int j = 0; j < pep[i][0].size(); ++j) 
					ans.push_back(pep[i][0][j]);
			}
			else {
				for(int j = 0; j < pep[i][1].size(); ++j) 
					ans.push_back(pep[i][1][j]);
			}
			t = pre[i][t];
		}
		sort(ans.begin(), ans.end());
		for(int i = 0; i < ans.size(); ++i) {
			printf("%d\n", ans[i]);
		}
		printf("end\n");
	}
	return 0;
} 

如有不当之处欢迎指出!


转载于:https://www.cnblogs.com/flyawayl/p/8305331.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值