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; }
如有不当之处欢迎指出!
POJ - 1417 并查集+背包的更多相关文章
- POJ 1417 并查集 dp
After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...
- poj 1417(并查集+简单dp)
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2087 Accepted: 640 Descrip ...
- poj 1984 并查集
题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...
- poj1417(带权并查集+背包DP+路径回溯)
题目链接:http://poj.org/problem;jsessionid=8C1721AF1C7E94E125535692CDB6216C?id=1417 题意:有p1个天使,p2个恶魔,天使只说 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- poj 1797(并查集)
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
- POJ 2492 并查集扩展(判断同性恋问题)
G - A Bug's Life Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- POJ 2492 并查集应用的扩展
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...
- POJ 3228 [并查集]
题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...
随机推荐
- scrapy_xpath
什么是xpath? 路径表达式 在xml和html中进行导航 包含标准函数库 遵循w3c标准 xpth节点关系是什么? 父节点 子节点 兄弟节点 先辈节点 后代节点 xpth语法 a ...
- Java多线程之线程的通信
Java多线程之线程的通信 在总结多线程通信前先介绍一个概念:锁池.线程因为未拿到锁标记而发生的阻塞不同于前面五个基本状态中的阻塞,称为锁池.每个对象都有自己的锁池的空间,用于放置等待运行的线程.这些 ...
- PHP 构造方法 __construct()(转)
PHP 析构方法 __destruct() 构造方法是类中的一个特殊方法.当使用 new 操作符创建一个类的实例时,构造方法将会自动调用,其名称必须是 __construct() . 在一个类中只能声 ...
- zabbix_sender用法实例
环境centos6.8 zabbix版本3.2.4 需求: 要远程监控一台服务器A,但只能通过远程服务器连接本地服务器B,但B不能主动连A(因为A没有固定公网ip) 使用了zabbix_agent的a ...
- 前端自动化测试神器-Katalon进阶用法
前言 上一篇介绍了Katalon的基础用法,本篇继续介绍一些进阶的用法. Keyword 和 Method Call Statement Keyword Keyword就是自定义方法,该方法在当前项目 ...
- root cause org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "XXX")
在执行一个查询语句的时候,mybatis报错:root cause org.apache.ibatis.ognl.OgnlException: source is null for getProper ...
- ffmpeg转码使用硬件加速
需求源于手机拍摄的视频,默认参数码率较大,拍摄的文件体积较大,不便于保存和转发.手机默认拍照的720P视频,默认码率达到4M,实际上转成1M就差不多了.FFmpeg默认的转码是使用软件解码,然后软件编 ...
- 洛谷 [P1169] [ZJOI2007] 最大的正方形
本题是一道求最大子矩阵的题,可以使用悬线法来做,因为是相邻的01矩阵,所以需要对悬线法进行改动. #include <iostream> #include <cstdio> # ...
- Validate Model State automatically in ASP.NET Core 2.0
if (!ModelState.IsValid) { //TODO 模型验证失败需要做的事情 } 上面的代码不管是在传统的ASP.NET还是新一代ASP.NET Core中都是为了验证模型的状态是否合 ...
- Windows Azure Virtual Machine (34) Azure VM挂载WebDAV
<Windows Azure Platform 系列文章目录> 之前使用Azure VM,挂载box网盘.发现不能正常挂载,这里简单记录一下. 1.WebDAV的网络映射,需要WebCli ...