思路:很简单的种类并查集,利用并查集可以将所有的人分成几个集合,每个集合又分为好人和坏人集合,直接进行背包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;
} 

如有不当之处欢迎指出!

POJ - 1417 并查集+背包的更多相关文章

  1. POJ 1417 并查集 dp

    After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...

  2. poj 1417(并查集+简单dp)

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2087   Accepted: 640 Descrip ...

  3. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  4. poj1417(带权并查集+背包DP+路径回溯)

    题目链接:http://poj.org/problem;jsessionid=8C1721AF1C7E94E125535692CDB6216C?id=1417 题意:有p1个天使,p2个恶魔,天使只说 ...

  5. poj1417 带权并查集 + 背包 + 记录路径

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

  6. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

  7. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  8. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  9. POJ 3228 [并查集]

    题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...

随机推荐

  1. tp5无法隐藏index.php入口文件

    一: 官方文件: <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine on Rewrite ...

  2. 【转】DELL R710服务器可以安装的VMWare ESX Server 4.1 全套下载带注册码

    随着R710的停产,R720随之面世,但DELL R720服务器只支持vmware esxi5.0以上,DELL客户经理给了一套系统安装后序列号无法解决,还是用4.1好了,却又发现怎么都无法安装.按网 ...

  3. 揭开Java内存管理的面纱

    前言 相对于C.C++这些高性能语言,Java有着让此类程序员羡慕的功能:内存自动管理.似乎这样,Java程序员不用再关心内存,也不用去了解相关知识.但结果真的是这样吗?特别对于我们这种Android ...

  4. 通过重写 class 的 ToString() 来简化获取 enum 的 DescriptionAttribute 值

    通过重写 class 的 ToString() 来简化获取 enum 的 DescriptionAttribute 值 目录 一.常见的 enum 类型 二.演变:class 版本的 enum 类型 ...

  5. Jmeter之性能测试类型

    pipe-clean test 在测试环境预先跑 确定脚本准确性 单用户跑 获取baseline   容量测试 经典的性能测试类型 获取系统稳定运行时最大的吞吐量/并发数/响应时间时 尽量模拟真实情况 ...

  6. Log4Net记录到MySql

    1.新建控制台程序. 2.添加Log4Net nuget 3.添加MySql 引用 4.添加配置文件如下: <?xml version="1.0"?> <conf ...

  7. 【OCR技术系列之四】基于深度学习的文字识别(3755个汉字)

    上一篇提到文字数据集的合成,现在我们手头上已经得到了3755个汉字(一级字库)的印刷体图像数据集,我们可以利用它们进行接下来的3755个汉字的识别系统的搭建.用深度学习做文字识别,用的网络当然是CNN ...

  8. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  9. Flink入门使用

    完全参考:Flink1.3QuickStart 启动本地运行 首先找一台安装了hadoop的linux. 将安装包解压,到bin目录启动local模式的脚本. tar -zxvf flink-1.3. ...

  10. qt中的tcp编程

    server .server.h #define DIALOG_H #include <QDialog> #include <QTcpServer> #include < ...