True Liars POJ - 1417
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
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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath> using namespace std; #define ll long long
#define pb push_back
#define fi first
#define se second const int N = ;
struct node
{
int rt, v;
}fa[N];
int dp[N][N];
int a[N][];//不同树上两种人的人数
vector<int > p[N][]; //记录编号
bool vis[N];
int n, p1, p2; int Find(int x)
{
if(fa[x].rt == x) return x;
else{
int tmp = fa[x].rt;
fa[x].rt = Find(fa[x].rt);
fa[x].v = (fa[x].v + fa[tmp].v) % ;
return fa[x].rt;
}
} void Union(int x, int y, int d)
{
int fax = Find(x);
int fay = Find(y);
if(fax != fay){
fa[fay].rt = fax;
fa[fay].v = (fa[x].v + d - fa[y].v + ) % ;
}
} void solve()
{
while(~scanf("%d%d%d", &n, &p1, &p2) && (n + p1 + p2)){ int m = p1 + p2;
for(int i = ; i <= m; ++i){
fa[i].rt = i;
fa[i].v = ;
a[i][] = ;
a[i][] = ;
p[i][].clear();
p[i][].clear();
vis[i] = ; for(int j = ; j <= m; ++j) dp[i][j] = ;
} int x, y;
char op[];
for(int i = ; i <= n; ++i){
scanf("%d%d%s", &x, &y, op);
Union(x, y, op[] == 'y' ? : );
} //所有叶子完整接收信息
for(int i = ; i <= m; ++i) Find(i); int cnt = ;
for(int i = ; i <= m; ++i){
if(!vis[i]){
for(int j = ; j <= m; ++j){
if(fa[j].rt == fa[i].rt){
vis[j] = true;
a[cnt][fa[j].v]++;
p[cnt][fa[j].v].pb(j);
}
}
cnt++;
}
} dp[][] = ;
for(int i = ; i < cnt; ++i){
for(int j = ; j <= m; ++j){
if(j - a[i][] >= && dp[i - ][j - a[i][]]){
dp[i][j] += dp[i - ][j - a[i][]];
}
if(j - a[i][] >= && dp[i - ][j - a[i][]]){
dp[i][j] += dp[i - ][j - a[i][]];
}
}
}
//cout << "ans \\\\\\" << endl;
if(dp[cnt - ][p1] != ) printf("no\n");
else{
vector<int > info;
int remains = p1;
int x;
for(int i = cnt - ; i >= ; --i){
x = remains - a[i][];
if(dp[i - ][x] == ){
for(int o = ; o < a[i][]; ++o){
info.pb(p[i][][o]);
}
remains = x;
continue;
}
x = remains - a[i][];
if(dp[i - ][x] == ){
for(int o = ; o < a[i][]; ++o){
info.pb(p[i][][o]);
} remains = x;
}
} sort(info.begin(), info.end());
int sum = (int)info.size();
for(int i = ; i < sum; ++i) printf("%d\n", info[i]);
printf("end\n");
//printf("end\n\n\n");
}
}
} int main()
{ solve(); return ;
}
True Liars POJ - 1417的更多相关文章
- POJ1417 True Liars —— 并查集 + DP
		题目链接:http://poj.org/problem?id=1417 True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submi ... 
- poj 1417(并查集+简单dp)
		True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2087 Accepted: 640 Descrip ... 
- POJ1417 True Liars
		题意 Language:Default True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6392 Accep ... 
- POJ1417:True Liars(DP+带权并查集)
		True Liars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ... 
- POJ 1417 - True Liars - [带权并查集+DP]
		题目链接:http://poj.org/problem?id=1417 Time Limit: 1000MS Memory Limit: 10000K Description After having ... 
- poj 1417 True Liars(并查集+背包dp)
		题目链接:http://poj.org/problem?id=1417 题意:就是给出n个问题有p1个好人,p2个坏人,问x,y是否是同类人,坏人只会说谎话,好人只会说实话. 最后问能否得出全部的好人 ... 
- POJ 1417 True Liars(种类并查集+dp背包问题)
		题目大意: 一共有p1+p2个人,分成两组,一组p1,一组p2.给出N个条件,格式如下: x y yes表示x和y分到同一组,即同是好人或者同是坏人. x y no表示x和y分到不同组,一个为好人,一 ... 
- POJ 1417 True Liars
		题意:有两种人,一种人只会说真话,另一种人只会说假话.只会说真话的人有p1个,另一种人有p2个.给出m个指令,每个指令为a b yes/no,意思是,如果为yes,a说b是只说真话的人,如果为no,a ... 
- POJ 1417 并查集 dp
		After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ... 
随机推荐
- Spring AOP 之二:Pointcut注解表达式
			简介 在Spring AOP概述中我们重点注意的是AOP的整体流程和Advice,简化了一些其他的东西,其中就有一些对灵活应用Spring AOP很重要的知识点,例如Pointcut表达式,下面就介绍 ... 
- FFT,NTT入门
			目录 -1.前置知识 复数 单位根 单位根反演 0.卷积 1.FFT -1.前置知识 复数 复数单位\(i\):定义为\(i^2=-1\).\(i\)可以直接参与运算. 复数:形如\(z=a+ ... 
- @bzoj - 5104@ Fib数列
			目录 @description@ @solution@ @accepted code@ @details@ @description@ Fib数列为1,1,2,3,5,8... 求在Mod10^9+9 ... 
- mitmdump+python的使用(代码篇)
			mitmproxy+python代码篇 一.上个推文我们介绍了mitmdump的简单操作,下面我们开始学习,mitmdump配合python脚本的使用.第一点先讲日志输出.请看图片 先导入ctx模块: ... 
- 从零开始的Spring Boot(3、Spring Boot静态资源和文件上传)
			Spring Boot静态资源和文件上传 写在前面 从零开始的Spring Boot(2.在Spring Boot中整合Servlet.Filter.Listener的方式) https://www. ... 
- 【环境安装】Docker安装
			[环境安装]Docker安装 CentoOS-7 安装步骤: 1.卸载已经安装的Docker sudo yum remove docker \ docker-client \ docker-clien ... 
- Latex 安装 教程
			需要安装texlive和编辑器texstudio. 安装教程如https://www.cnblogs.com/dingruihfut/p/9690073.html 
- input属性设置type="number"之后, 仍可输入e;input限制只输入数字
			只需在行内输入 onKeyUp="this.value=this.value.replace(/[^\.\d]/g,'');" 就解决了 <input typ ... 
- 重学 Java 设计模式:实战命令模式「模拟高档餐厅八大菜系,小二点单厨师烹饪场景」
			作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 持之以恒的重要性 初学编程往往都很懵,几乎在学习的过程中会遇到 ... 
- java之SFTP上传下载
			import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.ut ... 
